mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Why does LoadFromPath() function of generated .pkl.go file needs .pkl file at runtime when there is already generated .pkl.go files. #128
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @hnrNeha on GitHub (Mar 28, 2024).
Iam using pkl files for config and generated go files for it. In the process of loading config Iam using LoadFromPath() this function expects a pkl file as parameter so for this I need to have pkl executable in my system.My query is when we are already generating go file from pkl why do we need to give pkl file path again.Is there any way to load pkl files from generated code without giving path to .pkl file??
@holzensp commented on GitHub (Mar 28, 2024):
The generated Go code is the structure of the configuration (the schema, or template). It does not contain the values of the configuration. Those, you load at runtime (so that you can change configuration out-of-sync with your application code).
@bioball commented on GitHub (Mar 28, 2024):
Phil is right about the generated code only being the structure, and not the values.
With that said, though, there are a couple options that you have to embed values from Pkl into your application.
Embed pkl files
One option is to use
go:embedto embed those Pkl files as anembed.FS. You can then evaluate those modules straight from the embedded file system. Here is a commit that demonstrates that:3b843c3966The benefit of this approach is that because Pkl happens at runtime, you can pass in external variables into the Pkl program. For example, you can pass in environment variables and use
read("env:")within Pkl to read those environment variables. This is especially helpful for things like reading in secret values into Pkl.The drawback is that you still need to include the
pklCLI in the deployed environment of your application.Pre-evaluate Pkl into binary
If you really don't want to include the
pklCLI at runtime, there's another option here, which is to first evaluate Pkl into a binary format, and then embed that binary into your application. Then, you can usepkl.Unmarshalto turn that binary into your app config.That approach is demonstrated here:
841315bc14