Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created January 24, 2023 02:36
Show Gist options
  • Save sturdysturge/8c5344f290e5d07ebfe15aeb42ef35c9 to your computer and use it in GitHub Desktop.
Save sturdysturge/8c5344f290e5d07ebfe15aeb42ef35c9 to your computer and use it in GitHub Desktop.
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