Skip to content

Apply Custom Value Setters to custom struct types #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
byted opened this issue Oct 27, 2022 · 0 comments · May be fixed by #156
Open

Apply Custom Value Setters to custom struct types #106

byted opened this issue Oct 27, 2022 · 0 comments · May be fixed by #156

Comments

@byted
Copy link

byted commented Oct 27, 2022

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?

martinohansen added a commit to martinohansen/cleanenv that referenced this issue Mar 28, 2025
If the user do not own the custom type they have to implement Setter on
a wrapper type to be able to use it with ReadEnv.

Fixes ilyakaznacheev#106
@martinohansen martinohansen linked a pull request Mar 28, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant