aboutsummaryrefslogtreecommitdiffhomepage
path: root/parser/frontmatter.go
diff options
context:
space:
mode:
authorDawid Gaweł <[email protected]>2017-12-01 20:37:19 +0100
committerBjørn Erik Pedersen <[email protected]>2018-02-09 10:27:17 +0100
commit16a5c74519771138023f019fe535fa5b250dc50d (patch)
treeb9d113a4961ff9660a39448f9544d54f5ad65f1c /parser/frontmatter.go
parent49b98975be721ed848c14384868b6d9c0d146396 (diff)
downloadhugo-16a5c74519771138023f019fe535fa5b250dc50d.tar.gz
hugo-16a5c74519771138023f019fe535fa5b250dc50d.zip
parser: Fix YAML maps key type
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
Diffstat (limited to 'parser/frontmatter.go')
-rw-r--r--parser/frontmatter.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/parser/frontmatter.go b/parser/frontmatter.go
index e9552a859..c1a57ce5b 100644
--- a/parser/frontmatter.go
+++ b/parser/frontmatter.go
@@ -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) {