Skip to content

Commit bd65d61

Browse files
authored
Implement DynamicNodeEncoding (#232)
* Add script for sourcery tags * Amend script for DynamicNodeEncoding * Rename script * Add script to add sourcery attribute annotations * Add script to remove sourcery attribute annotations * Add XMLAttributeGroupCodingKey tag * Add DynamicNodeEncoding template * Remove sourcery tags * Update DynamicNodeEncoding implementation * Run formatting * Modify sourcery tags script * Disable auto initializer * Fix script * Update sourcery config * Implement DynamicNodeEncoding, prepare removal of tags * Remove sourcery tags * Fix formatting * Add tags for attribute groups * Collapse attributes to single line * Remove attribute group check where not needed * Clean up * Fix failing tests
1 parent 05d2ce2 commit bd65d61

File tree

145 files changed

+1705
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+1705
-24
lines changed

.sourcery.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sources:
2+
- "Sources/MusicXML/Complex\ Types"
3+
templates:
4+
- "Sources/MusicXML/Templates"
5+
output: "sourcery-output"

Sources/MusicXML/Complex Types/Accord.swift

+12
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ extension Accord: Codable {
4646
case tuningOctave = "tuning-octave"
4747
}
4848
}
49+
50+
import XMLCoder
51+
extension Accord: DynamicNodeEncoding {
52+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
53+
switch key {
54+
case CodingKeys.string:
55+
return .attribute
56+
default:
57+
return .element
58+
}
59+
}
60+
}

Sources/MusicXML/Complex Types/AccordionRegistration.swift

+10
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,13 @@ extension AccordionRegistration: Codable {
6868
try container.encodeIfPresent(middle, forKey: .middle)
6969
}
7070
}
71+
72+
import XMLCoder
73+
extension AccordionRegistration: DynamicNodeEncoding {
74+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
75+
if key is XMLAttributeGroupCodingKey {
76+
return .attribute
77+
}
78+
return .element
79+
}
80+
}

Sources/MusicXML/Complex Types/Appearance.swift

+7
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ extension Appearance: Codable {
4545
case otherAppearances = "other-appearance"
4646
}
4747
}
48+
49+
import XMLCoder
50+
extension Appearance: DynamicNodeEncoding {
51+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
52+
return .element
53+
}
54+
}

Sources/MusicXML/Complex Types/Arpeggiate.swift

+15
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ extension Arpeggiate: Codable {
6464
color = try container.decodeIfPresent(Color.self, forKey: .color)
6565
}
6666
}
67+
68+
import XMLCoder
69+
extension Arpeggiate: DynamicNodeEncoding {
70+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
71+
if key is XMLAttributeGroupCodingKey {
72+
return .attribute
73+
}
74+
switch key {
75+
case CodingKeys.number, CodingKeys.direction, CodingKeys.placement, CodingKeys.color:
76+
return .attribute
77+
default:
78+
return .element
79+
}
80+
}
81+
}

Sources/MusicXML/Complex Types/Arrow.swift

+14
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,17 @@ extension Arrow: Codable {
149149
try container.encodeIfPresent(placement, forKey: .placement)
150150
}
151151
}
152+
153+
extension Arrow: DynamicNodeEncoding {
154+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
155+
if key is XMLAttributeGroupCodingKey {
156+
return .attribute
157+
}
158+
switch key {
159+
case CodingKeys.placement:
160+
return .attribute
161+
default:
162+
return .element
163+
}
164+
}
165+
}

Sources/MusicXML/Complex Types/Articulations.swift

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ extension Articulations: Codable {
3030
self.values = try container.decode([Articulation].self)
3131
}
3232
}
33+
34+
import XMLCoder
35+
extension Articulations: DynamicNodeEncoding {
36+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
37+
return .element
38+
}
39+
}

Sources/MusicXML/Complex Types/Attributes.swift

+7
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,10 @@ extension Attributes: Codable {
9191
case measureStyles = "measure-style"
9292
}
9393
}
94+
95+
import XMLCoder
96+
extension Attributes: DynamicNodeEncoding {
97+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
98+
return .element
99+
}
100+
}

