Skip to content

Commit

Permalink
parser: Add WARNING for integer YAML keys
Browse files Browse the repository at this point in the history
```bash
benchmark                                               old ns/op     new ns/op     delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4     3053          2015          -34.00%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4        5.23          5.18          -0.96%
BenchmarkStringifyMapKeysIntegers-4                     2320          5177          +123.15%

benchmark                                               old allocs     new allocs     delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4     6              6              +0.00%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4        0              0              +0.00%
BenchmarkStringifyMapKeysIntegers-4                     6              14             +133.33%

benchmark                                               old bytes     new bytes     delta
BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps-4     1008          1008          +0.00%
BenchmarkStringifyMapKeysStringsOnlyStringMaps-4        0             0             +0.00%
BenchmarkStringifyMapKeysIntegers-4                     1008          1776          +76.19%
```
Closes #4393
  • Loading branch information
bep committed Feb 12, 2018
1 parent 10a917d commit 0816a97
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions parser/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"io"
"strings"

"github.com/gohugoio/hugo/helpers"

"github.com/spf13/cast"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -256,10 +258,20 @@ func stringifyMapKeys(in interface{}) (interface{}, bool) {
}
case map[interface{}]interface{}:
res := make(map[string]interface{})
var (
ok bool
err error
)
for k, v := range in {
ks, err := cast.ToStringE(k)
if err != nil {
ks = fmt.Sprintf("%v", k)
var ks string

if ks, ok = k.(string); !ok {
ks, err = cast.ToStringE(k)
if err != nil {
ks = fmt.Sprintf("%v", k)
}
// TODO(bep) added in Hugo 0.37, remove some time in the future.
helpers.DistinctFeedbackLog.Printf("WARNING: YAML data/frontmatter with keys of type %T is since Hugo 0.37 converted to strings", k)
}
if vv, replaced := stringifyMapKeys(v); replaced {
res[ks] = vv
Expand Down

0 comments on commit 0816a97

Please sign in to comment.