cleanenv icon indicating copy to clipboard operation
cleanenv copied to clipboard

Apply Custom Value Setters to custom struct types

Open byted opened this issue 2 years ago • 0 comments

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?

byted avatar Oct 27 '22 16:10 byted