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

[Encoding] Implement some encode(to:) methods for some types #193

Merged
merged 12 commits into from
Oct 29, 2019
27 changes: 15 additions & 12 deletions Sources/MusicXML/Complex Types/AccordionRegistration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public struct AccordionRegistration {
// MARK: - Initializers

public init(
printStyleAlign: PrintStyleAlign = PrintStyleAlign(),
accordionHigh: Bool = false,
accordionMiddle: AccordionMiddle? = nil,
accordionLow: Bool = false
high: Bool = false,
middle: AccordionMiddle? = nil,
low: Bool = false,
printStyleAlign: PrintStyleAlign = PrintStyleAlign()
) {
self.high = high
self.middle = middle
self.low = low
self.printStyleAlign = printStyleAlign
self.high = accordionHigh
self.middle = accordionMiddle
self.low = accordionLow
}
}

Expand All @@ -46,16 +46,19 @@ extension AccordionRegistration: Codable {
case middle = "accordion-middle"
case low = "accordion-low"
}

public func encode(to encoder: Encoder) throws {
fatalError()
}

public init(from decoder: Decoder) throws {
self.printStyleAlign = try PrintStyleAlign(from: decoder)
let container = try decoder.container(keyedBy: CodingKeys.self)
self.high = container.contains(.high)
self.low = container.contains(.low)
self.middle = try container.decodeIfPresent(AccordionMiddle.self, forKey: .middle)
}
public func encode(to encoder: Encoder) throws {
var singleValueContainer = encoder.singleValueContainer()
try singleValueContainer.encode(printStyleAlign)
var container = encoder.container(keyedBy: CodingKeys.self)
if high { try container.encode(Empty(), forKey: .high) }
if low { try container.encode(Empty(), forKey: .low) }
try container.encodeIfPresent(middle, forKey: .middle)
}
}
25 changes: 23 additions & 2 deletions Sources/MusicXML/Complex Types/Ending.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct Ending {

// MARK: Attribute Groups

public let printStyle: PrintStyle?
public let printStyle: PrintStyle

// MARK: - Initializers

Expand Down Expand Up @@ -89,6 +89,27 @@ extension Ending: Codable {
}

public func encode(to encoder: Encoder) throws {
fatalError("TODO: Ending.encode(to:)")
try printStyle.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(value, forKey: .value)
try container.encode(number, forKey: .number)
try container.encode(type, forKey: .type)
try container.encodeIfPresent(printObject, forKey: .printObject)
try container.encodeIfPresent(endLength, forKey: .endLength)
try container.encodeIfPresent(textX, forKey: .textX)
try container.encodeIfPresent(textY, forKey: .textY)
}
}

import XMLCoder

extension Ending: DynamicNodeEncoding {
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.value:
return .element
default:
return .attribute
}
}
}
7 changes: 7 additions & 0 deletions Sources/MusicXML/Complex Types/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ extension Font: Codable {
case size = "font-size"
case weight = "font-weight"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(family, forKey: .family)
try container.encodeIfPresent(style, forKey: .style)
try container.encodeIfPresent(size, forKey: .size)
try container.encodeIfPresent(weight, forKey: .weight)
}
}
27 changes: 20 additions & 7 deletions Sources/MusicXML/Complex Types/Fret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ public struct Fret {
}
}

extension Fret: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self.init(value)
}
}

extension Fret: Equatable { }
extension Fret: Codable {

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

public init(from decoder: Decoder) throws {
// Decode attribute groups
self.font = try Font(from: decoder)
Expand All @@ -57,14 +61,23 @@ extension Fret: Codable {
self.value = 0
}
}

public func encode(to encoder: Encoder) throws {
fatalError("TODO: Fret.encode(to:)")
try font.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(value, forKey: .value)
try container.encodeIfPresent(color, forKey: .color)
}
}

extension Fret: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self.init(value)
import XMLCoder

