Skip to content

Commit 20974f6

Browse files
committed
Decode gender and device class case insensitive
1 parent ea77249 commit 20974f6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Sources/Core/Models/Codable/DeviceClass.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@ public enum DeviceClass: String, Codable, Hashable, Sendable {
44
case iPhone = "IPHONE"
55
/// The iPad device class.
66
case iPad = "IPAD"
7+
8+
public init(from decoder: Decoder) throws {
9+
let container = try decoder.singleValueContainer()
10+
let rawString = try container.decode(String.self)
11+
if let deviceClass = DeviceClass(rawValue: rawString.uppercased()) {
12+
self = deviceClass
13+
} else {
14+
throw DecodingError.dataCorruptedError(
15+
in: container,
16+
debugDescription: "Cannot initialize \(Self.self) from invalid String value \(rawString)"
17+
)
18+
}
19+
}
720
}

Sources/Core/Models/Codable/Gender.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@ public enum Gender: String, Codable, Hashable, Sendable {
44
case male = "M"
55
/// Female.
66
case female = "F"
7+
8+
public init(from decoder: Decoder) throws {
9+
let container = try decoder.singleValueContainer()
10+
let rawString = try container.decode(String.self)
11+
if let gender = Gender(rawValue: rawString.uppercased()) {
12+
self = gender
13+
} else {
14+
throw DecodingError.dataCorruptedError(
15+
in: container,
16+
debugDescription: "Cannot initialize \(Self.self) from invalid String value \(rawString)"
17+
)
18+
}
19+
}
720
}

0 commit comments

Comments
 (0)