From 3756bbae901a299d04df407c440333a956493d67 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 18 Mar 2015 06:22:19 -0700 Subject: [PATCH] Reliably merge the yaml maps data --- config/config_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++- config/disk.go | 16 +++++-- util/util.go | 18 ++++++++ 3 files changed, 126 insertions(+), 5 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index c19a346a..65705917 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,12 @@ package config -import "testing" +import ( + "fmt" + "log" + "testing" + + "github.com/rancherio/os/util" +) import "reflect" func TestParseCmdline(t *testing.T) { @@ -93,3 +99,92 @@ func TestSet(t *testing.T) { t.Fatalf("Expected %v, got %v", expected, data) } } + +type OuterData struct { + One Data `"yaml:one"` +} + +type Data struct { + Two bool `"yaml:two"` + Three bool `"yaml:three"` +} + +func TestMapMerge(t *testing.T) { + one := ` +one: + two: true` + two := ` +one: + three: true` + + data := make(map[string]map[string]bool) + yaml.Unmarshal([]byte(one), data) + yaml.Unmarshal([]byte(two), data) + + if _, ok := data["one"]; !ok { + t.Fatal("one not found") + } + + if !data["one"]["three"] { + t.Fatal("three not found") + } + + if data["one"]["two"] { + t.Fatal("two not found") + } + + data2 := &OuterData{} + yaml.Unmarshal([]byte(one), data2) + yaml.Unmarshal([]byte(two), data2) + + if !data2.One.Three { + t.Fatal("three not found") + } + + if !data2.One.Two { + t.Fatal("two not found") + } + +} + +func TestUserDocker(t *testing.T) { + config := &Config{ + UserDocker: DockerConfig{ + TLS: true, + }, + } + + bytes, err := yaml.Marshal(config) + if err != nil { + log.Fatal(err) + } + + config = NewConfig() + err = yaml.Unmarshal(bytes, config) + if err != nil { + log.Fatal(err) + } + + data := make(map[interface{}]interface{}) + util.Convert(config, data) + + fmt.Println(data) + + val, ok := data["user_docker"] + if !ok { + t.Fatal("Failed to find user_docker") + } + + if m, ok := val.(map[interface{}]interface{}); ok { + if v, ok := m["tls"]; ok { + if v != true { + t.Fatal("user_docker.tls is not true") + } + } else { + t.Fatal("user_docker.tls is not found") + } + } else { + t.Fatal("Bad data") + } + +} diff --git a/config/disk.go b/config/disk.go index c2d35601..c0d34541 100644 --- a/config/disk.go +++ b/config/disk.go @@ -62,26 +62,34 @@ func readSavedConfig(bytes []byte) (map[interface{}]interface{}, error) { } func readConfig(bytes []byte, files ...string) (map[interface{}]interface{}, error) { - data := make(map[interface{}]interface{}) + // You can't just overlay yaml bytes on to maps, it won't merge, but instead + // just override the keys and not merge the map values. + left := make(map[interface{}]interface{}) for _, conf := range files { content, err := readConfigFile(conf) if err != nil { return nil, err } - err = yaml.Unmarshal(content, &data) + right := make(map[interface{}]interface{}) + err = yaml.Unmarshal(content, &right) if err != nil { return nil, err } + + util.MergeMaps(left, right) } if bytes != nil && len(bytes) > 0 { - if err := yaml.Unmarshal(bytes, &data); err != nil { + right := make(map[interface{}]interface{}) + if err := yaml.Unmarshal(bytes, &right); err != nil { return nil, err } + + util.MergeMaps(left, right) } - return data, nil + return left, nil } func readConfigFile(file string) ([]byte, error) { diff --git a/util/util.go b/util/util.go index 2d859307..36c49257 100644 --- a/util/util.go +++ b/util/util.go @@ -164,3 +164,21 @@ func Convert(from, to interface{}) error { return yaml.Unmarshal(bytes, to) } + +func MergeMaps(left, right map[interface{}]interface{}) { + for k, v := range right { + merged := false + if existing, ok := left[k]; ok { + if rightMap, ok := v.(map[interface{}]interface{}); ok { + if leftMap, ok := existing.(map[interface{}]interface{}); ok { + merged = true + MergeMaps(leftMap, rightMap) + } + } + } + + if !merged { + left[k] = v + } + } +}