-
-
Notifications
You must be signed in to change notification settings - Fork 727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rotacloud #394
Rotacloud #394
Conversation
Hello @valexa, I admit I don't know what to do from this pull request. On one side, it is very interesting, and it opens opportunities. On the other side, I wonder if it could conflict, or not, with future development of associations, where we'd also like to support code like the following: // Future Plan for GRDB associations
struct AccountInfo: Codable {
var account: Account
var users: [User]
}
let request = Account.including(Account.users)
let infos = AccountInfos.fetchAll(db, request) // [AccountInfo] Look at the I don't want to open a door that would close this one. Your PR thus requires further analysis. The question is: who will perform this analysis? Me, another GRDB contributor, or you? I suggest you do the first round. To help you, I'll copy below the message that has been presented to you when you opened the pull request, and that you didn't read:
This PR will remain open a few days: please develop your idea! I'll answer any question, if you have any. |
@@ -79,7 +79,12 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer | |||
} else if dbValue.isNull { | |||
return nil | |||
} else { | |||
return try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) | |||
if let data = row.dataNoCopy(named: key.stringValue), let dataString = String(data: data, encoding: .utf8), JSONSerialization.isValidJSONObject([dataString]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On top of my request for further information above, I have several questions here:
- Why do you read data here, and save a string below?
- Why isn't this code also present in
decode<T>(_: forKey:)
? It looks like an oversight. Caring about your use case (decoding an optional Array) is legit, but I'd like that the library remains as consistent and welcoming as it is today. This means thinking about other people as well, including those who, for example, decode a non-optional Array. - What about the
JSONSerialization.isValidJSONObject
performance hit? Is it really useful? - I'm not sure
JSONSerialization.isValidJSONObject([dataString])
really checks what you expect it to check. To me it looks like it always return true. - That
if
prevents types from providing customized decoding of JSON data. I'd like to read an exploration of the potential harm, if any.
Hi Gwendal and thanks for the prompt response, the insights you brought to the table were just what we were hoping for, me and my colleague are not that familiar with your library to know for sure all the implications so your feedback is most welcome, we will do more work on it then submit a new pull request that should also have tests for the functionality we propose. In regards for your future plan of associations would it not be possible to have both with the default being either one and having a setting to choose if you want json or associations ? |
Thanks for your answer, @valexa, it is comforting.
After I wrote my initial message, I realized that decoding of arrays of associated records will require a very different decoding process: it is unlikely that the two features overlap in a wrong way. After all, decoding a column in a row has nothing to do with associations. Please don't worry about this topic any longer. Here are a few hints for your exploration:
|
Thank you, we tried adopting DatabaseValueConvertible but it did not work out because of swifts implicit enum initializers being private amongst other things. |
I don't know when, but I agree that this is a natural feature request. I'm currently on three topics on GRDB, which you can see in currently open PR and issues:
All maintenance tasks, as you see.
Too bad 😭
I had in mind something like |
Hi, we added what seems to be working support for nested Codable types in models eg:
public struct AccountModel : FetchableRecord, MutablePersistableRecord, Codable {
let id: Int
let name: String?
let users: [UserModel]?
}
to save as a JSON string in the database.