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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)