-
-
Save sturdysturge/8c5344f290e5d07ebfe15aeb42ef35c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private extension ColourViewModel { | |
struct Request: Codable { | |
let input: [Input]? | |
let model: String? | |
} | |
enum Input: Codable { | |
static let unknown: Input = .string("N") | |
case integerArray([Int]) | |
case string(String) | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
if let intValue = try? container.decode([Int].self) { | |
self = .integerArray(intValue) | |
return | |
} | |
if let stringValue = try? container.decode(String.self) { | |
self = .string(stringValue) | |
return | |
} | |
let decodingContext = DecodingError | |
.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type") | |
throw DecodingError.typeMismatch(Input.self, decodingContext) | |
} | |
func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
switch self { | |
case .integerArray(let intValue): try container.encode(intValue) | |
case .string(let stringValue): try container.encode(stringValue) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment