mirror of
https://github.com/apple/pkl.git
synced 2026-01-13 15:13:38 +01:00
Output converters should allow selection for properties by Annotation #181
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 @HT154 on GitHub (Jul 12, 2024).
Currently, annotations are a helpful language feature for modules using
pkl:reflectand tooling like IDEs, but they're not broadly useful in most modules. One way they could be made more useful is through supporting them as a type of match forValueRendererconverters.Background
Here's an example where current language features fail to fully meet a module's needs. Take a module that has been adapted to validate property availability in multiple versions of a service:
The above code works just fine, but problems arise the moment this needs to be used outside the module's top level:
This produces an error:
Neither of the proposed solutions work here.
availableBefore/availableAfterareconst, thentargetVersionmust also beconst, but this design specifically requires that amending modules be able to set atargetVersion.One workaround today might be to define an output converter to handle this:
The major downside here is that this separates the information about availability from the property definition and assumes that
MyServiceConfigwill always be the root module being evaluated. This is particularly fatal in cases like Kubernetes configurations where there are many fields in many modules, some of which may not be known to the parent module at time of writing.Proposal
My proposed solution here is to allow converters to match on annotation classes. The converter function would need to be passed both the annotation value and the underlying property value, so it would either need to be allowed to be a
Function2<«annotation value», «property value», Any>or be passed aPair<«annotation value», «property value»>(or possibly some new class eg.ValueRendererConverterMatchAnnotation).This would make annotations broadly useful in a variety of scenarios, including the versioning example above. Here's how that example might look if this were implemented:
Modules embedding this one would need to copy its converters to inherit this behavior, but that's very doable since this is portable and not dependent on property key paths like the workaround above.
@bioball commented on GitHub (Jul 22, 2024):
Generally, this feature makes sense to me. However, I don't think that this contributes to the validation story--output converters generally are not the right place to apply validation because they don't apply to the library use-case (e.g. evaluating Pkl into Java/Go/Swift structs).
It would, however, be useful for transformation, because it helps untether a dependency between a template and a renderer.
For example, something like:
I'd encourage that you write a SPICE here, and perhaps even pair it with an implementation and it can be discussed in detail there.
@HT154 commented on GitHub (Sep 16, 2024):
I'm back to thinking about this some more. There seems to be a few problems this has potential to solve:
Durationis a good example, where it would be nice to annotate a property with eg.@DurationInt { unit = "s" }to force rendering as an integer number of seconds.This last use case currently seems like the most compelling motivation for this feature as-proposed, though having better control over property keys during rendering is definitely valuable separately.
@HT154 commented on GitHub (Nov 29, 2025):
Now that there's an in-language API for pkl-binary, this feature actually could be used to generally solve the validation problem!
Having now implemented a prototype of this feature, I think it makes sense to also accept the property's name in the converter function and expect a
Pair(of the converted property name and value) to be returned.Evaluator API clients would use the "evaluate output bytes" method and then decode/unmarshal the result into native types as usual. This would not bypass the module's configured renderer and instead would allow annotation converters to perform the validation this issue originally sought.