Use ghodss/yaml for yaml marshaling & unmarshaling in template (#1556)

Fixes #1555

Co-authored-by: Yoann Ciabaud <yoann@linxo.com>
Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
Yoann Ciabaud 2020-12-13 03:03:20 +01:00 committed by GitHub
parent 4c0987a618
commit 54eb73b423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 3 deletions

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/Masterminds/sprig/v3 v3.1.0
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a
github.com/frankban/quicktest v1.11.2 // indirect
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-test/deep v1.0.7
github.com/golang/mock v1.4.4
github.com/google/go-cmp v0.5.2

2
go.sum
View File

@ -205,6 +205,8 @@ github.com/fujiwara/tfstate-lookup v0.0.14 h1:cjSRXGRWRq57WCDt5VESFpFBNTQF18eCAv
github.com/fujiwara/tfstate-lookup v0.0.14/go.mod h1:08o5Rm5pKzvIxoZe3D0NIT8AHYBqTzyUU03BZ/c0IKA=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew=
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=

View File

@ -2,9 +2,9 @@ package tmpl
import (
"fmt"
"github.com/ghodss/yaml"
"github.com/roboll/helmfile/pkg/helmexec"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
"io"
"os"
"os/exec"

View File

@ -1,7 +1,9 @@
package tmpl
import (
"encoding/json"
"fmt"
"github.com/google/go-cmp/cmp"
"path/filepath"
"reflect"
"testing"
@ -47,12 +49,31 @@ func TestReadFile_PassAbsPath(t *testing.T) {
}
}
func TestToYaml_UnsupportedNestedMapKey(t *testing.T) {
expected := ``
vals := Values(map[string]interface{}{
"foo": map[interface{}]interface{}{
"bar": "BAR",
},
})
actual, err := ToYaml(vals)
if err == nil {
t.Fatalf("expected error but got none")
} else if err.Error() != "error marshaling into JSON: json: unsupported type: map[interface {}]interface {}" {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("unexpected result: expected=%v, actual=%v", expected, actual)
}
}
func TestToYaml(t *testing.T) {
expected := `foo:
bar: BAR
`
vals := Values(map[string]interface{}{
"foo": map[interface{}]interface{}{
"foo": map[string]interface{}{
"bar": "BAR",
},
})
@ -70,7 +91,7 @@ func TestFromYaml(t *testing.T) {
bar: BAR
`
expected := Values(map[string]interface{}{
"foo": map[interface{}]interface{}{
"foo": map[string]interface{}{
"bar": "BAR",
},
})
@ -83,6 +104,27 @@ func TestFromYaml(t *testing.T) {
}
}
func TestFromYamlToJson(t *testing.T) {
input := `foo:
bar: BAR
`
want := `{"foo":{"bar":"BAR"}}`
m, err := FromYaml(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err := json.Marshal(m)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if d := cmp.Diff(want, string(got)); d != "" {
t.Errorf("unexpected result: want (-), got (+):\n%s", d)
}
}
func TestSetValueAtPath_OneComponent(t *testing.T) {
input := map[string]interface{}{
"foo": "",