Skip to content

Instantly share code, notes, and snippets.

@bioball
Last active June 21, 2024 20:05
Show Gist options
  • Save bioball/8fcc848cffdf11fcbce68e420647aa1e to your computer and use it in GitHub Desktop.
Save bioball/8fcc848cffdf11fcbce68e420647aa1e to your computer and use it in GitHub Desktop.
Pkl multiple inheritance example
module com.example.AppConfig
// self import
import "AppConfig.pkl"
/// The profile to use.
local profile = read?("prop:profile") as ("dev"|"prod")?
/// The modifier to apply, if any.
local modifier = read?("prop:modifier") as ("canary"|"ci")?
local includes: Listing<AppConfig> = new {
import("baseConfig.pkl")
when (profile == "dev") {
import("profile-dev.pkl")
}
when (profile == "prod") {
import("profile-prod.pkl")
}
when (modifier == "ci") {
import("modifier-ci.pkl")
}
when (modifier == "canary") {
import("modifier-canary.pkl")
}
// Import ../dev/config.pkl if it exists.
when (read?("../dev/config.pkl") != null) {
import("../dev/config.pkl")
}
}
hidden config: Mixin<Config>
/// The actual configuration value
fixed value: Config = includes.fold(new Config {}, (result, cfg) -> cfg.appConfig.apply(result))
/// The underlying configuration
class Config {
hosts: Listing<String>
port: UInt16
ttl: Duration
}
amends "AppConfig.pkl"
config {
ttl = 3.h
port = 5122
}
amends "AppConfig.pkl"
config {
hosts {
"canary.example.com"
}
}
amends "AppConfig.pkl"
config {
hosts {
"ci.example.com"
}
}
amends "AppConfig.pkl"
config {
hosts {
"dev.example.com"
}
ttl = 5.min
}
amends "AppConfig.pkl"
config {
hosts {
"prod.example.com"
}
ttl = 5.h
}
@EugZol
Copy link

EugZol commented Jun 21, 2024

Line 35 of AppConfig.pkl should now read:

fixed value: Config = includes.fold(new Config {}, (result, cfg) -> result |> cfg.config)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment