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

[WIP] Enum usage #112

Closed
wants to merge 14 commits into from
Closed

Conversation

bwetherfield
Copy link
Collaborator

(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...

"""
<?xml version="1.0" encoding="UTF-8"?>
<container><intOrString><int>42</int></intOrString><intOrString><string>forty-two</string></intOrString></container>
"""

The goal is to be able to encode / decode the following less explicit...

"""
<?xml version="1.0" encoding="UTF-8"?>
<container><int>42</int><string>forty-two</string></container>
"""

The following abuse of the value = "" pattern seems to me to have some promise...

private struct IntOrStringArray: Equatable, Codable {
    let element: [IntOrString]
    
    enum CodingKeys: String, CodingKey {
        case element = ""
    }
}

... 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?

let decodedStringArray = try decoder.decode(IntOrStringArray.self, from: stringXML)
print(decodedStringArray)
}

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)

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)

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)

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()

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()

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)
}

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)
}

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 = []

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)])

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)
}
}

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)

))
}
}

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
}

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)
}

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)
}

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

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]

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"),])

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
}

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) }

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
}

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
}

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array of enums with associated values
2 participants