go-chassis/go-archaius

Got nil when using GetConfigurationByKey to get a array from a yaml file

qizha opened this issue · 1 comments

qizha commented

I have a yaml file with this structure:
mq:
endpoints:
- hosts: 127.0.0.1
port: 5672
- hosts: 192.168.1.1
port: 5672

The return value of GetConfigurationByKey("mq.endpoints") is nil

@qizha

RootCause:

The given yaml is not valid yaml format because of that yaml unmarshal is failing.

Please refer below code snippet

package main

import (
	"gopkg.in/yaml.v2"
	"fmt"
)

var filecontent = []byte(`
mq:
endpoints:
- hosts: 127.0.0.1
port: 5672
- hosts: 192.168.1.1
port: 5672
`)

func main() {
	ss := yaml.MapSlice{}
	err := yaml.Unmarshal([]byte(filecontent), &ss)
	fmt.Println("Error is", err)
}

The output of this is

$ go run test.go 
Error is yaml: line 5: did not find expected key

Solution:

Change file structure to the valid yaml format and use GetConfigurationByKey("mq.endpoints") it will give the value

Proper Yaml format

mq:
  endpoints:
  - hosts: 127.0.0.1
    port: 2222
  - hosts: 127.0.0.1
    port: 3333

fSource.GetConfigurationByKey("mq.endpoints")

OUTPUT:

  [[{hosts 127.0.0.1} {port 2222}] [{hosts 127.0.0.1} {port 3333}]]