Skip to content

Commit

Permalink
parser: Fix YAML maps key type
Browse files Browse the repository at this point in the history
Recurse through result of yaml package parsing and change all
maps from map[interface{}]interface{} to map[string]interface{}
making them jsonable and sortable.

Fixes #2441, #4083
  • Loading branch information
dmgawel committed Dec 6, 2017
1 parent 6233ddf commit 6474026
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
37 changes: 37 additions & 0 deletions parser/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -201,9 +202,45 @@ func removeTOMLIdentifier(datum []byte) []byte {
func HandleYAMLMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
err := yaml.Unmarshal(datum, &m)

// To support boolean keys, the `yaml` package unmarshals maps to
// map[interface{}]interface{}. Here we recurse through the result
// and change all maps to map[string]interface{} like we would've
// gotten from `json`.
if err == nil {
for k, v := range m {
m[k] = stringifyYAMLMapKeys(v)
}
}

return m, err
}

// stringifyKeysMapValue recurses into in and changes all instances of
// map[interface{}]interface{} to map[string]interface{}. This is useful to
// work around the impedence mismatch between JSON and YAML unmarshaling that's
// described here: https://github.com/go-yaml/yaml/issues/139
//
// Inspired by https://github.com/stripe/stripe-mock, MIT licensed
func stringifyYAMLMapKeys(in interface{}) interface{} {
switch in := in.(type) {
case []interface{}:
res := make([]interface{}, len(in))
for i, v := range in {
res[i] = stringifyYAMLMapKeys(v)
}
return res
case map[interface{}]interface{}:
res := make(map[string]interface{})
for k, v := range in {
res[fmt.Sprintf("%v", k)] = stringifyYAMLMapKeys(v)
}
return res
default:
return in
}
}

// HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface
// representing the encoded data structure.
func HandleJSONMetaData(datum []byte) (interface{}, error) {
Expand Down
38 changes: 37 additions & 1 deletion parser/frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ func TestHandleYAMLMetaData(t *testing.T) {
}{
{nil, map[string]interface{}{}, false},
{[]byte("title: test 1"), map[string]interface{}{"title": "test 1"}, false},
{[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), map[string]interface{}{"a": "Easy!", "b": map[interface{}]interface{}{"c": 2, "d": []interface{}{3, 4}}}, false},
{[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}, false},
{[]byte("a:\n true: 1\n false: 2"), map[string]interface{}{"a": map[string]interface{}{"true": 1, "false": 2}}, false},
// errors
{[]byte("z = not toml"), nil, true},
}
Expand Down Expand Up @@ -320,6 +321,41 @@ func TestRemoveTOMLIdentifier(t *testing.T) {
}
}

func TestStringifyYAMLMapKeys(t *testing.T) {
input := map[interface{}]interface{}{
true: []interface{}{
123,
map[interface{}]interface{}{
"intkey": 123,
},
},
"boolkey": true,
"intkey": 123,
"mapkey": map[interface{}]interface{}{
"intkey": 123,
},
}
want := map[string]interface{}{
"true": []interface{}{
123,
map[string]interface{}{
"intkey": 123,
},
},
"boolkey": true,
"intkey": 123,
"mapkey": map[string]interface{}{
"intkey": 123,
},
}

res := stringifyYAMLMapKeys(input)

if !reflect.DeepEqual(res, want) {
t.Errorf("key type mismatch: want %v, got %v", want, res)
}
}

func BenchmarkFrontmatterTags(b *testing.B) {

for _, frontmatter := range []string{"JSON", "YAML", "YAML2", "TOML"} {
Expand Down

0 comments on commit 6474026

Please sign in to comment.