ilyakaznacheev / cleanenv

✨Clean and minimalistic environment configuration reader for Golang
MIT License
1.67k stars 116 forks source link

Apply Custom Value Setters to custom struct types #106

Open byted opened 2 years ago

byted commented 2 years ago

I realized that a SetValue() defined on a custom struct type is never called. Insted, the struct is flattened. This prevents me from parsing e.g., a yaml string from an ENV variable:

type Config struct {
    Complex ComplexYamlConfig `env:"COMPLEX_CONFIG"`
}

type ComplexYamlConfig struct {
    Left  string `yaml:"myProp"`
}

func (c *ComplexYamlConfig) SetValue(s string) error {
        // will never be called by cleanenv
    return yaml.Unmarshal([]byte(s), c)
}

Is this something that can be added? Or is there a different way of achieving the same thing?