extension Fret: DynamicNodeEncoding {
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.value:
return .element
default:
return .attribute
}
}
}
40 changes: 36 additions & 4 deletions Sources/MusicXML/Complex Types/Harmonic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ public struct Harmonic {
// MARK: - Attributes

public var printObject: Bool?
public var printStyle: PrintStyle?

public var placement: AboveBelow?

// MARK: - Attribute Groups

public var printStyle: PrintStyle

public init(naturalArtificial: NaturalArtificial? = nil, baseSoundingTouchingPitch: BaseSoundingTouchingPitch? = nil, printObject: Bool? = nil, printStyle: PrintStyle? = nil, placement: AboveBelow? = nil) {
public init(
naturalArtificial: NaturalArtificial? = nil,
baseSoundingTouchingPitch: BaseSoundingTouchingPitch? = nil,
printObject: Bool? = nil,
placement: AboveBelow? = nil,
printStyle: PrintStyle = PrintStyle()
) {
self.naturalArtificial = naturalArtificial
self.baseSoundingTouchingPitch = baseSoundingTouchingPitch
self.printObject = printObject
Expand Down Expand Up @@ -74,6 +84,7 @@ extension Harmonic: Codable {
}

public init(from decoder: Decoder) throws {
self.printStyle = try PrintStyle(from: decoder)
let container = try decoder.container(keyedBy: CodingKeys.self)
if container.contains(.natural) {
self.naturalArtificial = .natural
Expand All @@ -92,12 +103,33 @@ extension Harmonic: Codable {
}

self.printObject = try container.decodeIfPresent(Bool.self, forKey: .printObject)
self.printStyle = try container.decodeIfPresent(PrintStyle.self, forKey: .printStyle)
self.placement = try container.decodeIfPresent(AboveBelow.self, forKey: .placement)
}

public func encode(to encoder: Encoder) throws {
fatalError()
var singleValueContainer = encoder.singleValueContainer()
try singleValueContainer.encode(printStyle)
var container = encoder.container(keyedBy: CodingKeys.self)
if let baseSoundingTouchingPitch = self.baseSoundingTouchingPitch {
switch baseSoundingTouchingPitch {
case .base:
try container.encode(Empty(), forKey: .base)
case .sounding:
try container.encode(Empty(), forKey: .sounding)
case .touching:
try container.encode(Empty(), forKey: .touching)
}
}
if let naturalArtificial = self.naturalArtificial {
switch naturalArtificial {
case .natural:
try container.encode(Empty(), forKey: .natural)
case .artificial:
try container.encode(Empty(), forKey: .artificial)
}
}
try container.encodeIfPresent(printObject, forKey: .printObject)
try container.encodeIfPresent(placement, forKey: .placement)
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/MusicXML/Complex Types/LyricFont.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ extension LyricFont: Codable {
self.name = try container.decodeIfPresent(String.self, forKey: .name)
}
public func encode(to encoder: Encoder) throws {
fatalError("TODO: LyricFont.encode(to:)")
var singleValueContainer = encoder.singleValueContainer()
try singleValueContainer.encode(font)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(number, forKey: .number)
try container.encodeIfPresent(name, forKey: .name)
}
}
17 changes: 16 additions & 1 deletion Sources/MusicXML/Complex Types/MeasureNumbering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ extension MeasureNumbering: Codable {
self.value = try container.decode(MeasureNumberingValue.self, forKey: .value)
}
public func encode(to encoder: Encoder) throws {
fatalError()
try printStyleAlign.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(value, forKey: .value)
}
}

import XMLCoder

extension MeasureNumbering: DynamicNodeEncoding {
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.value:
return .element
default:
return .attribute
}
}
}
15 changes: 14 additions & 1 deletion Sources/MusicXML/Complex Types/PrintStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ extension PrintStyle: Codable {
private enum CodingKeys: String, CodingKey {
case color
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.position = try Position(from: decoder)
self.font = try Font(from: decoder)
self.color = try container.decodeIfPresent(Color.self, forKey: .color)
}
public func encode(to encoder: Encoder) throws {
try position.encode(to: encoder)
try font.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(color, forKey: .color)
}
}

import XMLCoder

extension PrintStyle: DynamicNodeEncoding {
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
return .attribute
}
}
4 changes: 4 additions & 0 deletions Sources/MusicXML/Complex Types/Technical.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ extension Technical: Codable {
let container = try decoder.singleValueContainer()
values = try container.decode([Technique].self)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(values)
}
}
7 changes: 7 additions & 0 deletions Sources/MusicXML/Simple Types/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public struct Color {

extension Color: Equatable { }
extension Color: Codable {
private enum CodingKeys: String, CodingKey {
case hexValue = ""
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
guard let color = Color.init(hexString: try container.decode(String.self)) else {
Expand All @@ -41,6 +44,10 @@ extension Color: Codable {
}
self = color
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(String(hexValue, radix: 16, uppercase: true))
}
}

extension String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class AccordionRegistrationTests: XCTestCase {
</accordion-registration>
"""
let decoded = try XMLDecoder().decode(AccordionRegistration.self, from: xml.data(using: .utf8)!)
let expected = AccordionRegistration(accordionHigh: true, accordionMiddle: 2)
let expected = AccordionRegistration(high: true, middle: 2)
XCTAssertEqual(decoded, expected)
}

func testRoundTrip() throws {
try testRoundTrip(AccordionRegistration(high: true, middle: 2))
}
}
22 changes: 22 additions & 0 deletions Tests/MusicXMLTests/Complex Types/EndingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// EndingTests.swift
// MusicXMLTests
//
// Created by James Bean on 10/24/19.
//

import XCTest
import MusicXML

class EndingTests: XCTestCase {

func testRoundTrip() throws {
let ending = Ending("1.",
number: "1",
type: .start,
printObject: false,
printStyle: PrintStyle(position: Position(defaultX: 42))
)
try testRoundTrip(ending)
}
}
20 changes: 20 additions & 0 deletions Tests/MusicXMLTests/Complex Types/FretTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// FretTests.swift
// MusicXMLTests
//
// Created by James Bean on 10/24/19.
//

import XCTest
import MusicXML

class FretTests: XCTestCase {

func testRoundTrip() throws {
let fret = Fret(3,
color: Color(hexValue: 0xFFFFFF),
font: Font(style: .italic, weight: .bold)
)
try testRoundTrip(fret, loggingEncoded: true)
}
}
26 changes: 26 additions & 0 deletions Tests/MusicXMLTests/Complex Types/HarmonicTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// HarmonicTests.swift
// MusicXMLTests
//
// Created by James Bean on 10/24/19.
//

import XCTest
import MusicXML

class HarmonicTests: XCTestCase {

func testRoundTrip() throws {
let harmonic = Harmonic(
naturalArtificial: .natural,
baseSoundingTouchingPitch: .base,
printObject: true,
placement: .above,
printStyle: PrintStyle(
position: Position(relativeY: -30),
color: Color(hexValue: 0x000000)
)
)
try testRoundTrip(harmonic)
}
}
Loading