parser: Fix YAML maps key type
authorDawid Gaweł <d.gawel@livechatinc.com>
Fri, 1 Dec 2017 19:37:19 +0000 (20:37 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 9 Feb 2018 09:27:17 +0000 (10:27 +0100)
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

parser/frontmatter.go
parser/frontmatter_test.go

index e9552a859b72d6a94b571ccc18cd371e9bdc8045..c1a57ce5b175bf4d0568bb7afbf270dc773e1ec7 100644 (file)
@@ -19,6 +19,7 @@ import (
        "bytes"
        "encoding/json"
        "errors"
+       "fmt"
        "io"
        "strings"
 
@@ -201,9 +202,45 @@ func removeTOMLIdentifier(datum []byte) []byte {
 func HandleYAMLMetaData(datum []byte) (map[string]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) (map[string]interface{}, error) {
index 2e5bdec50034c5335d75a79f6de6acd966065404..a7a35fa076d4dfe68f4165107a513aa35a4ec93f 100644 (file)
@@ -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},
        }
@@ -320,6 +321,41 @@ func TestRemoveTOMLIdentifier(t *testing.T) {
        }
 }
 
+func TestStringifyYAMLMapKeys(t *testing.T) {
+       cases := []struct {
+               input map[interface{}]interface{}
+               want  map[string]interface{}
+       }{
+               {
+                       map[interface{}]interface{}{"a": 1, "b": 2},
+                       map[string]interface{}{"a": 1, "b": 2},
+               },
+               {
+                       map[interface{}]interface{}{"a": []interface{}{1, map[interface{}]interface{}{"b": 2}}},
+                       map[string]interface{}{"a": []interface{}{1, map[string]interface{}{"b": 2}}},
+               },
+               {
+                       map[interface{}]interface{}{true: 1, "b": false},
+                       map[string]interface{}{"true": 1, "b": false},
+               },
+               {
+                       map[interface{}]interface{}{1: "a", 2: "b"},
+                       map[string]interface{}{"1": "a", "2": "b"},
+               },
+               {
+                       map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": 1}},
+                       map[string]interface{}{"a": map[string]interface{}{"b": 1}},
+               },
+       }
+
+       for i, c := range cases {
+               res := stringifyYAMLMapKeys(c.input)
+               if !reflect.DeepEqual(res, c.want) {
+                       t.Errorf("[%d] given %q\nwant: %q\n got: %q", i, c.input, c.want, res)
+               }
+       }
+}
+
 func BenchmarkFrontmatterTags(b *testing.B) {
 
        for _, frontmatter := range []string{"JSON", "YAML", "YAML2", "TOML"} {