Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ let package = Package(
"Fixtures/Collections.pkl",
"Fixtures/Poly.pkl",
"Fixtures/ApiTypes.pkl",
"Fixtures/Collections2.pkl",
],
swiftSettings: [.enableUpcomingFeature("StrictConcurrency")]
),
Expand Down
70 changes: 70 additions & 0 deletions Sources/PklSwift/API/IntSeq.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import MessagePack

public struct IntSeq: Decodable, Sendable {
public let start: Int
public let end: Int
public let step: Int

public let intSeq: StrideThrough<Int>

public init(start: Int, end: Int, step: Int = 1) {
self.start = start
self.end = end
self.step = step
self.intSeq = stride(from: start, through: end, by: step)
}
}

extension IntSeq: Hashable {
public func hash(into hasher: inout Hasher) {
self.start.hash(into: &hasher)
self.end.hash(into: &hasher)
self.step.hash(into: &hasher)
}

public static func == (lhs: IntSeq, rhs: IntSeq) -> Bool {
lhs.start == rhs.start
&& lhs.end == rhs.end
&& lhs.step == rhs.step
}
}

extension IntSeq: PklSerializableType {
public static var messageTag: PklValueType { .intSeq }

public static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> Self {
try checkFieldCount(fields, codingPath: codingPath, min: 4)
return try Self(
start: self.decodeInt(fields[1], codingPath: codingPath, index: 1),
end: self.decodeInt(fields[2], codingPath: codingPath, index: 2),
step: self.decodeInt(fields[3], codingPath: codingPath, index: 3)
)
}

private static func decodeInt(_ value: MessagePackValue, codingPath: [any CodingKey], index: Int) throws -> Int {
guard case .int(let intValue) = value else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Expected field \(index) to be an integer but got \(value.debugDataTypeDescription)"
))
}
return Int(intValue)
}
}
55 changes: 55 additions & 0 deletions Sources/PklSwift/API/Pair.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
// Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import MessagePack

// Must be marked @unchecked Sendable because A or B can be AnyHashable
public struct Pair<A: Hashable, B: Hashable>: Hashable, @unchecked Sendable {
/// The value of the Pair's first element.
public let first: A

/// The value of the Pair's second element.
public let second: B

public init(_ first: A, _ second: B) {
self.first = first
self.second = second
}

public var tupleValue: (A, B) { (self.first, self.second) }
}

extension Pair {
// this can't be a let because Pair is generic
public static var messageTag: PklValueType { .pair }
}

extension Pair: PklSerializableType {
public static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> Self {
try checkFieldCount(fields, codingPath: codingPath, min: 3)
let first: A = try Self.decodeElement(fields[1], codingPath: codingPath)
let second: B = try Self.decodeElement(fields[2], codingPath: codingPath)
return Self(first, second)
}

private static func decodeElement<T: Decodable>(_ value: MessagePackValue, codingPath: [any CodingKey]) throws -> T {
try value.decode(T.self)
}

private static func decodeElement<T: Hashable>(_ value: MessagePackValue, codingPath: [any CodingKey]) throws -> T {
try (_PklDecoder.decodePolymorphic(value, codingPath: codingPath))?.value as! T
}
}
55 changes: 55 additions & 0 deletions Sources/PklSwift/API/PklRegex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import MessagePack

public struct PklRegex: Sendable {
private let pattern: String

public var regex: Regex<AnyRegexOutput> { try! Regex(self.pattern) }

public init(_ pattern: String) throws {
self.pattern = pattern
// check that this pattern is valid but don't store the regex
_ = try Regex(pattern)
}
}

extension PklRegex: Hashable {
public func hash(into hasher: inout Hasher) {
self.pattern.hash(into: &hasher)
}

public static func == (lhs: PklRegex, rhs: PklRegex) -> Bool {
lhs.pattern == rhs.pattern
}
}

extension PklRegex: PklSerializableType {
public static var messageTag: PklValueType { .regex }

public static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> Self {
try checkFieldCount(fields, codingPath: codingPath, min: 2)
guard case .string(let pattern) = fields[1] else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Expected field 0 to be a string but got \(fields[0].debugDataTypeDescription)"
))
}
return try Self(pattern)
}
}
57 changes: 43 additions & 14 deletions Sources/PklSwift/Decoder/PklDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public enum PklValueType: UInt8, Decodable, Sendable {

public protocol PklSerializableType: Decodable {
static var messageTag: PklValueType { get }

static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> Self
}

extension PklSerializableType {
static func checkFieldCount(_ fields: [MessagePackValue], codingPath: [any CodingKey], min: Int) throws {
guard fields.count >= min else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Expected at least \(min) fields but got \(fields.count)"
))
}
}
}

public protocol PklSerializableValueUnitType: PklSerializableType {
Expand All @@ -49,35 +63,41 @@ public protocol PklSerializableValueUnitType: PklSerializableType {
init(_: ValueType, unit: UnitType)
}

extension Decodable where Self: PklSerializableValueUnitType {
extension Decodable where Self: PklSerializableType {
public init(from decoder: Decoder) throws {
guard let decoder = decoder as? _PklDecoder else {
fatalError("\(Self.self) can only be decoded using \(_PklDecoder.self), but was: \(decoder)")
}
self = try Self.decodeValueUnitType(from: decoder.value, at: decoder.codingPath)
}
}

extension PklSerializableValueUnitType {
static func decodeValueUnitType(
from value: MessagePackValue,
at codingPath: [CodingKey]
) throws -> Self {
guard case .array(let arr) = value else {
let codingPath = decoder.codingPath
guard case .array(let arr) = decoder.value else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Expected array but got \(value.debugDataTypeDescription)"
debugDescription: "Expected array but got \(decoder.value.debugDataTypeDescription)"
))
}
let code = try arr[0].decode(PklValueType.self)
guard arr.count > 0 else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Expected non-empty array"
))
}
guard Self.messageTag == code else {
throw DecodingError.dataCorrupted(
.init(codingPath: codingPath, debugDescription: "Cannot decode \(code) into \(Self.self)"))
}

