-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
Copy pathAutoCodable.swift
162 lines (126 loc) · 4.2 KB
/
AutoCodable.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// AutoCodable.swift
// TemplatesTests
//
// Created by Ilya Puchka on 13/04/2018.
// Copyright © 2018 Pixle. All rights reserved.
//
#if canImport(ObjectiveC)
import Foundation
protocol AutoDecodable: Swift.Decodable {}
protocol AutoEncodable: Swift.Encodable {}
protocol AutoCodable: AutoDecodable, AutoEncodable {}
public struct CustomKeyDecodable: AutoDecodable {
let stringValue: String
let boolValue: Bool
let intValue: Int
enum CodingKeys: String, CodingKey {
case intValue = "integer"
// sourcery:inline:auto:CustomKeyDecodable.CodingKeys.AutoCodable
case stringValue
case boolValue
// sourcery:end
}
}
public struct CustomMethodsCodable: AutoCodable {
let boolValue: Bool
let intValue: Int?
let optionalString: String?
let requiredString: String
let requiredStringWithDefault: String
var computedPropertyToEncode: Int {
return 0
}
static let defaultIntValue: Int = 0
static let defaultRequiredStringWithDefault: String = ""
static func decodeIntValue(from container: KeyedDecodingContainer<CodingKeys>) -> Int? {
return (try? container.decode(String.self, forKey: .intValue)).flatMap(Int.init)
}
static func decodeBoolValue(from decoder: Decoder) throws -> Bool {
return try decoder.container(keyedBy: CodingKeys.self).decode(Bool.self, forKey: .boolValue)
}
func encodeIntValue(to container: inout KeyedEncodingContainer<CodingKeys>) {
try? container.encode(String(intValue ?? 0), forKey: .intValue)
}
func encodeBoolValue(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(boolValue, forKey: .boolValue)
}
func encodeComputedPropertyToEncode(to container: inout KeyedEncodingContainer<CodingKeys>) {
try? container.encode(computedPropertyToEncode, forKey: .computedPropertyToEncode)
}
func encodeAdditionalValues(to encoder: Encoder) throws {
}
}
public struct CustomContainerCodable: AutoCodable {
let value: Int
enum CodingKeys: String, CodingKey {
case nested
case value
}
static func decodingContainer(_ decoder: Decoder) throws -> KeyedDecodingContainer<CodingKeys> {
return try decoder.container(keyedBy: CodingKeys.self)
.nestedContainer(keyedBy: CodingKeys.self, forKey: .nested)
}
func encodingContainer(_ encoder: Encoder) -> KeyedEncodingContainer<CodingKeys> {
var container = encoder.container(keyedBy: CodingKeys.self)
return container.nestedContainer(keyedBy: CodingKeys.self, forKey: .nested)
}
}
struct CustomCodingWithNotAllDefinedKeys: AutoCodable {
let value: Int
var computedValue: Int { return 0 }
enum CodingKeys: String, CodingKey {
case value
// sourcery:inline:auto:CustomCodingWithNotAllDefinedKeys.CodingKeys.AutoCodable
case computedValue
// sourcery:end
}
func encodeComputedValue(to container: inout KeyedEncodingContainer<CodingKeys>) {
try? container.encode(computedValue, forKey: .computedValue)
}
}
struct SkipDecodingWithDefaultValueOrComputedProperty: AutoCodable {
let value: Int
let skipValue: Int = 0
var computedValue: Int { return 0 }
enum CodingKeys: String, CodingKey {
case value
case computedValue
}
}
struct SkipEncodingKeys: AutoCodable {
let value: Int
let skipValue: Int
enum SkipEncodingKeys {
case skipValue
}
}
enum SimpleEnum: AutoCodable {
case someCase
case anotherCase
}
enum AssociatedValuesEnum: AutoCodable, Equatable {
case someCase(id: Int, name: String)
case unnamedCase(Int, String)
case mixCase(Int, name: String)
case anotherCase
enum CodingKeys: String, CodingKey {
case enumCaseKey = "type"
// sourcery:inline:auto:AssociatedValuesEnum.CodingKeys.AutoCodable
case someCase
case unnamedCase
case mixCase
case anotherCase
case id
case name
// sourcery:end
}
}
enum AssociatedValuesEnumNoCaseKey: AutoCodable, Equatable {
case someCase(id: Int, name: String)
case unnamedCase(Int, String)
case mixCase(Int, name: String)
case anotherCase
}
#endif