Open HT154 opened 4 months ago
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:
@JsonPropertyName { value = "my-foo" }
myFoo: String
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.
I'm back to thinking about this some more. There seems to be a few problems this has potential to solve:
Duration
is 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.
Currently, annotations are a helpful language feature for modules using
pkl:reflect
and 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 forValueRenderer
converters.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
/availableAfter
areconst
, thentargetVersion
must 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
MyServiceConfig
will 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.