let value = try arr[1].decode(Self.ValueType.self)
let unit = try arr[2].decode(Self.UnitType.self)
self = try Self.decode(arr, codingPath: codingPath)
}
}

extension Decodable where Self: PklSerializableValueUnitType {
public static func decode(_ fields: [MessagePackValue], codingPath: [any CodingKey]) throws -> Self {
try checkFieldCount(fields, codingPath: codingPath, min: 3)
let value = try fields[1].decode(Self.ValueType.self)
let unit = try fields[2].decode(Self.UnitType.self)
return Self(value, unit: unit)
}
}
Expand Down Expand Up @@ -237,6 +257,15 @@ extension _PklDecoder {
case .dataSize:
let decoder = try _PklDecoder(value: propertyValue)
return try PklAny(value: DataSize(from: decoder))
case .pair:
let decoder = try _PklDecoder(value: propertyValue)
return try PklAny(value: Pair<AnyHashable?, AnyHashable?>(from: decoder))
case .regex:
let decoder = try _PklDecoder(value: propertyValue)
return try PklAny(value: PklRegex(from: decoder))
case .intSeq:
let decoder = try _PklDecoder(value: propertyValue)
return try PklAny(value: IntSeq(from: decoder))
case .bytes:
guard case .bin(let bytes) = value[1] else {
throw DecodingError.dataCorrupted(
Expand Down
6 changes: 6 additions & 0 deletions Tests/PklSwiftTests/Fixtures/AnyType.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ nullable: Any = null
duration: Any = 5.min

dataSize: Any = 10.mb

pair: Any = Pair(1, 2)

regex: Any = Regex("abc")

seq: Any = IntSeq(0, 10).step(2)
24 changes: 22 additions & 2 deletions Tests/PklSwiftTests/Fixtures/ApiTypes.pkl
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
res1: Duration = 10.h
res2: DataSize = 1.2345.gib
dur: Duration = 20.h

data: DataSize = 2.4680.gib

pair1: Pair = Pair("a", "b")
pair2: Pair<String, Int> = Pair("a", 1)
pair3: Pair<String, Int?> = Pair("a", 1)
pair4: Pair<String, Int?> = Pair("a", null)

// pairMapping1: Mapping<Pair, Any>
// pairMapping2: Mapping<Pair<String, Int>, Any>

pairListing1: Listing<Pair> = new {
pair2
pair3
pair4
}
pairListing2: Listing<Pair<String, Int?>> = pairListing1

regex: Regex = Regex("def")

seq: IntSeq = IntSeq(0, 20).step(3)
19 changes: 17 additions & 2 deletions Tests/PklSwiftTests/Fixtures/Generated/AnyType.pkl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ extension AnyType {

public var dataSize: AnyHashable?

public var pair: AnyHashable?

public var regex: AnyHashable?

public var seq: AnyHashable?

public init(
bird: AnyHashable?,
primitive: AnyHashable?,
Expand All @@ -34,7 +40,10 @@ extension AnyType {
mapping: AnyHashable?,
nullable: AnyHashable?,
duration: AnyHashable?,
dataSize: AnyHashable?
dataSize: AnyHashable?,
pair: AnyHashable?,
regex: AnyHashable?,
seq: AnyHashable?
) {
self.bird = bird
self.primitive = primitive
Expand All @@ -45,6 +54,9 @@ extension AnyType {
self.nullable = nullable
self.duration = duration
self.dataSize = dataSize
self.pair = pair
self.regex = regex
self.seq = seq
}

public init(from decoder: Decoder) throws {
Expand All @@ -58,7 +70,10 @@ extension AnyType {
let nullable = try dec.decode(PklSwift.PklAny.self, forKey: PklCodingKey(string: "nullable")).value
let duration = try dec.decode(PklSwift.PklAny.self, forKey: PklCodingKey(string: "duration")).value
let dataSize = try dec.decode(PklSwift.PklAny.self, forKey: PklCodingKey(string: "dataSize")).value
self = Module(bird: bird, primitive: primitive, primitive2: primitive2, array: array, set: set, mapping: mapping, nullable: nullable, duration: duration, dataSize: dataSize)
let pair = try dec.decode(PklSwift.PklAny.self, forKey: PklCodingKey(string: "pair")).value
let regex = try dec.decode(PklSwift.PklAny.self, forKey: PklCodingKey(string: "regex")).value
let seq = try dec.decode(PklSwift.PklAny.self, forKey: PklCodingKey(string: "seq")).value
self = Module(bird: bird, primitive: primitive, primitive2: primitive2, array: array, set: set, mapping: mapping, nullable: nullable, duration: duration, dataSize: dataSize, pair: pair, regex: regex, seq: seq)
}
}

Expand Down
Loading