SPICE-0024: Annotation converters (#1333)

This enables defining declarative key and/or value transformations in
cases where neither `Class`- nor path-based converters can be applied
gracefully. It is also the only way to express transforming the
resulting property names in `Typed` objects without applying a converter
to the entire containing type, which is cumbersome at best.

SPICE: https://github.com/apple/pkl-evolution/pull/26
This commit is contained in:
Jen Basch
2026-01-23 12:44:41 -08:00
committed by GitHub
parent ed0cad668f
commit 73264e8fd1
51 changed files with 773 additions and 141 deletions

View File

@@ -68,6 +68,16 @@ function ExtVar(_name: String): ExtVar = new { name = _name }
/// }
/// }
/// ```
///
/// The [Property] annotation can be used to change how a property name renders into Jsonnet.
///
/// Example:
/// ```
/// import "pkl:jsonnet"
///
/// @jsonnet.Property { name = "wing_span" }
/// wingSpan: Int
/// ```
class Renderer extends ValueRenderer {
extension = "jsonnet"
@@ -91,6 +101,16 @@ class Renderer extends ValueRenderer {
external function renderValue(value: Any): String
}
/// Annotate properties of classes and modules with this class to override how a [Renderer]
/// interprets a property's name.
@Since { version = "0.31.0" }
class Property extends ConvertProperty {
/// The new name to use for the annotated property when rendered by [Renderer].
name: String
render = (prop, renderer) -> if (renderer is Renderer) Pair(name, prop.value) else prop
}
/// An `importstr` construct that, when evaluated by Jsonnet, returns the content of a UTF-8 text file.
///
/// To construct an [ImportStr], use method [ImportStr()].