Skip to content

Commit

Permalink
Add an attemptApply function to deepToTyped (#73)
Browse files Browse the repository at this point in the history
Sometimes you want to be able to collect multiple conversion errors from
multiple deepToTyped.apply calls, this change makes that possible.
  • Loading branch information
thomaspurchas authored Jul 15, 2024
1 parent 3b7fe4e commit 6fd441e
Show file tree
Hide file tree
Showing 6 changed files with 15 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 = "1.0.10"
version = "1.0.11"
}
2 changes: 1 addition & 1 deletion packages/k8s.contrib.crd/PklProject.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.3",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.4",
"path": "../pkl.experimental.deepToTyped"
},
"package://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1": {
Expand Down
2 changes: 1 addition & 1 deletion packages/pkl.experimental.deepToTyped/PklProject
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
amends "../basePklProject.pkl"

package {
version = "1.0.3"
version = "1.0.4"
}
15 changes: 10 additions & 5 deletions packages/pkl.experimental.deepToTyped/deepToTyped.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ import "pkl:reflect"
/// local dynamicBar = new Dynamic { foo { x = 1 } }
/// apply(Bar, dynamicBar) == new Bar { foo = new Foo { x = 1 } }
/// ```
function apply(type: Class|TypeAlias, value: Any) =
function apply(type: Class|TypeAlias, value: Any): Any =
let (result =
if (type is Class)
applyClass(reflect.Class(type), List(), value)
else
applyType(reflect.TypeAlias(type).referent, value)
attemptApply(type, value)
)
if (result is ConversionFailure) throw(result.message) else result

/// Same as [apply], but returns [ConversionFailure] rather than throwing.
function attemptApply(type: Class|TypeAlias, value: Any): Any|ConversionFailure =
if (type is Class)
applyClass(reflect.Class(type), List(), value)
else
applyType(reflect.TypeAlias(type).referent, value)


local class ConversionFailure {
message: String

Expand Down
2 changes: 1 addition & 1 deletion packages/pkl.experimental.structuredRead/PklProject
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ amends ".../basePklProject.pkl"


package {
version = "1.0.0"
version = "1.0.1"
}

dependencies {
Expand Down
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.deepToTyped@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.3",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.4",
"path": "../pkl.experimental.deepToTyped"
}
}
Expand Down

0 comments on commit 6fd441e

Please sign in to comment.