SHARE
TWEET

Untitled

a guest Aug 17th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum CodingKeys: String, CodingKey {
  2.     case attributeCode = "attribute_code"
  3.     case value = "value"
  4. }
  5.      
  6. init(from decoder: Decoder) throws {
  7.     let container = try decoder.singleValueContainer()
  8.     if let x = try? container.decode([String].self) {
  9.         self = .stringArray(x)
  10.         return
  11.     }
  12.     if let x = try? container.decode(String.self) {
  13.         self = .string(x)
  14.         return
  15.     }
  16.     throw DecodingError.typeMismatch(ValueUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ValueUnion"))
  17. }
  18.  
  19. func encode(to encoder: Encoder) throws {
  20.     var container = encoder.singleValueContainer()
  21.     switch self {
  22.     case .string(let x):
  23.         try container.encode(x)
  24.     case .stringArray(let x):
  25.         try container.encode(x)
  26.     }
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top