Skip to content

Instantly share code, notes, and snippets.

@Matt54
Created January 25, 2024 23:18
Show Gist options
  • Select an option

  • Save Matt54/7d41bebdbcf4b30d75de77cd45c0aed9 to your computer and use it in GitHub Desktop.

Select an option

Save Matt54/7d41bebdbcf4b30d75de77cd45c0aed9 to your computer and use it in GitHub Desktop.
Stable Codable extension for ChordType in Tonic
import Tonic
import Foundation
extension ChordType: Codable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let chordKey = try values.decode(String.self, forKey: .chordKey)
self.init(chordKey: chordKey)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.chordKey, forKey: .chordKey)
}
public init(chordKey: String) {
for chordType in ChordType.allCases {
if chordKey == chordType.chordKey {
self = chordType
return
}
}
// some default case
self = ChordType.augmentedTriad
}
enum CodingKeys: String, CodingKey {
case chordKey
}
/// by having a constant name, we can ensure that breaking changes don't happen due to integer value mismatch in ChordType (such as from adding additional cases, thus changing rawValues of ChordType)
var chordKey: String {
switch self {
case .majorTriad: return "majorTriad"
case .minorTriad: return "minorTriad"
case .diminishedTriad: return "diminishedTriad"
case .augmentedTriad: return "augmentedTriad"
case .suspendedSecondTriad: return "suspended2Triad"
case .suspendedFourthTriad: return "suspendedTriad" // leaving this since it was changed later
case .sixth: return "sixth"
case .minorSixth: return "minorSixth"
case .halfDiminishedSeventh: return "halfDiminishedSeventh"
case .diminishedSeventh: return "diminishedSeventh"
case .dominantSeventh: return "dominantSeventh"
case .majorSeventh: return "majorSeventh"
case .minorSeventh: return "minorSeventh"
case .minorMajorSeventh: return "minorMajorSeventh"
case .halfDiminishedNinth: return "halfDiminishedNinth"
case .dominantNinth: return "dominantNinth"
case .flatNinth: return "flatNinth"
case .sharpNinth: return "sharpNinth"
case .majorNinth: return "majorNinth"
case .minorNinth: return "minorNinth"
case .minorFlatNinth: return "minorFlatNinth"
case .majorAddNine: return "majorAddNine"
case .minorAddNine: return "minorAddNine"
case .sixOverNine: return "sixOverNine"
case .majorEleventh: return "majorEleventh"
case .dominantEleventh: return "dominantEleventh"
case .minorEleventh: return "minorEleventh"
case .halfDiminishedEleventh: return "halfDiminishedEleventh"
case .majorSeventhFlatFifth: return "majorSeventhFlatFifth"
case .minorSeventhSharpFifth: return "minorSeventhSharpFifth"
case .majorNinthSharpEleventh: return "majorNinthSharpEleventh"
case .dominantFlatFifth: return "dominantFlatFifth"
case .dominantSharpFifth: return "dominantSharpFifth"
case .dominantFlatNinthSharpEleventh: return "dominantFlatNinthSharpEleventh"
case .dominantSharpNinthSharpEleventh: return "dominantSharpNinthSharpEleventh"
case .minorSeventhFlatNinthAddEleventh: return "minorSeventhFlatNinthAddEleventh"
case .majorThirteenth: return "majorThirteenth"
case .minorThirteenth: return "minorThirteenth"
case .minorFlatThirteenthFlatNinth: return "minorFlatThirteenthFlatNinth"
case .majorThirteenthSharpEleventh: return "majorThirteenthSharpEleventh"
case .dominantThirteenth: return "dominantThirteenth"
case .minorEleventhFlatThirteenth: return "majorEleventhFlatThirteenth"
case .halfDiminishedFlatThirteenth: return "halfDiminishedFlatThirteenth"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment