parser: Tune stringifyMapKeys
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 12 Feb 2018 16:39:11 +0000 (17:39 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 12 Feb 2018 18:14:02 +0000 (19:14 +0100)
```bash
benchmark                                               old ns/op     new ns/op     delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4     3269          3053          -6.61%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4        4.79          5.23          +9.19%
BenchmarkStringifyMapKeysIntegers-4                     2707          2320          -14.30%

benchmark                                               old allocs     new allocs     delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4     16             6              -62.50%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4        0              0              +0.00%
BenchmarkStringifyMapKeysIntegers-4                     16             6              -62.50%

benchmark                                               old bytes     new bytes     delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4     1080          1008          -6.67%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4        0             0             +0.00%
BenchmarkStringifyMapKeysIntegers-4                     1080          1008          -6.67%
```

parser/frontmatter.go
parser/frontmatter_test.go

index c5aaf807a2c0f63411413a1ddf6e3cd9211d680b..664cb6d5ca4c48fbf39681868b009e3fb8732a12 100644 (file)
@@ -23,6 +23,8 @@ import (
        "io"
        "strings"
 
+       "github.com/spf13/cast"
+
        "github.com/BurntSushi/toml"
        "github.com/chaseadamsio/goorgeous"
 
@@ -209,7 +211,9 @@ func HandleYAMLMetaData(datum []byte) (map[string]interface{}, error) {
        // gotten from `json`.
        if err == nil {
                for k, v := range m {
-                       m[k] = stringifyMapKeys(v)
+                       if vv, changed := stringifyMapKeys(v); changed {
+                               m[k] = vv
+                       }
                }
        }
 
@@ -221,16 +225,19 @@ func HandleYAMLMetaData(datum []byte) (map[string]interface{}, error) {
 func HandleYAMLData(datum []byte) (interface{}, error) {
        var m interface{}
        err := yaml.Unmarshal(datum, &m)
+       if err != nil {
+               return nil, err
+       }
 
        // 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 {
-               m = stringifyMapKeys(m)
+       if mm, changed := stringifyMapKeys(m); changed {
+               return mm, nil
        }
 
-       return m, err
+       return m, nil
 }
 
 // stringifyMapKeys recurses into in and changes all instances of
@@ -239,23 +246,31 @@ func HandleYAMLData(datum []byte) (interface{}, error) {
 // described here: https://github.com/go-yaml/yaml/issues/139
 //
 // Inspired by https://github.com/stripe/stripe-mock, MIT licensed
-func stringifyMapKeys(in interface{}) interface{} {
+func stringifyMapKeys(in interface{}) (interface{}, bool) {
        switch in := in.(type) {
        case []interface{}:
-               res := make([]interface{}, len(in))
                for i, v := range in {
-                       res[i] = stringifyMapKeys(v)
+                       if vv, replaced := stringifyMapKeys(v); replaced {
+                               in[i] = vv
+                       }
                }
-               return res
        case map[interface{}]interface{}:
                res := make(map[string]interface{})
                for k, v := range in {
-                       res[fmt.Sprintf("%v", k)] = stringifyMapKeys(v)
+                       ks, err := cast.ToStringE(k)
+                       if err != nil {
+                               ks = fmt.Sprintf("%v", k)
+                       }
+                       if vv, replaced := stringifyMapKeys(v); replaced {
+                               res[ks] = vv
+                       } else {
+                               res[ks] = v
+                       }
                }
-               return res
-       default:
-               return in
+               return res, true
        }
+
+       return nil, false
 }
 
 // HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface
index 4d28facb22d7ffcb8cbc6a5686edb1ce07a73f56..7281ca3368ff6b02374586bc4de39def3305c857 100644 (file)
@@ -323,37 +323,56 @@ func TestRemoveTOMLIdentifier(t *testing.T) {
 
 func TestStringifyYAMLMapKeys(t *testing.T) {
        cases := []struct {
-               input interface{}
-               want  map[string]interface{}
+               input    interface{}
+               want     interface{}
+               replaced bool
        }{
                {
                        map[interface{}]interface{}{"a": 1, "b": 2},
                        map[string]interface{}{"a": 1, "b": 2},
+                       true,
                },
                {
                        map[interface{}]interface{}{"a": []interface{}{1, map[interface{}]interface{}{"b": 2}}},
                        map[string]interface{}{"a": []interface{}{1, map[string]interface{}{"b": 2}}},
+                       true,
                },
                {
                        map[interface{}]interface{}{true: 1, "b": false},
                        map[string]interface{}{"true": 1, "b": false},
+                       true,
                },
                {
                        map[interface{}]interface{}{1: "a", 2: "b"},
                        map[string]interface{}{"1": "a", "2": "b"},
+                       true,
                },
                {
                        map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": 1}},
                        map[string]interface{}{"a": map[string]interface{}{"b": 1}},
+                       true,
                },
                {
                        map[string]interface{}{"a": map[string]interface{}{"b": 1}},
                        map[string]interface{}{"a": map[string]interface{}{"b": 1}},
+                       false,
+               },
+               {
+                       []interface{}{map[interface{}]interface{}{1: "a", 2: "b"}},
+                       []interface{}{map[string]interface{}{"1": "a", "2": "b"}},
+                       false,
                },
        }
 
        for i, c := range cases {
-               res := stringifyMapKeys(c.input)
+               res, replaced := stringifyMapKeys(c.input)
+
+               if c.replaced != replaced {
+                       t.Fatalf("[%d] Replaced mismatch: %t", i, replaced)
+               }
+               if !c.replaced {
+                       res = c.input
+               }
                if !reflect.DeepEqual(res, c.want) {
                        t.Errorf("[%d] given %q\nwant: %q\n got: %q", i, c.input, c.want, res)
                }
@@ -407,7 +426,7 @@ func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) {
 
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               stringifyYAMLMapKeys(m)
+               stringifyMapKeys(m)
        }
 }