mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 22:21:07 +03:00
parser: Tune stringifyMapKeys
```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% ```
This commit is contained in:
parent
d4beef0d2b
commit
10a917dfdc
2 changed files with 50 additions and 16 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue