-
I'm trying to figure out how to prevent the rendering of a class property that is either empty or null. Basically I have an optional property I'd prefer not to be rendered if it's not set, but if it is set I want it to be one of a defined list of possible values.
I'd like the resulting default output to end up being:
I'd like to not need to write a custom renderer for the entire class. Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Null is not rendered by default. So if you go with null as default, then it just works. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the lead @StefMa. What I ended up doing was:
That at least evaluated. The odd thing was running I didn't think that what I did was valid syntax - at least that's not what I got from the documentation on what "?" does. I thought it was syntactic sugar for performing operations on a nullable property. I guess it's also a way to define that a property is nullable. |
Beta Was this translation helpful? Give feedback.
-
The syntax for declaring nullable properties is documented here: https://pkl-lang.org/main/current/language-reference/index.html#nullable-types Typically, when you declare an enum (well, really a literal string union) type property, you'd typically write it like this: value: ("a"|"b"|"c")? For nullable properties, the default value is implicitly null. Finally, whether or not null values are rendered in the output is a property of the renderer you're using, typically via a property on it called output {
// always render as YAML omitting null property values
renderer = new YamlRenderer {
omitNullProperties = true
}
} You can read more about configuring module output in-language here: https://pkl-lang.org/main/current/language-reference/index.html#in-language |
Beta Was this translation helpful? Give feedback.
The syntax for declaring nullable properties is documented here: https://pkl-lang.org/main/current/language-reference/index.html#nullable-types
Typically, when you declare an enum (well, really a literal string union) type property, you'd typically write it like this:
For nullable properties, the default value is implicitly null.
Finally, whether or not null values are rendered in the output is a property of the renderer you're using, typically via a property on it called
omitNullProperties
. ForPcfRenderer
this defaults tofalse
(so null values are present in the output) but forJsonRenderer
andYamlRenderer
the default is true. If you care strongly about the output…