Sources/MusicXML/Complex Types/Barline.swift

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
public struct Barline {
3030
// MARK: - Instance Properties
3131

32+
// MARK: Attributes
33+
3234
/// Barlines have a location attribute to make it easier to process barlines independently of
3335
/// the other musical data in a score. It is often easier to set up measures separately from
3436
/// entering notes. The location attribute must match where the barline element occurs within
@@ -50,6 +52,8 @@ public struct Barline {
5052
/// playback when a barline element contains a divisions element.
5153
public var divisions: Int?
5254

55+
// MARK: Elements
56+
5357
/// The bar-style type represents barline style information. Choices are regular, dotted,
5458
/// dashed, heavy, light-light, light-heavy, heavy-light, heavy-heavy, tick (a short stroke
5559
/// through the top line), short (a partial barline between the 2nd and 4th lines), and none.
@@ -106,3 +110,15 @@ extension Barline: Codable {
106110
case divisions
107111
}
108112
}
113+
114+
import XMLCoder
115+
extension Barline: DynamicNodeEncoding {
116+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
117+
switch key {
118+
case CodingKeys.location, CodingKeys.segno, CodingKeys.coda, CodingKeys.divisions:
119+
return .attribute
120+
default:
121+
return .element
122+
}
123+
}
124+
}

Sources/MusicXML/Complex Types/Barre.swift

+12
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ public struct Barre {
2626

2727
extension Barre: Equatable {}
2828
extension Barre: Codable {}
29+
30+
import XMLCoder
31+
extension Barre: DynamicNodeEncoding {
32+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
33+
switch key {
34+
case CodingKeys.type, CodingKeys.color:
35+
return .attribute
36+
default:
37+
return .element
38+
}
39+
}
40+
}

Sources/MusicXML/Complex Types/Bass.swift

+7
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ extension Bass: Codable {
3232
case alter = "bass-alter"
3333
}
3434
}
35+
36+
import XMLCoder
37+
extension Bass: DynamicNodeEncoding {
38+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
39+
return .element
40+
}
41+
}

Sources/MusicXML/Complex Types/BeatRepeat.swift

+12
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ public struct BeatRepeat {
3232

3333
extension BeatRepeat: Equatable {}
3434
extension BeatRepeat: Codable {}
35+
36+
import XMLCoder
37+
extension BeatRepeat: DynamicNodeEncoding {
38+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
39+
switch key {
40+
case CodingKeys.type, CodingKeys.slashes, CodingKeys.useDots:
41+
return .attribute
42+
default:
43+
return .element
44+
}
45+
}
46+
}

Sources/MusicXML/Complex Types/Bend.swift

+15
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,18 @@ extension Bend: Codable {
9999
try container.encodeIfPresent(withBar, forKey: .withBar)
100100
}
101101
}
102+
103+
import XMLCoder
104+
extension Bend: DynamicNodeEncoding {
105+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
106+
if key is XMLAttributeGroupCodingKey {
107+
return .attribute
108+
}
109+
switch key {
110+
case CodingKeys.accelerate, CodingKeys.beats, CodingKeys.firstBeat, CodingKeys.lastBeat:
111+
return .attribute
112+
default:
113+
return .element
114+
}
115+
}
116+
}

Sources/MusicXML/Complex Types/BendSound.swift

+15-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ extension BendSound: Equatable {}
4141
extension BendSound: Codable {
4242
// MARK: - Codable
4343

44-
private enum CodingKeys: String, CodingKey {
44+
internal enum CodingKeys: String, CodingKey {
4545
case accelerate
4646
case beats
4747
case firstBeat = "first-beat"
4848
case lastBeat = "last-beat"
4949
}
5050
}
51+
52+
extension BendSound.CodingKeys: XMLAttributeGroupCodingKey {}
53+
54+
import XMLCoder
55+
extension BendSound: DynamicNodeEncoding {
56+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
57+
switch key {
58+
case CodingKeys.accelerate, CodingKeys.beats, CodingKeys.firstBeat, CodingKeys.lastBeat:
59+
return .attribute
60+
default:
61+
return .element
62+
}
63+
}
64+
}

Sources/MusicXML/Complex Types/Bezier.swift

+38-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,41 @@ public struct Bezier {
4242
}
4343

4444
extension Bezier: Equatable {}
45-
extension Bezier: Codable {}
45+
extension Bezier: Codable {
46+
// MARK: - Codable
47+
48+
internal enum CodingKeys: String, CodingKey {
49+
case bezierX = "bezier-x"
50+
case bezierY = "bezier-y"
51+
case bezierX2 = "bezier-x2"
52+
case bezierY2 = "bezier-y2"
53+
case bezierOffset = "bezier-offset"
54+
case bezierOffset2 = "bezier-offset2"
55+
}
56+
57+
// MARK: Decodable
58+
59+
public init(from decoder: Decoder) throws {
60+
let container = try decoder.container(keyedBy: CodingKeys.self)
61+
self.bezierX = try container.decodeIfPresent(Tenths.self, forKey: .bezierX)
62+
self.bezierY = try container.decodeIfPresent(Tenths.self, forKey: .bezierY)
63+
self.bezierX2 = try container.decodeIfPresent(Tenths.self, forKey: .bezierX2)
64+
self.bezierY2 = try container.decodeIfPresent(Tenths.self, forKey: .bezierY2)
65+
self.bezierOffset = try container.decodeIfPresent(Int.self, forKey: .bezierOffset)
66+
self.bezierOffset2 = try container.decodeIfPresent(Int.self, forKey: .bezierOffset2)
67+
}
68+
}
69+
70+
extension Bezier.CodingKeys: XMLAttributeGroupCodingKey {}
71+
72+
import XMLCoder
73+
extension Bezier: DynamicNodeEncoding {
74+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
75+
switch key {
76+
case CodingKeys.bezierX, CodingKeys.bezierY, CodingKeys.bezierX2, CodingKeys.bezierY2, CodingKeys.bezierOffset, CodingKeys.bezierOffset2:
77+
return .attribute
78+
default:
79+
return .element
80+
}
81+
}
82+
}

