mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Key coding strategies transform keys into columns
They are not String transformers
This commit is contained in:
1 parent
79abf62a47
commit
987b8a48e5
4 files changed
+46
-47
No files matched your search
@@ -123,7 +123,7 @@ private class RecordEncoder<Record: EncodableRecord>: 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<Record: EncodableRecord>: 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
|
||||
|
||||
@@ -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<String.Index>] = []
|
||||
// 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)..<stringKey.endIndex
|
||||
|
||||
|
||||
// Find next uppercase character
|
||||
while let upperCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.uppercaseLetters, options: [],
|
||||
range: searchRange) {
|
||||
let untilUpperCase = wordStart..<upperCaseRange.lowerBound
|
||||
words.append(untilUpperCase)
|
||||
|
||||
|
||||
// Find next lowercase character
|
||||
searchRange = upperCaseRange.lowerBound..<searchRange.upperBound
|
||||
guard let lowerCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.lowercaseLetters, options: [],
|
||||
@@ -456,7 +455,7 @@ public enum DatabaseKeyEncodingStrategy {
|
||||
wordStart = searchRange.lowerBound
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
// Is the next lowercase letter more than 1 after the uppercase? If so, we encountered a group of
|
||||
// uppercase letters that we should treat as its own word
|
||||
let nextCharacterAfterCapital = stringKey.index(after: upperCaseRange.lowerBound)
|
||||
@@ -469,7 +468,7 @@ public enum DatabaseKeyEncodingStrategy {
|
||||
// the lower case character.
|
||||
let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
|
||||
words.append(upperCaseRange.lowerBound..<beforeLowerIndex)
|
||||
|
||||
|
||||
// Next word starts at the capital before the lowercase we just found
|
||||
wordStart = beforeLowerIndex
|
||||
}
|
||||
|
||||
@@ -99,27 +99,27 @@ private struct _RowDecoder<R: FetchableRecord>: 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<T>(_ 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<R: FetchableRecord>: Decoder {
|
||||
|
||||
func decode<T>(_ 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) {
|
||||
|
||||
@@ -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<String.Index>] = []
|
||||
// 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)..<stringKey.endIndex
|
||||
|
||||
|
||||
// Find next uppercase character
|
||||
while let upperCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.uppercaseLetters,
|
||||
options: [], range: searchRange) {
|
||||
let untilUpperCase = wordStart..<upperCaseRange.lowerBound
|
||||
words.append(untilUpperCase)
|
||||
|
||||
|
||||
// Find next lowercase character
|
||||
searchRange = upperCaseRange.lowerBound..<searchRange.upperBound
|
||||
guard let lowerCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.lowercaseLetters,
|
||||
@@ -690,7 +690,7 @@ public enum DatabaseKeyDecodingStrategy {
|
||||
wordStart = searchRange.lowerBound
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
// Is the next lowercase letter more than 1 after the uppercase? If so, we encountered a group of
|
||||
// uppercase letters that we should treat as its own word
|
||||
let nextCharacterAfterCapital = stringKey.index(after: upperCaseRange.lowerBound)
|
||||
@@ -703,7 +703,7 @@ public enum DatabaseKeyDecodingStrategy {
|
||||
// before the lower case character.
|
||||
let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
|
||||
words.append(upperCaseRange.lowerBound..<beforeLowerIndex)
|
||||
|
||||
|
||||
// Next word starts at the capital before the lowercase we just found
|
||||
wordStart = beforeLowerIndex
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user