Skip to content

Support for yaml.v3 #95

Description

@kszafran

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions