PKL write api
Hi, I have PKL as a file on disk, I want to have my application write default values to the PKL files at a given path, is there an api for that?
Hi there! This is an area we've given some thought to. There are some challenges due to Pkl's more dynamic features like generators and functions that present obstacles to reading, transforming, and writing Pkl files.
That said, there some techniques you can use that may work for your needs:
Reading values from static files
You can write your set of default values in a static format like YAML or JSON and read this data in your Pkl code. For example:
# defaults.yaml
foo: 123
bar: abc
// defaults.pkl
amends "schema.pkl"
import "pkl:yaml"
const local defaults =
new yaml.Parser { useMapping = true }
.parse(read("defaults.yaml")) as Mapping
foo = defaults["foo"] as Int
bar = defaults["bar"] as String
Depending on your scenario, you may even be able to read the YAML/JSON data over HTTPS instead of needing to write it to a local file.
Generate an entire Pkl file
The Pkl team maintains a library called pkl.experimental.syntax that can be used to generate Pkl code from an in-language representation of the syntax tree. While there's no way to read an existing Pkl file into this representation, it still may be useful to you if you structure your modules effectively:
- A schema module declares your types
- A defaults module amends the schema module and is generated code
- A user module amends the defaults module and and is user-maintained
I am on java, is there any java equivalent