From 5f23d9790317f7459dfa9b973a17ee2e3ec2e4b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 28 Aug 2021 15:50:34 +0200 Subject: [PATCH] Add the custom strategies --- GRDB/Record/EncodableRecord.swift | 5 +++++ GRDB/Record/FetchableRecord.swift | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/GRDB/Record/EncodableRecord.swift b/GRDB/Record/EncodableRecord.swift index 1c224f982..bf1f22b0e 100644 --- a/GRDB/Record/EncodableRecord.swift +++ b/GRDB/Record/EncodableRecord.swift @@ -420,12 +420,17 @@ public enum DatabaseKeyEncodingStrategy { /// A key encoding strategy that converts camel-case keys to snake-case keys. case convertToSnakeCase + /// A key encoding strategy defined by the closure you supply. + case custom((CodingKey) -> String) + func column(forKey key: CodingKey) -> String { switch self { case .useDefaultKeys: return key.stringValue case .convertToSnakeCase: return Self.convertToSnakeCase(key.stringValue) + case let .custom(column): + return column(key) } } diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index 4fa5eaf8a..4f15b53c7 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -654,12 +654,17 @@ public enum DatabaseKeyDecodingStrategy { /// A key decoding strategy that converts snake-case keys to camel-case keys. case convertFromSnakeCase + /// A key decoding strategy defined by the closure you supply. + case custom((CodingKey) -> String) + func column(forKey key: CodingKey) -> String { switch self { case .useDefaultKeys: return key.stringValue case .convertFromSnakeCase: return Self.convertFromSnakeCase(key.stringValue) + case let .custom(column): + return column(key) } }