Reputation: 10961
I'm trying to parse a yaml
file with Golang. I defined the following types
:
type DockerNetwork struct {
MyNetwork struct {
driver string
} `yaml:"my_network"`
}
// DockerNetworks represent the docker Networks type
type DockerNetworks struct {
networks []DockerNetwork
}
so I have my unit test in place:
func TestDockerNetwork(t *testing.T) {
dn := DockerNetworks{}
var data = `
networks:
my_network:
driver: bridge
`
err := yaml.Unmarshal([]byte(data), &dn)
if err != nil {
log.Fatalf("error: %v", err)
t.Error("Could not Unmarshal the data")
}
log.Println(fmt.Sprintf("--- t:\n%v\n\n", dn))
}
I expected it to work, however I'm getting no input:
2016/12/14 13:38:12 --- t:
{{}}
what am I doing wrong?
Upvotes: 3
Views: 5021
Reputation: 10961
My yaml
file is a docker-compose.yml
file, which is in the format described above (part of it). I agree with Marius that I have unexported fields, which is an error. The solution I used was map
:
m := make(map[interface{}]interface{})
err = yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- m:\n%v\n\n", m)
Upvotes: 2
Reputation: 195
AFAIKT there to things, data structures with unexported fields and test data which doesn't match your data structures.
Something like this would give you a go:
type DockerNetwork struct {
MyNetwork struct {
Driver string `yaml:driver`
} `yaml:"my_network"`
}
type DockerNetworks struct {
Networks []DockerNetwork `yaml:networks`
}
And test data:
networks:
- my_network:
driver: bridge
Gives an output:
2016/12/14 23:47:23 --- t:
{[{{bridge}}]}
PASS
And It's hard to tell, what is "working" in your opinion.
Upvotes: 0