Integer keys#29
Open
michelf wants to merge 1 commit into
Open
Conversation
…aries when the CodingKey implementation provides an intValue.
hirotakan
requested changes
Feb 22, 2020
Comment on lines
163
to
171
| func contains(_ key: Key) -> Bool { | ||
| return container[key.stringValue] != nil | ||
| return container[decoder.convertToPackKey(from: key)] != nil | ||
| } | ||
|
|
||
| func findEntry(by key: CodingKey) throws -> Data { | ||
| guard let entry = self.container[key.stringValue] else { | ||
| guard let entry = self.container[decoder.convertToPackKey(from: key)] else { | ||
| let context = DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\").") | ||
| throw DecodingError.keyNotFound(key, context) | ||
| } |
Owner
There was a problem hiding this comment.
If you change as follows, I think that Decoder does not need allowIntKeys.
func contains(_ key: Key) -> Bool {
if let intValue = key.intValue, let _ = container[.int(intValue)] {
return true
}
return container[.string(key.stringValue)] != nil
}
func findEntry(by key: CodingKey) throws -> Data {
if let intValue = key.intValue, let entry = container[.int(intValue)] {
return entry
}
if let entry = self.container[.string(key.stringValue)] {
return entry
}
let context = DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\").")
throw DecodingError.keyNotFound(key, context)
}
| fileprivate var storage = MessagePackStorage() | ||
|
|
||
| public init() {} | ||
| public init(allowIntKeys: Bool = false) { |
Owner
There was a problem hiding this comment.
Thanks for a good idea!
But I want to change to:
- Add KeyEncodingType.
public enum KeyEncodingType {
case string
case int
}
- Change the initializer.
private let keyEncodingType: KeyEncodingType
public init(keyEncodingType: KeyEncodingType = .string) {
self.keyEncodingType = keyEncodingType
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This code adds an encoding and decoding mode where keys in the message pack data are allowed to be integer. If the provided key returns a non-
nilintValueandallowIntKeysis set totruein the encoder/decoder, the integer key will be used instead of its string form.