Support for yaml.v3
Opened this issue · 0 comments
In my application I'm using yaml.v3 instead of yaml.v2, which confita uses. This makes it problematic, since confita cannot read my types that have custom UnmarshalYAML
methods (the signature differs between yaml.v2 and yaml.v3).
I believe support for yaml.v3 could be added in a backwards compatible way, e.g. by passing opts to file.NewBackend(...)
. For example:
func NewBackend(path string, opts ...BackendOption) *Backend {
...
}
and an example option could be:
func WithUnmarshaler(decode func(f io.Reader, to interface{}) error) {
...
}
used like this:
import "gopkg.in/yaml.v3"
...
file.NewBackend("myfile.yaml", file.WithUnmarshaler(func(f io.Reader, to interface{}) error {
return yaml.NewDecoder(f).Decode(to)
}))
As a side effect, this would also allow supporting any file format, not just the built-in yaml, json, and toml.
Alternatively, if introducing functional options is undesirable, we could instead add new functions like:
type Umarshaler func(f io.Reader, to interface{}) error
func NewCustomBackend(path string, unmarshaler Unmarshaler) *Backend { ... }
func NewOptionalCustomBackend(path string, unmarshaler Unmarshaler) *Backend { ... }
What do you think? It should be a simple change and I could contribute a PR if this is something you'd be interested in.