-
Notifications
You must be signed in to change notification settings - Fork 121
Decode meta_data incorrectly sent as an object
#16141
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
Changes from all commits
060141d
c574960
5746bc6
c66e524
2d989a2
fd8e12e
7696d51
d26c2be
177b484
fe6f10c
465ed5c
3d57295
ae1c3df
501b660
afe445f
fa134fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||
| import Foundation | ||||||||||
|
|
||||||||||
| /// Extension to support flexible decoding of MetaData arrays | ||||||||||
| /// Handles both standard array format and object format keyed by index strings | ||||||||||
| extension Array where Element == MetaData { | ||||||||||
|
|
||||||||||
| /// Custom decoding from container that supports both array and dictionary formats | ||||||||||
| public static func decodeFlexibly<K>(from container: KeyedDecodingContainer<K>, | ||||||||||
| forKey key: KeyedDecodingContainer<K>.Key) -> [MetaData] { | ||||||||||
| // Try to decode as array first (standard format) | ||||||||||
| if let metaDataArray = try? container.decode([MetaData].self, forKey: key) { | ||||||||||
| return metaDataArray | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Try to decode as object keyed by index strings | ||||||||||
| if let metaDataDict = try? container.decode([String: MetaData].self, forKey: key) { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was checking if a plugin could produce
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I'm wary of even doing this much workaround for plugins messing with fields that don't belong to them, to be honest... it seems like a short road to insanity trying to pair that with our type safe models. It'll continue to fail gracefully even if the format is more broken than this. |
||||||||||
| return Array(metaDataDict.values) | ||||||||||
|
||||||||||
| return Array(metaDataDict.values) | |
| return metaDataDict.sorted { (lhs, rhs) in | |
| (Int(lhs.key) ?? 0) < (Int(rhs.key) ?? 0) | |
| }.map { $0.value } |
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.
I wonder if it's true, the docs suggest:
When iterated over, values appear in this collection in the same order as they occur in the dictionary’s key-value pairs.
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.
Maybe it would be worth logging a case where metadata decoding fails silently? It could help troubleshoot similar issues more easily.
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.
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.
[nitpick] The
reduce(into:)method is more efficient than the previousreduce()approach since it mutates the accumulator in-place rather than creating new dictionaries on each iteration.