Skip to content

Injecting data and objects #45

Answered by bioball
obj-p asked this question in Q&A
Discussion options

You must be logged in to vote

There's kind of two approaches here that I can think of.

One is to use external properties to make values available to Pkl:

foo = read?("prop:foo") ?? 15

You can use a non-nullable read too if the prop is required:

foo = read("prop:foo")

Or if you want to get all properties, you can use a globbed read:

allProps = read*("prop:**")
foo = allProps["prop:foo"]

Another approach is, if you need to ad-hoc change a value that isn't defined in terms of read("prop:"), is to evaluate an expression that changes a value:

// someModule.pkl
foo = 15
pkl eval someModule.pkl -x "(module) { foo = 35 }"

There's the equivalent of pkl eval -x in the Java API, and also in the Gradle plugin.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by obj-p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants