From 5f76bca8a36be409d1044a1839647e3005ec3c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fernando=20Carri=C3=B3n?= Date: Fri, 13 Oct 2023 13:07:10 +0200 Subject: [PATCH] fix value merging & json unmarshalling --- go.mod | 1 + go.sum | 2 ++ main.go | 25 ++++++++++++++----------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 085e242..4453b2c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/Masterminds/sprig/v3 v3.2.3 + github.com/peterbourgon/mergemap v0.0.1 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 45e7efc..27c9ebb 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/peterbourgon/mergemap v0.0.1 h1:5/brtSACv34REV0xoYjPQ8JXZnx3nurGt6WInLRwqX4= +github.com/peterbourgon/mergemap v0.0.1/go.mod h1:jQyRpOpE/KbvPc0VKXjAqctYglwUO5W6zAcGcFfbvlo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= diff --git a/main.go b/main.go index 728421e..652fc70 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,13 @@ import ( "html/template" "io" "io/fs" - "io/ioutil" "os" "path/filepath" "strings" "time" + "github.com/peterbourgon/mergemap" + sprig "github.com/Masterminds/sprig/v3" yaml "gopkg.in/yaml.v3" ) @@ -97,28 +98,30 @@ func expandFiles(files []string) (expFiles []string) { func mergeValues() (result map[string]interface{}) { result = map[string]interface{}{} for _, vf := range flags.valuesFiles { - vfContent, err := ioutil.ReadFile(vf) + vfContent, err := os.ReadFile(vf) checkErr(err) if strings.HasSuffix(vf, ".json") { - err = json.Unmarshal(vfContent, result) + err = json.Unmarshal(vfContent, &result) checkErr(err) } else { err = yaml.Unmarshal(vfContent, result) checkErr(err) } } - for i, v := range flags.values { - subMap := result - for _, node := range v.address { + for _, v := range flags.values { + valueMap := map[string]interface{}{} + subMap := valueMap + // fmt.Fprintf(os.Stderr, "113: %+v\n", subMap) + for i, node := range v.address { if i == len(v.address)-1 { subMap[node] = v.v - } else { - if _, ok := subMap[node]; !ok { - subMap[node] = map[string]interface{}{} - } - subMap = subMap[node].(map[string]interface{}) + break } + subMap[node] = map[string]interface{}{} + // fmt.Fprintf(os.Stderr, "120: %+v\n", subMap) + subMap = subMap[node].(map[string]interface{}) } + mergemap.Merge(result, valueMap) } return }