-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCodedBy.swift
More file actions
249 lines (244 loc) · 10.4 KB
/
Copy pathCodedBy.swift
File metadata and controls
249 lines (244 loc) · 10.4 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/// Indicates the field or enum/protocol identifier needs to be decoded and
/// encoded by the provided `helper` instance.
///
/// An instance confirming to ``HelperCoder`` can be provided
/// to allow decoding/encoding customizations or to provide decoding/encoding
/// to non-`Codable` types. i.e ``LossySequenceCoder`` that decodes
/// sequence from JSON ignoring invalid data matches instead of throwing error
/// (failing decoding of entire sequence).
///
/// For enums and protocols, applying this attribute means ``HelperCoder``
/// will be used to decode and encode identifier value in internally/adjacently
/// tagged data.
///
/// - Parameter helper: The value that performs decoding and encoding.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The `helper`'s ``HelperCoder/Coded``
/// associated type must be the same as field type.
///
/// - Important: When using with enums and protocols if ``HelperCoder/Coded``
/// is other than `String` type must be provided with ``CodedAs()`` macro.
///
/// - Important: This attribute must be used combined with ``Codable(commonStrategies:)``
/// and ``CodedAt(_:)`` when applying to enums/protocols.
@attached(peer)
@available(swift 5.9)
public macro CodedBy<T: HelperCoder>(_ helper: T) =
#externalMacro(module: "MacroPlugin", type: "CodedBy")
/// Indicates the field needs to be decoded and encoded by the created `helper`
/// instance from provided arguments.
///
/// This can be used for decoding/encoding transformation based on properties
/// as an alternative to ``CodedBy(_:)`` macro. i.e. data transformations
/// based on specific version:
/// ```swift
/// @Codable
/// struct Dog {
/// let name: String
/// @Default(1)
/// let version: Int
/// @CodedBy(Info.VersionBasedTag.init, properties: \Dog.version)
/// let info: Info
///
/// @Codable
/// struct Info {
/// private(set) var tag: Int
///
/// struct VersionBasedTag: HelperCoder {
/// let version: Int
///
/// func decode(from decoder: any Decoder) throws -> Info {
/// var info = try Info(from: decoder)
/// info.tag += version >= 2 ? 1 : 0
/// return info
/// }
///
/// func encode(_ value: Info, to encoder: any Encoder) throws {
/// var info = value
/// info.tag -= version >= 2 ? 1 : 0
/// try info.encode(to: encoder)
/// }
/// }
/// }
/// }
/// ```
///
/// - Parameters:
/// - helperCreation: The function that will create the helper expression.
/// - properties: The key path to properties passed to the creation action.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The `Parent` type must be the current `struct`/`class`/`actor`
/// type. The `Helper` type's ``HelperCoder/Coded`` associated type must be
/// the same as field type. Only stored `Property` types are supported.
@attached(peer)
@available(swift 5.9)
public macro CodedBy<Parent, Helper: HelperCoder, each Property>(
_ helperCreation: (repeat each Property) -> Helper,
properties: repeat KeyPath<Parent, each Property>
) = #externalMacro(module: "MacroPlugin", type: "CodedBy")
/// Indicates the field needs to be decoded and encoded by the created `helper`
/// instance from provided arguments.
///
/// This can be used for decoding/encoding transformation based on additional
/// arguments and properties as an alternative to ``CodedBy(_:)`` and
/// ``CodedBy(_:properties:)`` macros. i.e. passing data to a sequence
/// of child container items:
/// ```swift
/// @Codable
/// struct Item: Identifiable {
/// let title: String
/// @CodedBy(
/// SequenceCoder.init, arguments: Image.IdentifierCoder.init,
/// .lossy, properties: \Item.id
/// )
/// let images: [Image]
/// let id: String
///
/// @Codable
/// struct Image: Identifiable {
/// var id: String { identifier }
///
/// @IgnoreCoding
/// private(set) var identifier: String!
/// let width: Int
/// let height: Int
///
/// struct IdentifierCoder: HelperCoder {
/// let id: String
///
/// func decode(from decoder: any Decoder) throws -> Image {
/// var image = try Image(from: decoder)
/// image.identifier = id
/// return image
/// }
///
/// func encode(_ value: Image, to encoder: any Encoder) throws
/// {
/// var image = value
/// image.identifier = nil
/// try image.encode(to: encoder)
/// }
/// }
/// }
/// }
/// ```
///
/// - Parameters:
/// - helperCreation: The function that will create the helper expression.
/// - arguments: Additional arguments first passed to the creation action.
/// - properties: The key path to properties passed to the creation action.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The `Parent` type must be the current `struct`/`class`/`actor`
/// type. The `Helper` type's ``HelperCoder/Coded`` associated type must be
/// the same as field type. Only stored `Property` types are supported.
@attached(peer)
@available(swift 5.9)
public macro CodedBy<Parent, Helper: HelperCoder, each Argument, each Property>(
_ helperCreation: (repeat each Argument, repeat each Property) -> Helper,
arguments: repeat each Argument,
properties: repeat KeyPath<Parent, each Property>
) = #externalMacro(module: "MacroPlugin", type: "CodedBy")
/// Indicates the field needs to be decoded and encoded by the created `helper`
/// instance from provided arguments.
///
/// This can be used for decoding/encoding transformation based on additional
/// arguments and properties as an alternative to ``CodedBy(_:)`` and
/// ``CodedBy(_:properties:)`` macros.
///
/// - Parameters:
/// - helperCreation: The function that will create the helper expression.
/// - argument1: Additional argument first passed to the creation action.
/// - properties: The key path to properties passed to the creation action.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The `Parent` type must be the current `struct`/`class`/`actor`
/// type. The `Helper` type's ``HelperCoder/Coded`` associated type must be
/// the same as field type. Only stored `Property` types are supported.
///
/// - SeeAlso: ``CodedBy(_:arguments:properties:)-7j53l``
@attached(peer)
@available(swift 5.9)
public macro CodedBy<Parent, Helper: HelperCoder, Argument1, each Property>(
_ helperCreation: (Argument1, repeat each Property) -> Helper,
arguments argument1: Argument1,
properties: repeat KeyPath<Parent, each Property>
) = #externalMacro(module: "MacroPlugin", type: "CodedBy")
/// Indicates the field needs to be decoded and encoded by the created `helper`
/// instance from provided arguments.
///
/// This can be used for decoding/encoding transformation based on additional
/// arguments and properties as an alternative to ``CodedBy(_:)`` and
/// ``CodedBy(_:properties:)`` macros.
///
/// - Parameters:
/// - helperCreation: The function that will create the helper expression.
/// - argument1: Additional argument first passed to the creation action.
/// - argument2: Additional argument passed second to the creation action.
/// - properties: The key path to properties passed to the creation action.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The `Parent` type must be the current `struct`/`class`/`actor`
/// type. The `Helper` type's ``HelperCoder/Coded`` associated type must be
/// the same as field type. Only stored `Property` types are supported.
///
/// - SeeAlso: ``CodedBy(_:arguments:properties:)-7j53l``
@attached(peer)
@available(swift 5.9)
public macro CodedBy<
Parent, Helper: HelperCoder, Argument1, Argument2, each Property
>(
_ helperCreation: (Argument1, Argument2, repeat each Property) -> Helper,
arguments argument1: Argument1, _ argument2: Argument2,
properties: repeat KeyPath<Parent, each Property>
) = #externalMacro(module: "MacroPlugin", type: "CodedBy")
/// Indicates the field needs to be decoded and encoded by the created `helper`
/// instance from provided arguments.
///
/// This can be used for decoding/encoding transformation based on additional
/// arguments and properties as an alternative to ``CodedBy(_:)`` and
/// ``CodedBy(_:properties:)`` macros.
///
/// - Parameters:
/// - helperCreation: The function that will create the helper expression.
/// - argument1: Additional argument first passed to the creation action.
/// - argument2: Additional argument passed second to the creation action.
/// - argument3: Additional argument passed third to the creation action.
/// - properties: The key path to properties passed to the creation action.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The `Parent` type must be the current `struct`/`class`/`actor`
/// type. The `Helper` type's ``HelperCoder/Coded`` associated type must be
/// the same as field type. Only stored `Property` types are supported.
///
/// - SeeAlso: ``CodedBy(_:arguments:properties:)-7j53l``
@attached(peer)
@available(swift 5.9)
public macro CodedBy<
Parent, Helper: HelperCoder, Argument1, Argument2, Argument3, each Property
>(
_ helperCreation: (Argument1, Argument2, Argument3, repeat each Property) ->
Helper,
arguments argument1: Argument1, _ argument2: Argument2,
_ argument3: Argument3, properties: repeat KeyPath<Parent, each Property>
) = #externalMacro(module: "MacroPlugin", type: "CodedBy")