From bcc45dc076d8075cd87225f7f671f2ccc9e2bb77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 9 Aug 2018 08:51:50 +0200 Subject: [PATCH] no more throwing conversion methods, and refactor ValueConversionDebuggingInfo --- GRDB/Core/DatabaseValue.swift | 4 +- GRDB/Core/DatabaseValueConversion.swift | 164 +++++++++--------- GRDB/Core/DatabaseValueConvertible.swift | 20 ++- GRDB/Core/Row.swift | 66 +++---- GRDB/Core/RowAdapter.swift | 12 +- GRDB/Core/StatementColumnConvertible.swift | 5 +- .../DatabaseValueConvertible+Decodable.swift | 30 ++-- GRDB/Record/FetchableRecord+Decodable.swift | 6 +- 8 files changed, 161 insertions(+), 146 deletions(-) diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 27a5c7480..6e724ea0c 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -227,7 +227,7 @@ extension DatabaseValue { /// conversion error @available(*, deprecated) public func losslessConvert(sql: String? = nil, arguments: StatementArguments? = nil) -> T where T : DatabaseValueConvertible { - return require { try T.decode(from: self, debugInfo: ValueConversionDebuggingInfo()) } + return T.decode(from: self, debugInfo: ValueConversionDebuggingInfo()) } /// Converts the database value to the type Optional. @@ -251,7 +251,7 @@ extension DatabaseValue { /// conversion error @available(*, deprecated) public func losslessConvert(sql: String? = nil, arguments: StatementArguments? = nil) -> T? where T : DatabaseValueConvertible { - return require { try T.decodeIfPresent(from: self, debugInfo: ValueConversionDebuggingInfo()) } + return T.decodeIfPresent(from: self, debugInfo: ValueConversionDebuggingInfo()) } } diff --git a/GRDB/Core/DatabaseValueConversion.swift b/GRDB/Core/DatabaseValueConversion.swift index 24231c090..5ee4f7266 100644 --- a/GRDB/Core/DatabaseValueConversion.swift +++ b/GRDB/Core/DatabaseValueConversion.swift @@ -6,113 +6,115 @@ // All primitive value conversion methods. -/// An alternative to try!, preferred for conversion methods. -@inline(__always) -func require(file: StaticString = #file, line: UInt = #line, block: () throws -> T) -> T { - do { - return try block() - } catch { - fatalError(String(describing: error), file: file, line: line) - } -} - /// A type that helps the user understanding value conversion errors struct ValueConversionDebuggingInfo { - private var _statement: SelectStatement? - private var _row: Row? - private var _columnIndex: Int? - private var _columnName: String? - + enum Source { + case statement(SelectStatement) + case sql(String, StatementArguments) + case row(Row) + } + enum Column { + case columnIndex(Int) + case columnName(String) + } + private var source: Source? + private var column: Column? + init(statement: SelectStatement? = nil, row: Row? = nil, columnIndex: Int? = nil, columnName: String? = nil) { - _statement = statement - _row = row - _columnIndex = columnIndex - _columnName = columnName + fatalError("not implemented") } - var statement: SelectStatement? { - return _statement ?? _row?.statement + var sql: String? { + guard let source = source else { return nil } + switch source { + case .statement(let statement): return statement.sql + case .row(let row): return row.statement?.sql + case .sql(let sql, _): return sql + } + } + + var arguments: StatementArguments? { + guard let source = source else { return nil } + switch source { + case .statement(let statement): return statement.arguments + case .row(let row): return row.statement?.arguments + case .sql(_, let arguments): return arguments + } } var row: Row? { - return _row ?? _statement.map { Row(statement: $0) } + guard let source = source else { return nil } + switch source { + case .statement(let statement): return Row(statement: statement) + case .row(let row): return row + case .sql: return nil + } } var columnIndex: Int? { - if let columnIndex = _columnIndex { - return columnIndex + guard let column = column else { return nil } + switch column { + case .columnIndex(let index): return index + case .columnName(let name): return row?.index(ofColumn: name) } - if let columnName = _columnName, let row = row { - return row.index(ofColumn: columnName) - } - return nil } var columnName: String? { - if let columnName = _columnName { - return columnName - } - if let columnIndex = _columnIndex, let row = row { - let rowIndex = row.index(row.startIndex, offsetBy: columnIndex) + guard let column = column else { return nil } + switch column { + case .columnIndex(let index): + guard let row = row else { return nil } + let rowIndex = row.index(row.startIndex, offsetBy: index) return row[rowIndex].0 + case .columnName(let name): return name } - return nil } } -/// A conversion error -struct ValueConversionError: Error, CustomStringConvertible { - var dbValue: DatabaseValue - var debugInfo: ValueConversionDebuggingInfo - - var description: String { - var error = "could not convert database value \(dbValue) to \(T.self)" - var extras: [String] = [] - if let columnName = debugInfo.columnName { - extras.append("column: `\(columnName)`") - } - if let columnIndex = debugInfo.columnIndex { - extras.append("column index: \(columnIndex)") - } - if let row = debugInfo.row { - extras.append("row: \(row)") - } - if let statement = debugInfo.statement { - extras.append("statement: `\(statement.sql)`") - if statement.arguments.isEmpty == false { - extras.append("arguments: \(statement.arguments)") - } - } - if extras.isEmpty == false { - error += " (" + extras.joined(separator: ", ") + ")" - } - return error +/// The canonical conversion fatal error +func fatalConversionError(to: T.Type, from dbValue: DatabaseValue, debugInfo: ValueConversionDebuggingInfo) -> Never { + var message = "could not convert database value \(dbValue) to \(T.self)" + var extras: [String] = [] + if let columnName = debugInfo.columnName { + extras.append("column: `\(columnName)`") } + if let columnIndex = debugInfo.columnIndex { + extras.append("column index: \(columnIndex)") + } + if let row = debugInfo.row { + extras.append("row: \(row)") + } + if let sql = debugInfo.sql { + extras.append("statement: `\(sql)`") + if let arguments = debugInfo.arguments, arguments.isEmpty == false { + extras.append("arguments: \(arguments)") + } + } + if extras.isEmpty == false { + message += " (" + extras.joined(separator: ", ") + ")" + } + fatalError(message) } extension DatabaseValueConvertible { /// Performs lossless conversion from a database value. - /// - /// - throws: ValueConversionError - static func decode(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Self { + static func decode(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self { if let value = fromDatabaseValue(dbValue) { return value } else { - throw ValueConversionError(dbValue: dbValue, debugInfo: debugInfo()) + fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo()) } } /// Performs lossless conversion from a database value. - /// - /// - throws: ValueConversionError - static func decodeIfPresent(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Self? { + static func decodeIfPresent(from dbValue: DatabaseValue, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self? { // Use fromDatabaseValue before checking for null: this allows DatabaseValue to convert NULL to .null. if let value = fromDatabaseValue(dbValue) { return value } else if dbValue.isNull { return nil } else { - throw ValueConversionError(dbValue: dbValue, debugInfo: debugInfo()) + fatalConversionError(to: Self.self, from: dbValue, debugInfo: debugInfo()) } } } @@ -120,12 +122,10 @@ extension DatabaseValueConvertible { extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// Performs lossless conversion from a statement value. - /// - /// - throws: ValueConversionError @inline(__always) - static func decode(from sqliteStatement: SQLiteStatement, index: Int32, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Self { + static func decode(from sqliteStatement: SQLiteStatement, index: Int32, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Self { if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL { - throw ValueConversionError(dbValue: .null, debugInfo: debugInfo()) + fatalConversionError(to: Self.self, from: .null, debugInfo: debugInfo()) } return self.init(sqliteStatement: sqliteStatement, index: index) } @@ -146,41 +146,41 @@ extension Row { func decodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? { - return try Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + return Value.decodeIfPresent(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) } @inline(__always) func decode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value { - return try Value.decode(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + return Value.decode(from: impl.databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) } @inline(__always) func fastDecodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? { if let sqliteStatement = sqliteStatement { return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index)) } - return try impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + return impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) } @inline(__always) func fastDecode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value { if let sqliteStatement = sqliteStatement { - return try Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo) + return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo) } - return try impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) + return impl.fastDecode(Value.self, atUncheckedIndex: index, debugInfo: debugInfo) } } diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index 356b64172..d27adbb86 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -70,10 +70,16 @@ public final class DatabaseValueCursor : Cursor return nil case SQLITE_ROW: let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex) - return require { try Value.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) } + return Value.decode( + from: dbValue, + debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) case let code: statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) + throw DatabaseError( + resultCode: code, + message: statement.database.lastErrorMessage, + sql: statement.sql, + arguments: statement.arguments) } } } @@ -110,10 +116,16 @@ public final class NullableDatabaseValueCursor return nil case SQLITE_ROW: let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex) - return require { try Value.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) } + return Value.decodeIfPresent( + from: dbValue, + debugInfo: ValueConversionDebuggingInfo(statement: statement, columnIndex: Int(columnIndex))) case let code: statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) + throw DatabaseError( + resultCode: code, + message: statement.database.lastErrorMessage, + sql: statement.sql, + arguments: statement.arguments) } } } diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 512f767f0..4de7c2579 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -195,10 +195,10 @@ extension Row { /// fail, a fatal error is raised. public subscript(_ index: Int) -> Value? { GRDBPrecondition(index >= 0 && index < count, "row index out of range") - return require { try decodeIfPresent( + return decodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } /// Returns the value at given index, converted to the requested type. @@ -215,10 +215,10 @@ extension Row { /// (see https://www.sqlite.org/datatype3.html). public subscript(_ index: Int) -> Value? { GRDBPrecondition(index >= 0 && index < count, "row index out of range") - return require { try fastDecodeIfPresent( + return fastDecodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } /// Returns the value at given index, converted to the requested type. @@ -230,10 +230,10 @@ extension Row { /// SQLite value can not be converted to `Value`. public subscript(_ index: Int) -> Value { GRDBPrecondition(index >= 0 && index < count, "row index out of range") - return require { try decode( + return decode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } /// Returns the value at given index, converted to the requested type. @@ -249,10 +249,10 @@ extension Row { /// (see https://www.sqlite.org/datatype3.html). public subscript(_ index: Int) -> Value { GRDBPrecondition(index >= 0 && index < count, "row index out of range") - return require { try fastDecode( + return fastDecode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } /// Returns Int64, Double, String, Data or nil, depending on the value @@ -288,10 +288,10 @@ extension Row { guard let index = index(ofColumn: columnName) else { return nil } - return require { try decodeIfPresent( + return decodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } /// Returns the value at given column, converted to the requested type. @@ -310,10 +310,10 @@ extension Row { guard let index = index(ofColumn: columnName) else { return nil } - return require { try fastDecodeIfPresent( + return fastDecodeIfPresent( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } /// Returns the value at given column, converted to the requested type. @@ -330,10 +330,10 @@ extension Row { // Programmer error fatalError("no such column: \(columnName)") } - return require { try decode( + return decode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } /// Returns the value at given column, converted to the requested type. @@ -354,10 +354,10 @@ extension Row { // Programmer error fatalError("no such column: \(columnName)") } - return require { try fastDecode( + return fastDecode( Value.self, atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } /// Returns Int64, Double, String, NSData or nil, depending on the value @@ -441,9 +441,9 @@ extension Row { /// than the row's lifetime. public func dataNoCopy(atIndex index: Int) -> Data? { GRDBPrecondition(index >= 0 && index < count, "row index out of range") - return require { try impl.dataNoCopy( + return impl.dataNoCopy( atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index)) } /// Returns the optional Data at given column. @@ -461,9 +461,9 @@ extension Row { guard let index = index(ofColumn: columnName) else { return nil } - return require { try impl.dataNoCopy( + return impl.dataNoCopy( atUncheckedIndex: index, - debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } + debugInfo: ValueConversionDebuggingInfo(row: self, columnIndex: index, columnName: columnName)) } /// Returns the optional `NSData` at given column. @@ -1205,9 +1205,9 @@ protocol RowImpl { func columnName(atUncheckedIndex index: Int) -> String func hasNull(atUncheckedIndex index:Int) -> Bool func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue - func fastDecode(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value - func fastDecodeIfPresent(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? - func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? + func fastDecode(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value + func fastDecodeIfPresent(_ type: Value.Type, atUncheckedIndex index: Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? + func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? /// Returns the index of the leftmost column that matches *name* (case-insensitive) func index(ofColumn name: String) -> Int? @@ -1242,24 +1242,24 @@ extension RowImpl { func fastDecode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value { // default implementation is slow - return try Value.decode(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + return Value.decode(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) } func fastDecodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? { // default implementation is slow - return try Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) } - func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? { + func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? { // default implementation copies - return try Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) + return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), debugInfo: debugInfo) } } @@ -1359,7 +1359,7 @@ private struct StatementRowImpl : RowImpl { return sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL } - func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? { + func dataNoCopy(atUncheckedIndex index:Int, debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? { guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else { return nil } @@ -1376,14 +1376,14 @@ private struct StatementRowImpl : RowImpl { func fastValue( atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value { - return try Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo) + return Value.decode(from: sqliteStatement, index: Int32(index), debugInfo: debugInfo) } func fastOptionalValue( atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? { return Value.decodeIfPresent(from: sqliteStatement, index: Int32(index)) } diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index 0f68e839f..6cdf94aff 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -504,27 +504,27 @@ struct AdaptedRowImpl : RowImpl { func fastDecode( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value { let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index) - return try base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation + return base.impl.fastDecode(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation } func fastDecodeIfPresent( _ type: Value.Type, atUncheckedIndex index: Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Value? + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Value? { let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index) - return try base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation + return base.impl.fastDecodeIfPresent(Value.self, atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation } func dataNoCopy( atUncheckedIndex index:Int, - debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) throws -> Data? + debugInfo: @autoclosure () -> ValueConversionDebuggingInfo) -> Data? { let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index) - return try base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation + return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex, debugInfo: debugInfo) // base.impl: Demeter violation } func columnName(atUncheckedIndex index: Int) -> String { diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index abf1f34a6..70bb71814 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -81,7 +81,10 @@ public final class ColumnCursor Bool { return require { try Bool.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Int.Type) throws -> Int { return require { try Int.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Int8.Type) throws -> Int8 { return require { try Int8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Int16.Type) throws -> Int16 { return require { try Int16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Int32.Type) throws -> Int32 { return require { try Int32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Int64.Type) throws -> Int64 { return require { try Int64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: UInt.Type) throws -> UInt { return require { try UInt.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: UInt8.Type) throws -> UInt8 { return require { try UInt8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: UInt16.Type) throws -> UInt16 { return require { try UInt16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: UInt32.Type) throws -> UInt32 { return require { try UInt32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: UInt64.Type) throws -> UInt64 { return require { try UInt64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Float.Type) throws -> Float { return require { try Float.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: Double.Type) throws -> Double { return require { try Double.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } - func decode(_ type: String.Type) throws -> String { return require { try String.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } } + func decode(_ type: Bool.Type) throws -> Bool { return Bool.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Int.Type) throws -> Int { return Int.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Int8.Type) throws -> Int8 { return Int8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Int16.Type) throws -> Int16 { return Int16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Int32.Type) throws -> Int32 { return Int32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Int64.Type) throws -> Int64 { return Int64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: UInt.Type) throws -> UInt { return UInt.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: UInt8.Type) throws -> UInt8 { return UInt8.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: UInt16.Type) throws -> UInt16 { return UInt16.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: UInt32.Type) throws -> UInt32 { return UInt32.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: UInt64.Type) throws -> UInt64 { return UInt64.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Float.Type) throws -> Float { return Float.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: Double.Type) throws -> Double { return Double.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } + func decode(_ type: String.Type) throws -> String { return String.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) } /// Decodes a single value of the given type. /// @@ -41,7 +41,7 @@ private struct DatabaseValueDecodingContainer: SingleValueDecodingContainer { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows custom database decoding, such as decoding Date from // String, for example. - return require { try type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) as! T } + return type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo()) as! T } else { return try T(from: DatabaseValueDecoder(dbValue: dbValue, codingPath: codingPath)) } diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index 450aab87e..4f6d86cfb 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -75,7 +75,7 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer if let type = T.self as? DatabaseValueConvertible.Type { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. - return require { try type.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T? } + return type.decodeIfPresent(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T? } else if dbValue.isNull { return nil } else { @@ -114,7 +114,7 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer if let type = T.self as? DatabaseValueConvertible.Type { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. - return require { try type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T } + return type.decode(from: dbValue, debugInfo: ValueConversionDebuggingInfo(row: row, columnName: key.stringValue)) as! T } else { return try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) } @@ -227,7 +227,7 @@ private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer { if let type = T.self as? DatabaseValueConvertible.Type { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. - return require { try type.decode(from: row[column.stringValue], debugInfo: ValueConversionDebuggingInfo(row: row, columnName: column.stringValue)) as! T } + return type.decode(from: row[column.stringValue], debugInfo: ValueConversionDebuggingInfo(row: row, columnName: column.stringValue)) as! T } else { return try T(from: RowDecoder(row: row, codingPath: [column])) }