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

YAML parser issue with naked lists as the top level objects in a yaml document

FranPavelic-SB opened this issue · comments

Describe the bug
Attempting to read a yaml document consisting of a list results in cannot unmarshal !!seq into map[string]interface {} error.
I'm assuming the same behaviour will happen with naked scalars as well.

To Reproduce

# test fixture consisting of list at the root of the yaml doc
- entry1
- entry2
- entry3
- entry4
- entry5
package main

import (
	"fmt"
	"github.com/knadh/koanf/parsers/yaml"
	"github.com/knadh/koanf/providers/file"
	"github.com/knadh/koanf/v2"
)

func main() {
	k := koanf.New(".")
	err := k.Load(file.Provider("test-list-on-root.yml"), yaml.Parser())
	if err != nil {
		fmt.Printf("%v", err)
	}
}

Expected behavior
A better error than a raw error thrown from yaml.Unmarshall.

Possible alternative behaviour, that i implemented in my usecase, was to use the bare name of the file as the key of the "document" and loading the list there.

Please provide the following information):

  • OS: osx
  • Koanf Version: v2.0.1

Describe the bug Attempting to read a yaml document consisting of a list results in cannot unmarshal !!seq into map[string]interface {} error. I'm assuming the same behaviour will happen with naked scalars as well.

To Reproduce

# test fixture consisting of list at the root of the yaml doc
- entry1
- entry2
- entry3
- entry4
- entry5
package main

import (
	"fmt"
	"github.com/knadh/koanf/parsers/yaml"
	"github.com/knadh/koanf/providers/file"
	"github.com/knadh/koanf/v2"
)

func main() {
	k := koanf.New(".")
	err := k.Load(file.Provider("test-list-on-root.yml"), yaml.Parser())
	if err != nil {
		fmt.Printf("%v", err)
	}
}

Expected behavior A better error than a raw error thrown from yaml.Unmarshall.

Possible alternative behaviour, that i implemented in my usecase, was to use the bare name of the file as the key of the "document" and loading the list there.

Please provide the following information):

  • OS: osx
  • Koanf Version: v2.0.1

in koanf, the file data will unmarshal into map[string]interface{} object.
so you need add a key for values.

entrys:
  - entry1
  - entry2
  - entry3
  - entry4
  - entry5

This is expected behaviour. koanf is a key-value configuration system. Everything has to have a key. In a config map, without a key, it’s not conceptually possible to refer to a specific value.