Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(file) convert yaml to JSON #229

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions file/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"path/filepath"
"regexp"

ghodssyaml "github.com/ghodss/yaml"
"github.com/imdario/mergo"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)

// getContent reads all the YAML and JSON files in the directory or the
Expand Down Expand Up @@ -91,13 +91,22 @@ func readContent(reader io.Reader) (*Content, error) {
if err != nil {
return nil, errors.Wrap(err, "validating file content")
}
err = yaml.Unmarshal(bytes, &content)
err = yamlUnmarshal(bytes, &content)
if err != nil {
return nil, err
}
return &content, nil
}

// yamlUnmarshal is a wrapper around yaml.Unmarshal to ensure that the right
// yaml package is in use. Using ghodss/yaml ensures that no
// `map[interface{}]interface{}` is present in go-kong.Plugin.Configuration.
// If it is present, then it leads to a silent error. See Github Issue #144.
// The verification for this is done using a test.
func yamlUnmarshal(bytes []byte, v interface{}) error {
return ghodssyaml.Unmarshal(bytes, v)
}

// configFilesInDir traverses the directory rooted at dir and
// returns all the files with a case-insensitive extension of `yml` or `yaml`.
func configFilesInDir(dir string) ([]string, error) {
Expand Down
34 changes: 34 additions & 0 deletions file/readfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,37 @@ func Test_getContent(t *testing.T) {
})
}
}

func Test_yamlUnmarshal(t *testing.T) {
stringToInterfaceMap := map[string]interface{}{}
bytes1 := `
versions:
v1:
enabled: false
`
mapOfMap := map[string]interface{}{}
err := yamlUnmarshal([]byte(bytes1), &mapOfMap)
if err != nil {
t.Errorf("yamlUnmarshal() error = %v (should be nil)", err)
}
subMap := mapOfMap["versions"]
if reflect.TypeOf(subMap) != reflect.TypeOf(stringToInterfaceMap) {
t.Errorf("yamlUnmarshal() expected type: %T, got: %T", stringToInterfaceMap, subMap)
}

bytes2 := `
versions:
- enabled: false
version: 1
`
mapOfArrayOfMap := map[string]interface{}{}
err = yamlUnmarshal([]byte(bytes2), &mapOfArrayOfMap)
if err != nil {
t.Errorf("yamlUnmarshal() error = %v (should be nil)", err)
}
array := mapOfArrayOfMap["versions"].([]interface{})
element := array[0]
if reflect.TypeOf(element) != reflect.TypeOf(stringToInterfaceMap) {
t.Errorf("yamlUnmarshal() expected type: %T, got: %T", stringToInterfaceMap, element)
}
}