Skip to content

Commit

Permalink
fix: nested state values weren't correctly unmarshalled
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusramberg committed Aug 15, 2024
1 parent 5f6dbb1 commit 4f45e48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ func writeValJson(state string, env string, overrides []string) (*os.File, error
keys := strings.Split(kv[0], ".")
for i, key := range keys {
if i == len(keys)-1 {
mref[key] = kv[1]
mref[key], err = unmarshalOption(kv[1])
if err != nil {
return nil, err
}
} else {
if _, ok := m[key]; !ok {
mref[key] = make(map[string]interface{})
Expand All @@ -269,15 +272,14 @@ func writeValJson(state string, env string, overrides []string) (*os.File, error
}
}
} else {
var val interface{}
err := json.Unmarshal([]byte(kv[1]), &val)
m[kv[0]], err = unmarshalOption(kv[1])
if err != nil {
return nil, fmt.Errorf("failed to marshal %s: %s", kv[1], err)
return nil, err
}
m[kv[0]] = val
}
}
}

// Serialize the values
envStr, err := json.Marshal(m)
if err != nil {
Expand All @@ -294,6 +296,15 @@ func writeValJson(state string, env string, overrides []string) (*os.File, error
return f, nil
}

func unmarshalOption(val string) (interface{}, error) {
var v interface{}
err := yaml.Unmarshal([]byte(val), &v)
if err != nil {
return nil, err
}
return v, nil
}

func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
for k, v := range a {
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestTemplate(t *testing.T) {
}
}

var vals = `{"bad":123,"bar":"true","foo":{"bad":"hello","bar":"false","baz":true,"foo":true}}`
var vals = `{"bad":123,"bar":"true","foo":{"bad":"hello","bar":false,"baz":true,"foo":true}}`

func TestWriteValJson(t *testing.T) {
f, err := writeValJson(cwd+"/testData/helm", "test", []string{"foo.bar=false", "bad=123", "foo.bad=hello"})
Expand Down

0 comments on commit 4f45e48

Please sign in to comment.