Skip to content

Commit 780ca2f

Browse files
committed
Fix codegen for enum types (#87)
`Decoder` should be `any Decoder` in Swift 6.
1 parent a63657d commit 780ca2f

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

Tests/PklSwiftTests/Fixtures/Generated/UnionTypes.pkl.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension UnionTypes {
1616
case grape(Grape)
1717
case apple(Apple)
1818

19-
public init(from decoder: Decoder) throws {
19+
public init(from decoder: any Decoder) throws {
2020
let container = try decoder.singleValueContainer()
2121
let value = try container.decode(PklSwift.PklAny.self).value
2222
switch value?.base {
@@ -49,7 +49,7 @@ extension UnionTypes {
4949
case zebra(Zebra)
5050
case donkey(Donkey)
5151

52-
public init(from decoder: Decoder) throws {
52+
public init(from decoder: any Decoder) throws {
5353
let container = try decoder.singleValueContainer()
5454
let value = try container.decode(PklSwift.PklAny.self).value
5555
switch value?.base {
@@ -84,7 +84,7 @@ extension UnionTypes {
8484
}
8585
}
8686

87-
public init(from decoder: Decoder) throws {
87+
public init(from decoder: any Decoder) throws {
8888
let container = try decoder.singleValueContainer()
8989
let value = try container.decode(PklSwift.PklAny.self).value
9090
switch value?.base {
@@ -119,12 +119,12 @@ extension UnionTypes {
119119
case int(Int)
120120
case float64(Float64)
121121

122-
private static func decodeNumeric(from decoder: Decoder, _ container: any SingleValueDecodingContainer) -> IntOrFloat? {
122+
private static func decodeNumeric(from decoder: any Decoder, _ container: any SingleValueDecodingContainer) -> IntOrFloat? {
123123
return (try? .int(container.decode(Int.self)))
124124
?? (try? .float64(container.decode(Float64.self)))
125125
}
126126

127-
public init(from decoder: Decoder) throws {
127+
public init(from decoder: any Decoder) throws {
128128
let container = try decoder.singleValueContainer()
129129
let decoded = IntOrFloat.decodeNumeric(from: decoder, container)
130130
if decoded != nil {
@@ -163,7 +163,7 @@ extension UnionTypes {
163163
}
164164
}
165165

166-
public init(from decoder: Decoder) throws {
166+
public init(from decoder: any Decoder) throws {
167167
let container = try decoder.singleValueContainer()
168168
let value = try container.decode(PklSwift.PklAny.self).value
169169
switch value?.base {
@@ -200,14 +200,14 @@ extension UnionTypes {
200200
case int32(Int32)
201201
case int(Int)
202202

203-
private static func decodeNumeric(from decoder: Decoder, _ container: any SingleValueDecodingContainer) -> Numbers? {
203+
private static func decodeNumeric(from decoder: any Decoder, _ container: any SingleValueDecodingContainer) -> Numbers? {
204204
return (try? .int8(container.decode(Int8.self)))
205205
?? (try? .int16(container.decode(Int16.self)))
206206
?? (try? .int32(container.decode(Int32.self)))
207207
?? (try? .int(container.decode(Int.self)))
208208
}
209209

210-
public init(from decoder: Decoder) throws {
210+
public init(from decoder: any Decoder) throws {
211211
let container = try decoder.singleValueContainer()
212212
let decoded = Numbers.decodeNumeric(from: decoder, container)
213213
if decoded != nil {

Tests/PklSwiftTests/Fixtures/Generated/pkl_swift_example_Poly.pkl.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension pkl_swift_example_Poly {
4141
}
4242
}
4343

44-
public init(from decoder: Decoder) throws {
44+
public init(from decoder: any Decoder) throws {
4545
let container = try decoder.singleValueContainer()
4646
let value = try container.decode(PklSwift.PklAny.self).value
4747
switch value?.base {
@@ -183,7 +183,7 @@ extension pkl_swift_example_Poly {
183183
}
184184
}
185185

186-
public init(from decoder: Decoder) throws {
186+
public init(from decoder: any Decoder) throws {
187187
let container = try decoder.singleValueContainer()
188188
let value = try container.decode(PklSwift.PklAny.self).value
189189
switch value?.base {
@@ -349,7 +349,7 @@ extension pkl_swift_example_Poly {
349349
}
350350
}
351351

352-
public init(from decoder: Decoder) throws {
352+
public init(from decoder: any Decoder) throws {
353353
let container = try decoder.singleValueContainer()
354354
let value = try container.decode(PklSwift.PklAny.self).value
355355
switch value?.base {
@@ -627,7 +627,7 @@ extension pkl_swift_example_Poly {
627627
}
628628
}
629629

630-
public init(from decoder: Decoder) throws {
630+
public init(from decoder: any Decoder) throws {
631631
let container = try decoder.singleValueContainer()
632632
let value = try container.decode(PklSwift.PklAny.self).value
633633
switch value?.base {
@@ -854,7 +854,7 @@ extension pkl_swift_example_Poly {
854854
case mappingStringString([String: String])
855855
case mapStringString([String: String])
856856

857-
public init(from decoder: Decoder) throws {
857+
public init(from decoder: any Decoder) throws {
858858
let container = try decoder.singleValueContainer()
859859
let value = try container.decode(PklSwift.PklAny.self).value
860860
switch value?.base {

codegen/snippet-tests/output/Enums.pkl.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Enums {
1818
case zebra(Zebra)
1919
case monkey(Monkey)
2020

21-
public init(from decoder: Decoder) throws {
21+
public init(from decoder: any Decoder) throws {
2222
let container = try decoder.singleValueContainer()
2323
let value = try container.decode(PklSwift.PklAny.self).value
2424
switch value?.base {
@@ -45,7 +45,7 @@ extension Enums {
4545
case mappingStringString([String: String])
4646
case listingString([String])
4747

48-
public init(from decoder: Decoder) throws {
48+
public init(from decoder: any Decoder) throws {
4949
let container = try decoder.singleValueContainer()
5050
let value = try container.decode(PklSwift.PklAny.self).value
5151
switch value?.base {
@@ -70,7 +70,7 @@ extension Enums {
7070
case string(String)
7171
case string(String)
7272

73-
public init(from decoder: Decoder) throws {
73+
public init(from decoder: any Decoder) throws {
7474
let container = try decoder.singleValueContainer()
7575
let value = try container.decode(PklSwift.PklAny.self).value
7676
switch value?.base {
@@ -96,7 +96,7 @@ extension Enums {
9696
case optionalHorse(Horse?)
9797
case zebra(Zebra)
9898

99-
public init(from decoder: Decoder) throws {
99+
public init(from decoder: any Decoder) throws {
100100
let container = try decoder.singleValueContainer()
101101
let value = try container.decode(PklSwift.PklAny.self).value
102102
switch value?.base {

codegen/src/internal/EnumGen.pkl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ local throwTypeMismatch =
120120

121121
local initMethod =
122122
new Listing {
123-
"\(module.indent)public init(from decoder: Decoder) throws {"
123+
"\(module.indent)public init(from decoder: any Decoder) throws {"
124124
"\(module.indent.repeat(2))let container = try decoder.singleValueContainer()"
125125
when (!enumNumericMembers.isEmpty) {
126126
"\(module.indent.repeat(2))let decoded = \(module.mapping.name).decodeNumeric(from: decoder, container)"
@@ -190,7 +190,7 @@ local synthesizedHash =
190190

191191
local decodeNumeric =
192192
new Listing {
193-
"\(module.indent)private static func decodeNumeric(from decoder: Decoder, _ container: any SingleValueDecodingContainer) -> \(module.mapping.name)? {"
193+
"\(module.indent)private static func decodeNumeric(from decoder: any Decoder, _ container: any SingleValueDecodingContainer) -> \(module.mapping.name)? {"
194194
when (enumNumericMembers.length == 1) {
195195
local member = enumNumericMembers.first
196196
"\(module.indent.repeat(2))return try? .\(member.name)(container.decode(\(member.renderedType).self))"

0 commit comments

Comments
 (0)