diff --git a/GRDB/Record/EncodableRecord+Encodable.swift b/GRDB/Record/EncodableRecord+Encodable.swift index 9c844eaa9..7b55fc418 100644 --- a/GRDB/Record/EncodableRecord+Encodable.swift +++ b/GRDB/Record/EncodableRecord+Encodable.swift @@ -123,7 +123,7 @@ private class RecordEncoder: Encoder { /// Helper methods @inline(__always) fileprivate func persist(_ value: DatabaseValueConvertible?, forKey key: CodingKey) { - _persistenceContainer[keyEncodingStrategy.convertedKey(key: key.stringValue)] = value + _persistenceContainer[keyEncodingStrategy.column(forKey: key)] = value } @inline(__always) @@ -150,9 +150,9 @@ private class RecordEncoder: Encoder { } catch is JSONRequiredError { // Encode to JSON try autoreleasepool { - + let jsonData = try Record.databaseJSONEncoder(for: key.stringValue).encode(value) - + // Store JSON String in the database for easier debugging and // database inspection. Thanks to SQLite weak typing, we won't // have any trouble decoding this string into data when we diff --git a/GRDB/Record/EncodableRecord.swift b/GRDB/Record/EncodableRecord.swift index 41e388a07..62ec57a01 100644 --- a/GRDB/Record/EncodableRecord.swift +++ b/GRDB/Record/EncodableRecord.swift @@ -117,7 +117,7 @@ public protocol EncodableRecord { /// var uuid: UUID /// } static var databaseUUIDEncodingStrategy: DatabaseUUIDEncodingStrategy { get } - + /// When the EncodableRecord type also adopts the standard Encodable /// protocol, this property controls the key encoding strategy. /// @@ -159,7 +159,7 @@ extension EncodableRecord { public static var databaseUUIDEncodingStrategy: DatabaseUUIDEncodingStrategy { .deferredToUUID } - + public static var databaseKeyEncodingStrategy: DatabaseKeyEncodingStrategy { .useDefaultKeys } @@ -412,25 +412,24 @@ public enum DatabaseUUIDEncodingStrategy { /// // encoded as player_id /// var playerID: String /// } - public enum DatabaseKeyEncodingStrategy { case useDefaultKeys case convertToSnakeCase - - func convertedKey(key: String) -> String { + + func column(forKey key: CodingKey) -> String { switch self { case .useDefaultKeys: - return key + return key.stringValue case .convertToSnakeCase: - return Self.convertToSnakeCase(key) + return Self.convertToSnakeCase(key.stringValue) } } - + /// This function is taken directly from Apple's open source JSONEncoder. /// https://github.com/apple/swift-corelibs-foundation/blob/eec4b26deee34edb7664ddd9c1222492a399d122/Sources/Foundation/JSONEncoder.swift#L127 private static func convertToSnakeCase(_ stringKey: String) -> String { guard !stringKey.isEmpty else { return stringKey } - + var words: [Range] = [] // The general idea of this algorithm is to split words on transition from lower to upper case, then on // transition of >1 upper case characters to lowercase @@ -441,13 +440,13 @@ public enum DatabaseKeyEncodingStrategy { // We assume, per Swift naming conventions, that the first character of the key is lowercase. var wordStart = stringKey.startIndex var searchRange = stringKey.index(after: wordStart)..: Decoder { // swiftlint:disable comma // swiftlint:disable line_length - func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Int.Type, forKey key: Key) throws -> Int { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Float.Type, forKey key: Key) throws -> Float { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: Double.Type, forKey key: Key) throws -> Double { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } - func decode(_ type: String.Type, forKey key: Key) throws -> String { try decoder.row.decode(forKey: convertedKey(key.stringValue)) } + func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Int.Type, forKey key: Key) throws -> Int { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Float.Type, forKey key: Key) throws -> Float { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: Double.Type, forKey key: Key) throws -> Double { try decoder.row.decode(forKey: column(forKey: key)) } + func decode(_ type: String.Type, forKey key: Key) throws -> String { try decoder.row.decode(forKey: column(forKey: key)) } // swiftlint:enable line_length // swiftlint:enable comma - - private func convertedKey(_ key: String) -> String { - decoder.keyDecodingStrategy.convertedKey(key: key) + + private func column(forKey key: Key) -> String { + decoder.keyDecodingStrategy.column(forKey: key) } - + func decodeIfPresent(_ type: T.Type, forKey key: Key) throws -> T? where T: Decodable { let row = decoder.row let keyName = key.stringValue @@ -154,7 +154,7 @@ private struct _RowDecoder: Decoder { func decode(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable { let row = decoder.row - let keyName = convertedKey(key.stringValue) + let keyName = column(forKey: key) // Column? if let index = row.index(forColumn: keyName) { diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index 90dc7aa8b..94d669f90 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -103,7 +103,7 @@ public protocol FetchableRecord { /// var registrationDate: Date // decoded from epoch timestamp /// } static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { get } - + /// When the FetchableRecord type also adopts the standard Decodable /// protocol, this property controls the key decoding strategy. /// @@ -141,7 +141,7 @@ extension FetchableRecord { public static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { .deferredToDate } - + public static var databaseKeyDecodingStrategy: DatabaseKeyDecodingStrategy { .useDefaultKeys } @@ -650,21 +650,21 @@ public enum DatabaseKeyDecodingStrategy { case useDefaultKeys /// Converts the key from snake case format, e.g. player_id to playerID case convertFromSnakeCase - - func convertedKey(key: String) -> String { + + func column(forKey key: CodingKey) -> String { switch self { case .useDefaultKeys: - return key + return key.stringValue case .convertFromSnakeCase: - return Self.convertFromSnakeCase(key) + return Self.convertFromSnakeCase(key.stringValue) } } - + /// This function is taken directly from Apple's open source JSONDecoder. /// https://github.com/apple/swift-corelibs-foundation/blob/eec4b26deee34edb7664ddd9c1222492a399d122/Sources/Foundation/JSONDecoder.swift#L103 private static func convertFromSnakeCase(_ stringKey: String) -> String { guard !stringKey.isEmpty else { return stringKey } - + var words: [Range] = [] // The general idea of this algorithm is to split words on transition from lower to upper case, then // on transition of >1 upper case characters to lowercase @@ -675,13 +675,13 @@ public enum DatabaseKeyDecodingStrategy { // We assume, per Swift naming conventions, that the first character of the key is lowercase. var wordStart = stringKey.startIndex var searchRange = stringKey.index(after: wordStart)..