Skip to content

Commit a1ac94e

Browse files
authored
Updates Class and TypeAlias representation according to SPICE-0021 (#67)
1 parent c2c2437 commit a1ac94e

File tree

6 files changed

+60
-12
lines changed

6 files changed

+60
-12
lines changed

Sources/PklSwift/API/Class.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,29 @@
1717
import MessagePack
1818

1919
/// Class is the Swift representation of Pkl's `pkl.base#Class`.
20-
public struct Class: Hashable {}
20+
public struct Class: Hashable {
21+
/// The URI of the module containing this class.
22+
/// Will be an empty string for values encoded by Pkl versions older than 0.30.
23+
public let moduleUri: String
24+
25+
/// The qualified name of this class.
26+
/// Will be an empty string for values encoded by Pkl versions older than 0.30.
27+
public let qualifiedName: String
28+
}
2129

2230
extension Class: PklSerializableType, Sendable {
2331
public static let messageTag: PklValueType = .class
2432

2533
public static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> Class {
34+
if fields.count > 1 { // pkl 0.30+ includes the qualified name and module uri
35+
try checkFieldCount(fields, codingPath: codingPath, min: 3)
36+
return try Class(
37+
moduleUri: fields[1].decode(String.self),
38+
qualifiedName: fields[2].decode(String.self)
39+
)
40+
}
41+
2642
try checkFieldCount(fields, codingPath: codingPath, min: 1)
27-
return Class()
43+
return Class(moduleUri: "", qualifiedName: "")
2844
}
2945
}

Sources/PklSwift/API/TypeAlias.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,29 @@
1717
import MessagePack
1818

1919
/// TypeAlias is the Swift representation of Pkl's `pkl.base#TypeAlias`.
20-
public struct TypeAlias: Hashable {}
20+
public struct TypeAlias: Hashable {
21+
/// The URI of the module containing this typealias.
22+
/// Will be an empty string for values encoded by Pkl versions older than 0.30.
23+
public let moduleUri: String
24+
25+
/// The qualified name of this typealias.
26+
/// Will be an empty string for values encoded by Pkl versions older than 0.30.
27+
public let qualifiedName: String
28+
}
2129

2230
extension TypeAlias: PklSerializableType, Sendable {
2331
public static let messageTag: PklValueType = .typealias
2432

2533
public static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> TypeAlias {
34+
if fields.count > 1 { // pkl 0.30+ includes the qualified name and module uri
35+
try checkFieldCount(fields, codingPath: codingPath, min: 3)
36+
return try TypeAlias(
37+
moduleUri: fields[1].decode(String.self),
38+
qualifiedName: fields[2].decode(String.self)
39+
)
40+
}
41+
2642
try checkFieldCount(fields, codingPath: codingPath, min: 1)
27-
return TypeAlias()
43+
return TypeAlias(moduleUri: "", qualifiedName: "")
2844
}
2945
}

Sources/PklSwift/EvaluatorManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,13 @@ let pklVersion0_26 = SemanticVersion("0.26.0")!
378378
let pklVersion0_27 = SemanticVersion("0.27.0")!
379379
let pklVersion0_28 = SemanticVersion("0.28.0")!
380380
let pklVersion0_29 = SemanticVersion("0.29.0")!
381+
let pklVersion0_30 = SemanticVersion("0.30.0")!
381382

382383
let supportedPklVersions = [
383384
pklVersion0_25,
384385
pklVersion0_26,
385386
pklVersion0_27,
386387
pklVersion0_28,
387388
pklVersion0_29,
389+
pklVersion0_30,
388390
]

Tests/PklSwiftTests/Fixtures/ApiTypes.pkl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import "pkl:base"
2+
13
res1: Duration = 10.h
24
res2: DataSize = 1.2345.gib
35

46
stringClass: Class = String
5-
moduleClass: Class = module.getClass()
6-
typeAlias: TypeAlias = Foo
7+
moduleClass: Class = base.getClass()
8+
typeAlias: TypeAlias = UInt8
79

8-
typealias Foo = String

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ extension ApiTypes {
3232
}
3333
}
3434

35-
public typealias Foo = String
36-
3735
/// Load the Pkl module at the given source and evaluate it into `ApiTypes.Module`.
3836
///
3937
/// - Parameter source: The source of the Pkl module.

Tests/PklSwiftTests/FixturesTest.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,29 @@ class FixturesTest: XCTestCase {
9090
evaluator: self.evaluator,
9191
source: .path("\(#filePath)/../Fixtures/ApiTypes.pkl")
9292
)
93+
94+
let stringClass: Class
95+
let moduleClass: Class
96+
let typeAlias: TypeAlias
97+
let version = try await SemanticVersion(EvaluatorManager().getVersion())!
98+
if version < pklVersion0_30 {
99+
stringClass = Class(moduleUri: "", qualifiedName: "")
100+
moduleClass = Class(moduleUri: "", qualifiedName: "")
101+
typeAlias = TypeAlias(moduleUri: "", qualifiedName: "")
102+
} else {
103+
stringClass = Class(moduleUri: "pkl:base", qualifiedName: "pkl.base#String")
104+
moduleClass = Class(moduleUri: "pkl:base", qualifiedName: "pkl.base")
105+
typeAlias = TypeAlias(moduleUri: "pkl:base", qualifiedName: "pkl.base#UInt8")
106+
}
107+
93108
XCTAssertEqual(
94109
result,
95110
ApiTypes.Module(
96111
res1: .hours(10),
97112
res2: .gibibytes(1.2345),
98-
stringClass: Class(),
99-
moduleClass: Class(),
100-
typeAlias: TypeAlias()
113+
stringClass: stringClass,
114+
moduleClass: moduleClass,
115+
typeAlias: typeAlias
101116
)
102117
)
103118
}

0 commit comments

Comments
 (0)