-
Notifications
You must be signed in to change notification settings - Fork 113
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
Can’t reach an XML value #12
Comments
Unifies the representation of `T` where `T: Box` and makes it explicit where shared reference semantics is expected. With this code performance is consistently 5-30% worse, could this be caused by copies of value types? Can this be improved when Swift 5.0 is released or whatever the version is that allows us to improve performance of value types? After reviewing related issues we'd prefer XMLCoder to be slightly slower, but with #12, #17 and #25 fixed than the opposite 😞 * Added `SharedBox` type for on-demand reference semantics * Remove `print` that kills performance tests * Handle all cases with SharedBox containers * Add RJITest back to the project file, fix the test * Remove unused internal initializer
Add intrinsic encoding decoding with attributes support
Add intrinsic encoding decoding with attributes support
Add intrinsic encoding decoding with attributes support
Add intrinsic encoding decoding with attributes support
## Overview This PR fixes #12: decoding of unkeyed single value elements that contain attributes as reported in that issue. ## Example ```xml <?xml version="1.0" encoding="UTF-8"?> <foo id="123">456</foo> ``` ```swift private struct Foo: Codable, DynamicNodeEncoding { let id: String let value: String enum CodingKeys: String, CodingKey { case id case value // case value = "" would also work } static func nodeEncoding(forKey key: CodingKey) -> XMLEncoder.NodeEncoding { switch key { case CodingKeys.id: return .attribute default: return .element } } } ``` Previously this XML example would fail to decode. This PR allows two different methods of decoding discussed in usage. ## Usage This PR will support decoding the example XML in two cases as long as the prerequisite cases are matched. ### Prerequisites 1. No keyed child elements exist 2. Any keyed child nodes are supported Attribute types and are indicated to decode as such ### Supported cases 1. An instance var with the key `value` of any decodable type. 2. An instance var of any key that has a Decoding key of String value "value" or "". The decoder will look for the case where an element was keyed with either "value" or "", but not both, and only one of those values (ie; no other keyed elements). It will automatically find the correct value based on the CodingKey supplied. ## Other considerations The choice to decode as either "value" or "" keys was purely to try to support the inverse to XML version which would only work if an instance var specifically has a `CodingKey` with associated value type `String` that returns an empty string, if PR #70 is commited as-is, which adds XML coding support for unkeyed attributed value elements. The 'value' variant was added as a simpler means to support decoding a nested unkeyed element without having to provide custom CodingKey enum for a struct. Something needed to be provided since Swift doesn't have empty string iVars `let "" : String`, isn't a valid iVar token for example, so `value` was chosen as a logical default. ## Notes This PR is an extension of #70 , though it could be recoded to work off of `master`. The last commit in this PR is the only commit specific to this feature, though #70 provides the inverse solution of creating XML from an attributed value wrapping struct. Coding and decoding unit tests of String and Int values are included.
This is now supported in |
Trying to use master with the following and still getting an error <preview_image_time format="24/999 1000/nonDrop">00:00:17:01</preview_image_time> struct PreviewImageTime: Codable, DynamicNodeEncoding {
var format: String
var value: String
enum CodingKeys: String, CodingKey {
case format
case value = ""
}
static func nodeEncoding(forKey key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.format:
return .attribute
default:
return .element
}
}
} could not parse |
@thecb4 What happens if you change your string values to something without colons or backslashes. I think I ran into something similar with UUID strings that had colons. I believe the decoder is attempting to decode a dictionary before a string, based on the ability to convert "k : v " strings to dictionaries. If that's a case, a workaround may be to provide your own decoder that specifies the type to try. I would just try a simple test case first with simple strings to see if that is indeed the issue. I have some example code that might help if that is it. |
Now I understand the error message! Perfect. |
hm, I don't think the attempt to decode that string as a dictionary is something anyone expects to be a default behavior. @thecb4 I hope you don't mind if I add that XML snippet to XMLCoder test suite |
Thank you. I have been trying to figure out the appropriate work around. I tried a custom init and was still getting the same error.
|
FYI - When I create a standalone XML element, it works. When it's embedded in another element. it fails.
structs
|
Thank you @thecb4, that's very helpful! Clearly looks like a bug to me, looking into it right now. |
Hi there! I can’t find the right data structure to decode the following XML string:
I ran out of ideas. May anyone give me a hint?
Thanks!
The text was updated successfully, but these errors were encountered: