@@ -11,6 +11,7 @@ enum DecodingAttribute: Identifiable, Equatable {
11
11
case date( AttributeSyntax )
12
12
case url( AttributeSyntax )
13
13
case stringOrInt( AttributeSyntax )
14
+ case stringOrDouble( AttributeSyntax )
14
15
case stringToDouble( AttributeSyntax )
15
16
16
17
var id : String {
@@ -23,6 +24,8 @@ enum DecodingAttribute: Identifiable, Equatable {
23
24
return String ( describing: CustomURL . self)
24
25
case . stringOrInt:
25
26
return String ( describing: StringOrInt . self)
27
+ case . stringOrDouble:
28
+ return String ( describing: StringOrDouble . self)
26
29
case . stringToDouble:
27
30
return String ( describing: StringToDouble . self)
28
31
}
@@ -38,6 +41,8 @@ enum DecodingAttribute: Identifiable, Equatable {
38
41
return attributeSyntax
39
42
case . stringOrInt( let attributeSyntax) :
40
43
return attributeSyntax
44
+ case . stringOrDouble( let attributeSyntax) :
45
+ return attributeSyntax
41
46
case . stringToDouble( let attributeSyntax) :
42
47
return attributeSyntax
43
48
}
@@ -87,6 +92,10 @@ struct DecodeVariableBuild {
87
92
return . stringOrInt( variableAttribute)
88
93
}
89
94
95
+ if variableAttribute. adapter. name == String ( describing: StringOrDouble . self) {
96
+ return . stringOrDouble( variableAttribute)
97
+ }
98
+
90
99
if variableAttribute. adapter. name == String ( describing: StringToDouble . self) {
91
100
return . stringToDouble( variableAttribute)
92
101
}
@@ -112,6 +121,10 @@ struct DecodeVariableBuild {
112
121
return buildStringOrInt ( attribute: attribute)
113
122
}
114
123
124
+ if let attribute = variableDecodingAttributes. getAttribute ( macro: StringOrDouble . self) ? . attribute {
125
+ return buildStringOrDouble ( attribute: attribute)
126
+ }
127
+
115
128
if let attribute = variableDecodingAttributes. getAttribute ( macro: StringToDouble . self) ? . attribute {
116
129
return buildStringToDouble ( attribute: attribute)
117
130
}
@@ -207,6 +220,41 @@ struct DecodeVariableBuild {
207
220
return . init( otherBuilder: ifBuilder)
208
221
}
209
222
223
+ func buildStringOrDouble( attribute: AttributeSyntax ) -> CodeBlockItemSyntaxBuilder {
224
+ guard type == " String " else {
225
+ return buildBasicDecode ( )
226
+ }
227
+
228
+ let conditionalName = " tmp " + varName. capitalized
229
+
230
+ let ifBody : [ CodeBlockItemSyntaxBuilder ] = [
231
+ . init( code: " \( varName) = \( conditionalName) " )
232
+ ]
233
+
234
+ let elseIfBody : [ CodeBlockItemSyntaxBuilder ] = [
235
+ . init( code: " \( varName) = String( \( conditionalName) ) " )
236
+ ]
237
+
238
+ let elseBody : [ CodeBlockItemSyntaxBuilder ] = [
239
+ . init( code: " \( varName) = nil " )
240
+ ]
241
+
242
+ let elseIfBuilder = IfExprSyntaxBuilder (
243
+ condition: " if let \( conditionalName) = try? container.decode(Double.self, forKey: . \( varName) ) " ,
244
+ body: elseIfBody,
245
+ elseBody: elseBody
246
+ )
247
+ let elseIfCode = CodeBlockItemSyntaxBuilder . init ( otherBuilder: elseIfBuilder)
248
+
249
+ let ifBuilder = IfExprSyntaxBuilder (
250
+ condition: " if let \( conditionalName) = try? container.decode(String.self, forKey: . \( varName) ) " ,
251
+ body: ifBody,
252
+ elseBody: [ elseIfCode]
253
+ )
254
+
255
+ return . init( otherBuilder: ifBuilder)
256
+ }
257
+
210
258
func buildStringToDouble( attribute: AttributeSyntax ) -> CodeBlockItemSyntaxBuilder {
211
259
212
260
guard type == " Double " else {
0 commit comments