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 / Do Not Merge] Create MusicXMLDecoder #181

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sources/MusicXML/Decoding/Decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import XMLCoder

extension MusicXML {

Expand All @@ -29,6 +28,6 @@ extension MusicXML {

/// Creates a `MusicXML` model from the given MusicXML-formatted `data`.
public init(data: Data) throws {
self.score = try XMLDecoder(trimValueWhitespaces: false).decode(Score.self, from: data)
self.score = try MusicXMLDecoder().decode(Score.self, from: data)
}
}
23 changes: 23 additions & 0 deletions Sources/MusicXML/Decoding/MusicXMLDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// MusicXMLDecoder.swift
// MusicXML
//
// Created by James Bean on 10/17/19.
//

import XMLCoder

public class MusicXMLDecoder: XMLDecoder {

// MARK: - Initializers

public init() {
super.init(trimValueWhitespaces: false)
}

// MARK: - Instance Methods

public func decode<T>(_ type: T.Type, from string: String) throws -> T where T: Decodable {
return try decode(type, from: string.data(using: .utf8)!)
}
}
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/AccidentalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AccidentalTests: XCTestCase {
let xml = """
<accidental>sharp</accidental>
"""
let decoded = try XMLDecoder().decode(Accidental.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Accidental.self, from: xml)
let expected = Accidental.sharp
XCTAssertEqual(decoded, expected)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AccordionRegistrationTests: XCTestCase {
<accordion-middle>2</accordion-middle>
</accordion-registration>
"""
let decoded = try XMLDecoder().decode(AccordionRegistration.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(AccordionRegistration.self, from: xml)
let expected = AccordionRegistration(accordionHigh: Empty(), accordionMiddle: 2)
XCTAssertEqual(decoded, expected)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/AttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AttributesTests: XCTestCase {
</clef>
</attributes>
"""
let decoded = try XMLDecoder().decode(Attributes.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Attributes.self, from: xml)
let expected = Attributes(
divisions: 1,
keys: [Key(fifths: 0, mode: .major)],
Expand Down
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/BackupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BackupTests: XCTestCase {
<duration>4</duration>
</backup>
"""
let decoded = try XMLDecoder().decode(Backup.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Backup.self, from: xml)
let expected = Backup(duration: 4)
XCTAssertEqual(decoded, expected)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/BarlineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BarlineTests: XCTestCase {
<bar-style>light-heavy</bar-style>
</barline>
"""
let decoded = try XMLDecoder().decode(Barline.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Barline.self, from: xml)
let expected = Barline(location: .right, barStyle: .lightHeavy)
XCTAssertEqual(decoded, expected)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/MusicXMLTests/Complex Types/DirectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DirectionTests: XCTestCase {
</direction>
"""

let decoded = try XMLDecoder().decode(Direction.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Direction.self, from: xml)
let expected: Direction = Direction(
[
.metronome(
Expand Down Expand Up @@ -55,7 +55,7 @@ class DirectionTests: XCTestCase {
</direction-type>
</direction>
"""
let decoded = try XMLDecoder().decode(Direction.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Direction.self, from: xml)
let expected: Direction = Direction([.wedge(Wedge(type: .crescendo, spread: 0))],
placement: .above
)
Expand All @@ -70,7 +70,7 @@ class DirectionTests: XCTestCase {
</direction-type>
</direction>
"""
let decoded = try XMLDecoder().decode(Direction.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Direction.self, from: xml)
let expected: Direction = Direction([.octaveShift(OctaveShift(type: .down, size: 8))])
XCTAssertEqual(decoded, expected)
}
Expand All @@ -83,7 +83,7 @@ class DirectionTests: XCTestCase {
</direction-type>
</direction>
"""
let decoded = try XMLDecoder().decode(Direction.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Direction.self, from: xml)
let expected: Direction = Direction(
[.bracket(Bracket(type: .start, number: 1, lineEnd: .down, lineType: .solid))],
placement: .above
Expand Down
10 changes: 5 additions & 5 deletions Tests/MusicXMLTests/Complex Types/HarmonyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HarmonyTests: XCTestCase {
<inversion>2</inversion>
</harmony>
"""
let decoded = try XMLDecoder().decode(Harmony.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Harmony.self, from: xml)
let expected: Harmony = Harmony(
chords: [
HarmonyChord(
Expand Down Expand Up @@ -68,7 +68,7 @@ class HarmonyTests: XCTestCase {
</degree>
</harmony>
"""
let decoded = try XMLDecoder().decode(Harmony.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Harmony.self, from: xml)
let expected: Harmony = Harmony(
chords: [
HarmonyChord(
Expand Down Expand Up @@ -120,7 +120,7 @@ class HarmonyTests: XCTestCase {
</degree>
</harmony>
"""
let decoded = try XMLDecoder().decode(Harmony.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Harmony.self, from: xml)
let expected: Harmony = Harmony(
printStyle: PrintStyle(position: Position(defaultY: 40)),
chords: [
Expand All @@ -147,7 +147,7 @@ class HarmonyTests: XCTestCase {
<degree-type>add</degree-type>
</degree>
"""
let decoded = try XMLDecoder().decode(Degree.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Degree.self, from: xml)
let expected: Degree = Degree(13, alter: -1, type: .add)
XCTAssertEqual(decoded, expected)
}
Expand All @@ -160,7 +160,7 @@ class HarmonyTests: XCTestCase {
</root>
</wrapper>
"""
let decoded = try XMLDecoder().decode(HarmonyChord.RootOrFunction.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(HarmonyChord.RootOrFunction.self, from: xml)
let expected: HarmonyChord.RootOrFunction = .root(Root(step: .a))
XCTAssertEqual(decoded, expected)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/MusicXMLTests/Complex Types/IdentificationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IdentificationTests: XCTestCase {
</miscellaneous>
</identification>
"""
let decoded = try XMLDecoder().decode(Identification.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Identification.self, from: xml)
let expected = Identification(
miscellaneous: Miscellaneous(
fields: [
Expand All @@ -45,7 +45,7 @@ class IdentificationTests: XCTestCase {
<source>Based on Breitkopf and Härtel edition of 1895</source>
</identification>
"""
let decoded = try XMLDecoder().decode(Identification.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Identification.self, from: xml)
let expected = Identification(
creators: [
Creator("Franz Schubert", type: "composer"),
Expand Down
12 changes: 6 additions & 6 deletions Tests/MusicXMLTests/Complex Types/KeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class KeyTests: XCTestCase {
<mode>major</mode>
</key>
"""
let decoded = try XMLDecoder().decode(Key.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Key.self, from: xml)
let expected = Key(fifths: 0, mode: .major)
XCTAssertEqual(decoded, expected)
}
Expand All @@ -27,7 +27,7 @@ class KeyTests: XCTestCase {
let xml = """
<key-octave number="1">2</key-octave>
"""
let decoded = try XMLDecoder().decode(KeyOctave.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(KeyOctave.self, from: xml)
let expected = KeyOctave(2, number: 1)
XCTAssertEqual(decoded, expected)
}
Expand All @@ -52,7 +52,7 @@ class KeyTests: XCTestCase {
<key-octave number="5">6</key-octave>
</key>
"""
let decoded = try XMLDecoder().decode(Key.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Key.self, from: xml)
let expected = Key(
kind: .nonTraditional([
Key.AlteredTone(step: .c, alter: -2),
Expand Down Expand Up @@ -93,7 +93,7 @@ class KeyTests: XCTestCase {
<key-octave number="5">6</key-octave>
</key>
"""
let decoded = try XMLDecoder().decode(Key.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Key.self, from: xml)
let expected = Key(
kind: .nonTraditional([
Key.AlteredTone(step: .c, alter: -2),
Expand Down Expand Up @@ -130,7 +130,7 @@ class KeyTests: XCTestCase {
<key-accidental>sharp</key-accidental>
</key>
"""
let decoded = try XMLDecoder().decode(Key.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Key.self, from: xml)
let expected = Key(
kind: .nonTraditional([
Key.AlteredTone(step: .b, alter: -1, accidental: .quarterFlat),
Expand Down Expand Up @@ -158,7 +158,7 @@ class KeyTests: XCTestCase {
<key-accidental>sharp</key-accidental>
</key>
"""
let decoded = try XMLDecoder().decode(Key.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Key.self, from: xml)
let expected = Key(
kind: .nonTraditional([
Key.AlteredTone(step: .b, alter: -1, accidental: .quarterFlat),
Expand Down
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/MIDIDeviceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MIDIDeviceTests: XCTestCase {
let xml = """
<midi-device id="P1-I1" port="1"></midi-device>
"""
let decoded = try XMLDecoder().decode(MIDIDevice.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(MIDIDevice.self, from: xml)
let expected = MIDIDevice(port: 1, id: "P1-I1")

XCTAssertEqual(decoded, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MIDIInstrumentTests: XCTestCase {
<pan>-45</pan>
</midi-instrument>
"""
let decoded = try XMLDecoder().decode(MIDIInstrument.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(MIDIInstrument.self, from: xml)
let expected = MIDIInstrument(id: "P1-I2", channel: 1, program: 1, volume: 50, pan: -45)

XCTAssertEqual(decoded, expected)
Expand Down
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/MeasureStyleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MeasureStyleTests: XCTestCase {
<multiple-rest>2</multiple-rest>
</measure-style>
"""
let decoded = try XMLDecoder().decode(MeasureStyle.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(MeasureStyle.self, from: xml)
let expected = MeasureStyle(kind: .multipleRest(MultipleRest(2)))
XCTAssertEqual(decoded, expected)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/MusicXMLTests/Complex Types/MetronomeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MetronomeTests: XCTestCase {
</metronome>
"""

let decoded = try XMLDecoder().decode(Metronome.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Metronome.self, from: xml)
let expected: Metronome = Metronome(
position: Position(defaultX: -25.96, relativeY: 20.00),
parentheses: false,
Expand All @@ -41,7 +41,7 @@ class MetronomeTests: XCTestCase {
</metronome>
"""

let decoded = try XMLDecoder().decode(Metronome.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Metronome.self, from: xml)
let expected: Metronome = Metronome(
parentheses: true,
kind: .regular(
Expand All @@ -65,7 +65,7 @@ class MetronomeTests: XCTestCase {
</metronome>
"""

let decoded = try XMLDecoder().decode(Metronome.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Metronome.self, from: xml)
let expected: Metronome = Metronome(
kind: .regular(
Metronome.Regular(
Expand All @@ -87,7 +87,7 @@ class MetronomeTests: XCTestCase {
</metronome>
"""

let decoded = try XMLDecoder().decode(Metronome.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Metronome.self, from: xml)
let expected: Metronome = Metronome(
parentheses: true,
kind: .regular(
Expand Down
2 changes: 1 addition & 1 deletion Tests/MusicXMLTests/Complex Types/MiscellaneousTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MiscellaneousTests: XCTestCase {
are tested at the end.</miscellaneous-field>
</miscellaneous>
"""
let decoded = try XMLDecoder().decode(Miscellaneous.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Miscellaneous.self, from: xml)
let expected = Miscellaneous(
fields: [
MiscellaneousField("""
Expand Down
4 changes: 2 additions & 2 deletions Tests/MusicXMLTests/Complex Types/MusicDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MusicDataTests: XCTestCase {
<divisions>1</divisions>
</attributes>
"""
let decoded = try XMLDecoder().decode(Attributes.self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode(Attributes.self, from: xml)
let expected = Attributes(divisions: 1)
XCTAssertEqual(decoded, expected)
}
Expand All @@ -42,7 +42,7 @@ class MusicDataTests: XCTestCase {
</attributes>
</music-data>
"""
let decoded = try XMLDecoder().decode([MusicData].self, from: xml.data(using: .utf8)!)
let decoded = try MusicXMLDecoder().decode([MusicData].self, from: xml)
let expected: [MusicData] = [
.attributes(
Attributes(
Expand Down
Loading