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
[WIP] Enum usage #112
[WIP] Enum usage #112
Conversation
let decodedStringArray = try decoder.decode(IntOrStringArray.self, from: stringXML) | ||
print(decodedStringArray) | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
let arrayXMLString = String(data: encodedArray, encoding: .utf8) | ||
|
||
print(arrayXMLString) | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
let encodedArray = try encoder.encode(array, withRootKey: "container", header: header) | ||
|
||
let arrayXMLString = String(data: encodedArray, encoding: .utf8) | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
|
||
let header = XMLHeader(version: 1.0, encoding: "UTF-8") | ||
let encodedArray = try encoder.encode(array, withRootKey: "container", header: header) | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
|
||
func testNonExplicitArray() throws { | ||
let encoder = XMLEncoder() | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
|
||
func testEncodeExplicitArray() throws { | ||
let encoder = XMLEncoder() | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
XCTAssertEqual(string, decodedString) | ||
XCTAssertEqual(int, decodedInt) | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
XCTAssertEqual(encodedStringXML, originalStringXML) | ||
XCTAssertEqual(encodedIntXML, originalIntXML) | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
func testEncode() throws { | ||
let encoder = XMLEncoder() | ||
encoder.outputFormatting = [] | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
private let string = IntOrStringContaining(element: IntOrString.string("forty-two")) | ||
private let int = IntOrStringContaining(element: IntOrString.int(42)) | ||
private let array = IntOrStringArray(element: [.string("forty-two"), .int(42)]) | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
try container.encode(value, forKey: .int) | ||
} | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
)) | ||
} | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
case string | ||
case int | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
public init(from decoder: Decoder) throws { | ||
element = try IntOrString(from: decoder) | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
public func encode(to encoder: Encoder) throws { | ||
try element.encode(to: encoder) | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
|
||
private struct IntOrStringContaining: Equatable, Codable { | ||
let element: IntOrString | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
|
||
private struct IntOrStringArray: Equatable, Codable { | ||
let element: [IntOrString] | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
final class IntOrStringTest: XCTestCase { | ||
private let string = IntOrStringContaining(element: IntOrString.string("forty-two")) | ||
private let int = IntOrStringContaining(element: IntOrString.int(42)) | ||
private let array = IntOrStringArray(element: [ .int(42), .string("forty-two"),]) |
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.
Comma Spacing Violation: There should be no space before and one after any comma. (comma)
|
||
// does not work for <int></int><string></string><int></int> sequence etc | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
let ints = try container.decode([Int].self, forKey: .int) | ||
let strings = try container.decode([String].self, forKey: .string) | ||
element = ints.map { IntOrString.int($0) } + strings.map { IntOrString.string($0) } | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
public init(element: [IntOrString]) { | ||
self.element = element | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
case int | ||
case string | ||
} | ||
|
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.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
(work-in-progress) Attempts to fix #25 !
A couple of attempts at implementing arrays of enums with associated values.
Adds passing tests that implement the encoding and decoding of the more explicit...
The goal is to be able to encode / decode the following less explicit...
The following abuse of the
value = ""
pattern seems to me to have some promise...... and, with a minor non-breaking change to the
XMLCoderElement
source, works nicely for encoding. @MaxDesiatov, do you think there is any way it could be used with decoding too?