forked from CoreOffice/XMLCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntOrString.swift
41 lines (35 loc) · 1013 Bytes
/
IntOrString.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// IntOrString.swift
// XMLCoderTests
//
// Created by Benjamin Wetherfield on 11/24/19.
//
import XMLCoder
internal enum IntOrString: Equatable {
case int(Int)
case string(String)
}
extension IntOrString: Codable {
enum CodingKeys: String, CodingKey {
case int
case string
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case let .int(value):
try container.encode(value, forKey: .int)
case let .string(value):
try container.encode(value, forKey: .string)
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
self = .int(try container.decode(Int.self, forKey: .int))
} catch {
self = .string(try container.decode(String.self, forKey: .string))
}
}
}
extension IntOrString.CodingKeys: XMLChoiceCodingKey {}