PKL write api #353

Open
opened 2025-12-30 01:23:47 +01:00 by adam · 2 comments
Owner

Originally created by @casperwtf on GitHub (Nov 1, 2025).

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?

Originally created by @casperwtf on GitHub (Nov 1, 2025). 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?
Author
Owner

@HT154 commented on GitHub (Nov 1, 2025):

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
@HT154 commented on GitHub (Nov 1, 2025): 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: ```yaml # defaults.yaml foo: 123 bar: abc ``` ```pkl // 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`](https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax/current/index.html) 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
Author
Owner

@casperwtf commented on GitHub (Nov 20, 2025):

I am on java, is there any java equivalent

@casperwtf commented on GitHub (Nov 20, 2025): I am on java, is there any java equivalent
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#353