Sources/MusicXML/Complex Types/Bookmark.swift

+12
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ public struct Bookmark {
2828

2929
extension Bookmark: Equatable {}
3030
extension Bookmark: Codable {}
31+
32+
import XMLCoder
33+
extension Bookmark: DynamicNodeEncoding {
34+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
35+
switch key {
36+
case CodingKeys.id, CodingKeys.name, CodingKeys.element, CodingKeys.position:
37+
return .attribute
38+
default:
39+
return .element
40+
}
41+
}
42+
}

Sources/MusicXML/Complex Types/Bracket.swift

+17
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,20 @@ extension Bracket: Codable {
7171
self.color = try container.decodeIfPresent(Color.self, forKey: .color)
7272
}
7373
}
74+
75+
import XMLCoder
76+
extension Bracket: DynamicNodeEncoding {
77+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
78+
if key is XMLAttributeGroupCodingKey {
79+
return .attribute
80+
}
81+
switch key {
82+
case CodingKeys.type, CodingKeys.number:
83+
return .attribute
84+
case CodingKeys.lineEnd, CodingKeys.endLength, CodingKeys.lineType, CodingKeys.dashLength, CodingKeys.spaceLength, CodingKeys.color:
85+
return .attribute
86+
default:
87+
return .element
88+
}
89+
}
90+
}

Sources/MusicXML/Complex Types/Cancel.swift

+12
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ extension Cancel: Codable {
5353
try container.encode(fifths, forKey: .fifths)
5454
}
5555
}
56+
57+
import XMLCoder
58+
extension Cancel: DynamicNodeEncoding {
59+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
60+
switch key {
61+
case CodingKeys.location:
62+
return .attribute
63+
default:
64+
return .element
65+
}
66+
}
67+
}

Sources/MusicXML/Complex Types/Credit.swift

+11
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,14 @@ extension Credit.Kind.CodingKeys: XMLChoiceCodingKey {}
134134

135135
extension Credit: Equatable {}
136136
extension Credit: Codable {}
137+
138+
extension Credit: DynamicNodeEncoding {
139+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
140+
switch key {
141+
case CodingKeys.page:
142+
return .attribute
143+
default:
144+
return .element
145+
}
146+
}
147+
}

Sources/MusicXML/Complex Types/DashedFormatting.swift

+23-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,30 @@ extension DashedFormatting: Equatable {}
2828
extension DashedFormatting: Codable {
2929
// MARK: - Codable
3030

31-
private enum CodingKeys: String, CodingKey {
31+
internal enum CodingKeys: String, CodingKey {
3232
case dashLength = "dash-length"
3333
case spaceLength = "space-length"
3434
}
35+
36+
// MARK: Decodable
37+
38+
public init(from decoder: Decoder) throws {
39+
let container = try decoder.container(keyedBy: CodingKeys.self)
40+
self.dashLength = try container.decodeIfPresent(Tenths.self, forKey: .dashLength)
41+
self.spaceLength = try container.decodeIfPresent(Tenths.self, forKey: .spaceLength)
42+
}
43+
}
44+
45+
extension DashedFormatting.CodingKeys: XMLAttributeGroupCodingKey {}
46+
47+
import XMLCoder
48+
extension DashedFormatting: DynamicNodeEncoding {
49+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
50+
switch key {
51+
case CodingKeys.dashLength, CodingKeys.spaceLength:
52+
return .attribute
53+
default:
54+
return .element
55+
}
56+
}
3557
}

Sources/MusicXML/Complex Types/Dashes.swift

+15
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ extension Dashes: Codable {
4242
self.type = try container.decode(StartStopContinue.self, forKey: .type)
4343
}
4444
}
45+
46+
import XMLCoder
47+
extension Dashes: DynamicNodeEncoding {
48+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
49+
if key is XMLAttributeGroupCodingKey {
50+
return .attribute
51+
}
52+
switch key {
53+
case CodingKeys.type:
54+
return .attribute
55+
default:
56+
return .element
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)