Skip to content

Commit

Permalink
[pkl.experimental.syntax] Support default types in UnionTypeNode (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
HT154 authored Mar 5, 2025
1 parent c4a51bb commit f4bdd6d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/k8s.contrib.crd/PklProject
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ dependencies {
}

package {
version = "2.0.1"
version = "2.0.2"
}
4 changes: 2 additions & 2 deletions packages/k8s.contrib.crd/PklProject.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
},
"package://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1.1.3",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1.1.4",
"path": "../org.json_schema.contrib"
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1.0.3",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1.1.0",
"path": "../pkl.experimental.syntax"
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.uri@1": {
Expand Down
2 changes: 1 addition & 1 deletion packages/org.json_schema.contrib/PklProject
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ dependencies {
}

package {
version = "1.1.3"
version = "1.1.4"
}
2 changes: 1 addition & 1 deletion packages/org.json_schema.contrib/PklProject.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"resolvedDependencies": {
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1.0.3",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1.1.0",
"path": "../pkl.experimental.syntax"
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.uri@1": {
Expand Down
4 changes: 2 additions & 2 deletions packages/pkl.experimental.syntax/PklProject
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
// 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.
Expand All @@ -17,6 +17,6 @@
amends "../basePklProject.pkl"

package {
version = "1.0.3"
version = "1.1.0"
apiTests = import*("tests/*.pkl").keys.toListing()
}
14 changes: 12 additions & 2 deletions packages/pkl.experimental.syntax/TypeNode.pkl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
// 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.
Expand All @@ -20,6 +20,7 @@ extends "Node.pkl"
import "QualifiedIdentifierNode.pkl"
import "ExpressionNode.pkl"
import "TypeNode.pkl"
import "Node.pkl"

class NullableTypeNode extends TypeNode {
typeNode: TypeNode
Expand Down Expand Up @@ -65,8 +66,17 @@ class ConstrainedTypeNode extends TypeNode {
renderedUnderlyingType + renderConstraints(currentIndent)
}

class UnionDefaultType extends Node {
typeNode: TypeNode

function render(currentIndent: String) = "*\(typeNode.render(currentIndent))"
}

class UnionTypeNode extends TypeNode {
members: Listing<TypeNode>
members: Listing<TypeNode|UnionDefaultType>(atMostOneUnionDefaultType)

const local atMostOneUnionDefaultType = (members: Listing<TypeNode|UnionDefaultType>) ->
members.fold(0, (acc, member) -> acc + if (member is UnionDefaultType) 1 else 0) <= 1

function render(currentIndent: String) =
let (childrenRendered = members.toList().map((t) -> t.render(currentIndent)))
Expand Down
34 changes: 33 additions & 1 deletion packages/pkl.experimental.syntax/tests/TypeNode.pkl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
// 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.
Expand Down Expand Up @@ -106,5 +106,37 @@ facts {
}.render("") == """
"one"|"two"
"""

new TypeNode.UnionTypeNode {
members {
new TypeNode.UnionDefaultType {
typeNode = new TypeNode.StringLiteralTypeNode {
value = "one"
}
}
new TypeNode.StringLiteralTypeNode {
value = "two"
}
}
}.render("") == """
*"one"|"two"
"""

module.catch(() ->
new TypeNode.UnionTypeNode {
members {
new TypeNode.UnionDefaultType {
typeNode = new TypeNode.StringLiteralTypeNode {
value = "one"
}
}
new TypeNode.UnionDefaultType {
typeNode = new TypeNode.StringLiteralTypeNode {
value = "two"
}
}
}
}.render("")
).startsWith("Type constraint `atMostOneUnionDefaultType` violated.")
}
}

0 comments on commit f4bdd6d

Please sign in to comment.