mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Templating with classes does not fail if expected #100
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @StefMa on GitHub (Mar 2, 2024).
Given a similar template as you have in the documentation "How to write a template".
The implementation leaves out the
eventproperty:Expected:
Error with message
But I get:
Not error, outpout contains
For me looks wrong.
event: Eventfrom the template is not overriden so it should fail.Fun fact:
If I leave a property within
Eventnon-nullable, it will fail with the correct error message:Another (simpler) example:
Given the following template:
My template expect to override
On.However, non of the properties of
Onare required to set.Implementation:
☝️ No joke, just the
amendstatement of course 😁What will be rendered?
Technically this behaviour seems to be correct.
It might be the case that
pklwill "simply" add the "null class" for you to the implementation.Because not defining
onat all or usingon {}will lead to the same output.The latter (
on {}) is perfectly fine and shouldn't print an error,Nevertheless, I think we should fail here because it behaves differently to other errors that says "value is undefined".
@stackoverflow commented on GitHub (Mar 4, 2024):
Most non-primitive types in Pkl have a default value. The default value of nullable types is
null.If you want to force people to give a value, you have two options:
first: Int? = throw("error: no value provided for property 'first'")@StefMa commented on GitHub (Mar 4, 2024):
Hey @stackoverflow ,
thanks for the answer.
How do I mark a property as
non-null?Isn't declaring it as
on: Onenough?In fact, I have this!
See
d266b8b2f9/GitHubAction.pkl (L139-L149)It also works with build-in classes like

StringorInt.In these cases it works:
However,

onandjobswill be rendered empty...Package used:
@stackoverflow commented on GitHub (Mar 4, 2024):
Here's a list of default values for different types, in case you are confused: https://pkl-lang.org/main/current/language-reference/index.html#default-values
Primitive types like
String,Int,Boolean, etc have no default values. That's why you get an error if you don't provide them.@bioball commented on GitHub (Mar 4, 2024):
Is this invalid output?
If this is valid, then there is nothing to do here.
If it this invalid, then you will want to think about what the validation rules are, and define them in your template.
For example, maybe you want to say that at least one sub-property is defined. If so, then you can do:
Then, Pkl will throw if some property is not defined.
Alternatively, if
on: {}is valid, but you would rather omit this from your output altogether if it's empty. In that case, you can just make the property nullable, and have your renderer omit null properties.For the YAML use-case, it already does omit null properties so nothing else needs to be set.
@StefMa commented on GitHub (Mar 4, 2024):
@stackoverflow I was not aware of the concept of default properties 🤯 thanks for sharing! Know it make sense!
@bioball thanks for the explanation!
Indeed,
on {}without a single property set is invalid. Seems I wanna introduce a logic you provided.Thanks for your support here guys.
Much appreciated.
Going to close it as it everything works as expected.