Skip to content

Commit

Permalink
[pkl.experimental.deepToTyped] Add support for key transformations du…
Browse files Browse the repository at this point in the history
…ring apply (#88)
  • Loading branch information
HT154 authored Dec 13, 2024
1 parent 267ad65 commit 90b75bf
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 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.13"
version = "1.0.14"
}
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.5",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.1.0",
"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.5"
version = "1.1.0"
}
9 changes: 6 additions & 3 deletions packages/pkl.experimental.deepToTyped/deepToTyped.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@ local function getAllProperties(clazz: reflect.Class?): Map<String, reflect.Prop
else if (doesNotInherit(clazz)) clazz.properties
else getAllProperties(clazz.superclass!!) + clazz.properties

hidden keyTransform: Function1<reflect.Property, String>?

local function applyProperty(valueAsMap: Map, prop: reflect.Property) =
if (valueAsMap.containsKey(prop.name)) applyType(prop.type, valueAsMap[prop.name])
else if (!(prop.type is reflect.NullableType) && prop.defaultValue != null) prop.defaultValue
else null
let (propName = keyTransform?.ifNonNull((it) -> (it as Function1<reflect.Property, String>).apply(prop)) ?? prop.name)
if (valueAsMap.containsKey(propName)) applyType(prop.type, valueAsMap[propName])
else if (!(prop.type is reflect.NullableType) && prop.defaultValue != null) prop.defaultValue
else null

local function applyDynamicOrMapping(type: reflect.Class, value: Dynamic|Mapping): Typed|ConversionFailure =
let (valueAsMap = value.toMap())
Expand Down
35 changes: 35 additions & 0 deletions packages/pkl.experimental.deepToTyped/tests/deepToTyped.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ local class LooselyTyped {
anything: Any
}

local class KeyTransform extends Annotation {
name: String
}

local class ClassWithKeyTranform {
@KeyTransform { name = "my_field" }
myField: String
inner: InnerClassWithKeyTranform
}

local class InnerClassWithKeyTranform {
@KeyTransform { name = "my_nested_field" }
myNestedField: String
}

facts {
["Basic types"] {
t.apply(Int, 1) == 1
Expand Down Expand Up @@ -318,4 +333,24 @@ facts {
}
t.apply(LooselyTyped, looselyTypedString) == looselyTypedStringExpected
}

["Supports key transformations, eg. via annotation"] {
local input = new Dynamic {
my_field = "hello"
inner {
my_nested_field = "world"
}
}

local expectedClassWithKeyTranform = new ClassWithKeyTranform {
myField = "hello"
inner {
myNestedField = "world"
}
}

new t {
keyTransform = (prop) -> prop.annotations.findOrNull((it) -> it is KeyTransform)?.name
}.apply(ClassWithKeyTranform, input) == expectedClassWithKeyTranform
}
}
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.2"
version = "1.0.3"
}

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.5",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.1.0",
"path": "../pkl.experimental.deepToTyped"
}
}
Expand Down

0 comments on commit 90b75bf

Please sign in to comment.