Skip to content
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

Replace value intrinsic with empty string key #149

Merged
merged 3 commits into from Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift
Expand Up @@ -24,7 +24,7 @@ struct KeyedBox {
}

var value: SimpleBox? {
return elements["value"].first as? SimpleBox
return elements[""].first as? SimpleBox
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Expand Up @@ -61,7 +61,7 @@ struct XMLCoderElement: Equatable {
// Handle attributed unkeyed value <foo attr="bar">zap</foo>
// Value should be zap. Detect only when no other elements exist
if elements.isEmpty, let value = value {
elements.append(StringBox(value), at: "value")
elements.append(StringBox(value), at: "")
}
return KeyedBox(elements: elements, attributes: attributes)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift
Expand Up @@ -97,7 +97,7 @@ class XMLDecoderImplementation: Decoder {
return KeyedDecodingContainer(XMLKeyedDecodingContainer<Key>(
referencing: self,
wrapping: SharedBox(KeyedBox(
elements: KeyedStorage([("value", string)]),
elements: KeyedStorage([("", string)]),
attributes: KeyedStorage()
))
))
Expand Down
26 changes: 4 additions & 22 deletions Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift
Expand Up @@ -232,29 +232,11 @@ extension XMLKeyedDecodingContainer {

let elements = container
.withShared { keyedBox -> [KeyedBox.Element] in
if ["value", ""].contains(key.stringValue) {
let keyString = key.stringValue.isEmpty ? "value" : key.stringValue
let value = keyedBox.elements[keyString]
if !value.isEmpty {
return value.map {
if let singleKeyed = $0 as? SingleKeyedBox {
return singleKeyed.element
} else {
return $0
}
}
} else if let value = keyedBox.value {
return [value]
keyedBox.elements[key.stringValue].map {
if let singleKeyed = $0 as? SingleKeyedBox {
return singleKeyed.element
} else {
return []
}
} else {
return keyedBox.elements[key.stringValue].map {
if let singleKeyed = $0 as? SingleKeyedBox {
return singleKeyed.element
} else {
return $0
}
return $0
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions Tests/XMLCoderTests/AttributedIntrinsicTest.swift
Expand Up @@ -82,7 +82,7 @@ private struct Foo: Codable, DynamicNodeEncoding, Equatable {

enum CodingKeys: String, CodingKey {
case id
case value
case value = ""
}

static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
Expand All @@ -97,6 +97,10 @@ private struct Foo: Codable, DynamicNodeEncoding, Equatable {

private struct FooValue: Codable, Equatable {
let value: Int

enum CodingKeys: String, CodingKey {
case value = ""
}
}

private struct FooOptional: Codable, DynamicNodeEncoding, Equatable {
Expand All @@ -105,7 +109,7 @@ private struct FooOptional: Codable, DynamicNodeEncoding, Equatable {

enum CodingKeys: String, CodingKey {
case id
case value
case value = ""
}

static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
Expand Down Expand Up @@ -167,7 +171,7 @@ private struct PreviewImageTime: Codable, Equatable, DynamicNodeEncoding {

enum CodingKeys: String, CodingKey {
case format
case value
case value = ""
}

static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
Expand Down