How use pkl with a interface which holds a config for a repository configuration #193
-
Hello, I am very new to PKL and as such I might be missing something here. I am writing a program which takes in a configuration that uses an interface to abstract the config of datasources. Datasources are configuration entities for clients to write to different databases. e.g. influxdb, prom, clickhouse. Config Example: outputs:
- name: "influx2"
type: "influxdb_v1"
config:
host: http://localhost:8086
database: slo
- name: "prometheus"
type: "prometheus"
config:
url: http://localhost:9090/metrics My structs are: type Configuration struct {
Inputs []Datasource `pkl:"inputs"`
Outputs []Datasource `pkl:"outputs"`
}
type Datasource struct {
Name string `pkl:"name"`
Type string `pkl:"type"`
// Config is a datasource interface that can be used to store any configuration
// this is defined by the type of the datasource
Config interface{} `pkl:"config"`
}
// Database Configuration
type InfluxV1Configuration struct {
Host string `yaml:"host"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Database string `yaml:"database"`
} This is where Datasource.Config can equal InfluxV1Configuration or another kind of struct. My code when loading: config := entities.Configuration{}
ctx := context.Background()
evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions)
if err != nil {
return config, err
}
defer func() {
cerr := evaluator.Close()
if err == nil {
err = cerr
}
}()
if err := evaluator.EvaluateModule(ctx, pkl.FileSource(file), &config); err != nil {
return config, err
}
fmt.Printf("config: %+v\n", config)
os.Exit(0) Error: This is because config is an interface (tested by removing it). So thoughts on how I go about sorting this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Gave up and used code gen which after tinkering seemed to work. Final output: spec.pkl
generate: You can then use a config like so:
The bit that got me was that |
Beta Was this translation helpful? Give feedback.
Gave up and used code gen which after tinkering seemed to work.
Final output:
spec.pkl