apple / pkl

A configuration as code language with rich validation and tooling.
https://pkl-lang.org
Apache License 2.0
9.84k stars 259 forks source link

Use pkl to validate existing config files #550

Closed derphilipp closed 1 week ago

derphilipp commented 1 week ago

Is it possible to achive:

This way golang programs could harness the power of pkl without modifying the user expirience / config files of existing tools

holzensp commented 1 week ago

[aside: I'd prefer questions like this under Discussions]

Yes. You don't even need to write a Go program for the validation. That said, I think our editing experience is better than most JSON experiences. I guess "existing tools" might be a deal-breaker. Anyway, yes, you can always add the following to any template;

import "pkl:json"
import "package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1.0.2#/deepToTyped.pkl"

output {
  value = deepToTyped.apply(module.getClass(), new json.Parser {}.parse(read(read("prop:JsonPath"))))
}

Now, if you invoke Pkl with a value for external property JsonPath to point to your .json file, you can consume output.value as an instance of the module it's defined in.

derphilipp commented 1 week ago

Thank you very much and you are absolutely right, this should be under 'Discussion'!