knadh / koanf

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How a custom Yaml Unmarshal structure can be executed

alimoli opened this issue · comments

Describe the bug
I would like to load a YAML file and execute the custom YAML unmarshal (yaml v3 interface) for some fields.
The result is that no custom implementation is executed.

To Reproduce

type Address string

func (a *Address) UnmarshalYAML(value *yaml.Node) error {
	log.Println("Custom unmarshal")
	var address string
	err := value.Decode(&address)
	if err != nil {
		return err
	}

	// Lowercase the address string
	*a = Address(strings.ToLower(address))
	return nil
}

And this is how I parse the file:

k.Load(file.Provider(filepath.Join(absDir, "config.yml")), yaml.Parser())

Expected behavior
I expect that the unmarshal is executed.

Please provide the following information):

  • OS: OSX
  • Koanf Version 1.4.4

Hi @alimoli. This is not possible. koanf unmarshals all data formats into map[string]interface{} first.

Once the config map is loaded, you can look into koanf.Unmarshal() to unmarshal the config map into a struct.