When using a non-struct pointer value, we cannot pass in existing data.
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/caarlos0/env/v6"
"k8s.io/utils/pointer"
)
type Test struct {
// Bool *bool `env:"TEST"`
Str *string `env:"TEST"`
}
type ComplexConfig struct {
Foo Test `envPrefix:"FOO_"`
Bar Test `envPrefix:"BAR_"`
D Test `envPrefix:"D_"`
}
func main() {
os.Setenv("FOO_TEST", "1")
os.Setenv("BAR_TEST", "0")
cfg := ComplexConfig{
D: Test{
Str: pointer.String("hello"),
// Bool: pointer.Bool(true),
},
}
err := env.Parse(&cfg)
if err != nil {
log.Fatal(err)
}
confBytes, _ := json.Marshal(cfg)
fmt.Printf("%s", confBytes)
}
Result
env: env: expected a pointer to a Struct
It works fine if the cfg variable had nothing set for D (ie, cfg := ComplexConfig{}):
{"Foo":{"Str":"1"},"Bar":{"Str":"0"},"D":{"Str":null}}
The use-case for this is to use it with our Installer to fill in the configuration
When using a non-struct pointer value, we cannot pass in existing data.
Result
It works fine if the
cfgvariable had nothing set forD(ie,cfg := ComplexConfig{}):{"Foo":{"Str":"1"},"Bar":{"Str":"0"},"D":{"Str":null}}The use-case for this is to use it with our Installer to fill in the configuration