Skip to content

v6.1.0: Custom and external readers for modules and resources

Latest

Choose a tag to compare

@jdx jdx released this 29 Nov 14:42
· 21 commits to main since this release
Immutable release. Only release title and notes can be modified.
c10a6e6

Hosts can now serve modules and resources under their own URI schemes, either in process or through Pkl external reader processes. This release also fixes four evaluation bugs and the build on 32-bit targets without 64-bit atomics.

Added

  • Module and resource readers for custom URI schemes (#495) @jdx. A host can register a reader for its own scheme. Configs can then import, amends and read() from that scheme without writing files to disk.
    pklr::EvaluatorBuilder::new()
        .module_reader("myapp", |uri: &str| -> pklr::Result<Option<String>> {
            Ok(match uri {
                "myapp:/Config.pkl" => Some(include_str!("Config.pkl").to_string()),
                _ => None,
            })
        })
        // A config can now `amends "myapp:/Config.pkl"`.
        .eval_to_json(std::path::Path::new("config.pkl"))
    • EvaluatorBuilder::module_reader and resource_reader accept a ModuleReader or ResourceReader, and closures work too. Evaluator::set_module_reader and set_resource_reader do the same on an existing evaluator.
    • Module URIs must be hierarchical (myapp:/a/b.pkl). Relative imports, triple-dot imports, dot-segment normalization, the module allowlist and the import cache all work as they do for files. Opaque URIs such as myapp:Config are rejected with an error that shows the hierarchical form.
    • Resource reader schemes are added to the default allowed resources, as in Pkl. If you set allowed_resources yourself, your list is used as is.
    • Built-in schemes can't be replaced. If a reader is registered for an invalid scheme, every evaluation fails with a clear error.
    • With the new opt-in external-readers feature, external_module_reader(scheme, exe, args) and external_resource_reader(...) run a reader process that speaks Pkl's message passing protocol. The process starts on first use and is shared by one command. pklr::external_reader also provides the message codec (Message, read_message, write_message), so you can write a reader that pkl eval --external-module-reader can also run. The codec has been checked against the spec but not yet against the real pkl CLI.

Fixed

  • Undeclared properties are now rejected on every amendment of a non-open class (#496) @jdx. A typo such as aprove = true used to appear in the output without an error when it was set in a typed Mapping<String, N> entry, an amend expression (a) { ... }, a nested body, or an amending module. These cases now fail with cannot add property 'aprove' to non-open class N, the same error that new N { ... } already gave. Open classes, Dynamic, untyped values, and inherited, hidden and local members are still accepted. Compatibility note: configs that relied on this silent acceptance now fail. Declare the property on the class or remove it.
  • List elements repeated through amends chains (#497) @jdx. If a module amended a Mapping entry's listing (for example m { ["a"] { also { "x"; "y" } } }), a module that amended it again repeated the elements: three times through one wrapper module and five times through two. The elements now appear once at every depth. A listing that reads a member overridden by a later module now uses the override.
  • read? of a missing resource could return another reader's cached value (#498) @jdx. A missing resource now always reads as null.
  • Hidden members assigned inside a when generator showed up in the output (#499) @jdx. With class C { hidden h: Int = 1; y: Int = h }, new C { when (true) { h = 5 } } rendered {"h": 5, "y": 1}. It now renders {"y": 5}, the same as new C { h = 5 }.
  • Build failure on 32-bit targets without 64-bit atomics (#486) @jdx. pklr now compiles on targets such as 32-bit PowerPC and MIPS. The build failure was reported while building hk through MacPorts. The FetchBudget API and its behavior are unchanged.

Full Changelog: v6.0.4...v6.1.0