diff options
Diffstat (limited to 'parser')
-rw-r--r-- | parser/frontmatter.go | 232 | ||||
-rw-r--r-- | parser/frontmatter_test.go | 266 | ||||
-rw-r--r-- | parser/metadecoders/decoder.go | 146 | ||||
-rw-r--r-- | parser/metadecoders/decoder_test.go | 207 | ||||
-rw-r--r-- | parser/metadecoders/format.go | 66 | ||||
-rw-r--r-- | parser/metadecoders/format_test.go | 57 | ||||
-rw-r--r-- | parser/metadecoders/json.go | 31 | ||||
-rw-r--r-- | parser/metadecoders/yaml.go | 84 | ||||
-rw-r--r-- | parser/page.go | 9 | ||||
-rw-r--r-- | parser/page_test.go | 129 | ||||
-rw-r--r-- | parser/pageparser/item.go | 2 | ||||
-rw-r--r-- | parser/pageparser/pagelexer.go | 34 | ||||
-rw-r--r-- | parser/pageparser/pageparser.go | 10 | ||||
-rw-r--r-- | parser/pageparser/pageparser_intro_test.go | 2 |
14 files changed, 533 insertions, 742 deletions
diff --git a/parser/frontmatter.go b/parser/frontmatter.go index 284d3f955..a42db0cca 100644 --- a/parser/frontmatter.go +++ b/parser/frontmatter.go @@ -19,16 +19,12 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io" "strings" - "github.com/gohugoio/hugo/helpers" - - "github.com/spf13/cast" + "github.com/gohugoio/hugo/parser/metadecoders" "github.com/BurntSushi/toml" - "github.com/chaseadamsio/goorgeous" "gopkg.in/yaml.v2" ) @@ -79,6 +75,82 @@ func InterfaceToConfig(in interface{}, mark rune, w io.Writer) error { } } +func InterfaceToConfig2(in interface{}, format metadecoders.Format, w io.Writer) error { + if in == nil { + return errors.New("input was nil") + } + + switch format { + case metadecoders.YAML: + b, err := yaml.Marshal(in) + if err != nil { + return err + } + + _, err = w.Write(b) + return err + + case metadecoders.TOML: + return toml.NewEncoder(w).Encode(in) + case metadecoders.JSON: + b, err := json.MarshalIndent(in, "", " ") + if err != nil { + return err + } + + _, err = w.Write(b) + if err != nil { + return err + } + + _, err = w.Write([]byte{'\n'}) + return err + + default: + return errors.New("Unsupported Format provided") + } +} + +func InterfaceToFrontMatter2(in interface{}, format metadecoders.Format, w io.Writer) error { + if in == nil { + return errors.New("input was nil") + } + + switch format { + case metadecoders.YAML: + _, err := w.Write([]byte(YAMLDelimUnix)) + if err != nil { + return err + } + + err = InterfaceToConfig2(in, format, w) + if err != nil { + return err + } + + _, err = w.Write([]byte(YAMLDelimUnix)) + return err + + case metadecoders.TOML: + _, err := w.Write([]byte(TOMLDelimUnix)) + if err != nil { + return err + } + + err = InterfaceToConfig2(in, format, w) + + if err != nil { + return err + } + + _, err = w.Write([]byte("\n" + TOMLDelimUnix)) + return err + + default: + return InterfaceToConfig2(in, format, w) + } +} + // InterfaceToFrontMatter encodes a given input into a frontmatter // representation based upon the mark with the appropriate front matter delimiters // surrounding the output, which is written to w. @@ -155,34 +227,6 @@ func FormatSanitize(kind string) string { } } -// DetectFrontMatter detects the type of frontmatter analysing its first character. -func DetectFrontMatter(mark rune) (f *FrontmatterType) { - switch mark { - case '-': - return &FrontmatterType{HandleYAMLMetaData, []byte(YAMLDelim), []byte(YAMLDelim), false} - case '+': - return &FrontmatterType{HandleTOMLMetaData, []byte(TOMLDelim), []byte(TOMLDelim), false} - case '{': - return &FrontmatterType{HandleJSONMetaData, []byte{'{'}, []byte{'}'}, true} - case '#': - return &FrontmatterType{HandleOrgMetaData, []byte("#+"), []byte("\n"), false} - default: - return nil - } -} - -// HandleTOMLMetaData unmarshals TOML-encoded datum and returns a Go interface -// representing the encoded data structure. -func HandleTOMLMetaData(datum []byte) (map[string]interface{}, error) { - m := map[string]interface{}{} - datum = removeTOMLIdentifier(datum) - - _, err := toml.Decode(string(datum), &m) - - return m, err - -} - // removeTOMLIdentifier removes, if necessary, beginning and ending TOML // frontmatter delimiters from a byte slice. func removeTOMLIdentifier(datum []byte) []byte { @@ -200,125 +244,3 @@ func removeTOMLIdentifier(datum []byte) []byte { b = bytes.Trim(b, "\r\n") return bytes.TrimSuffix(b, []byte(TOMLDelim)) } - -// HandleYAMLMetaData unmarshals YAML-encoded datum and returns a Go interface -// representing the encoded data structure. -// TODO(bep) 2errors remove these handlers (and hopefully package) -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 { - if vv, changed := stringifyMapKeys(v); changed { - m[k] = vv - } - } - } - - return m, err -} - -// HandleYAMLData unmarshals YAML-encoded datum and returns a Go interface -// representing the encoded data structure. -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 mm, changed := stringifyMapKeys(m); changed { - return mm, nil - } - - return m, nil -} - -// stringifyMapKeys 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 stringifyMapKeys(in interface{}) (interface{}, bool) { - switch in := in.(type) { - case []interface{}: - for i, v := range in { - if vv, replaced := stringifyMapKeys(v); replaced { - in[i] = vv - } - } - case map[interface{}]interface{}: - res := make(map[string]interface{}) - var ( - ok bool - err error - ) - for k, v := range in { - 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 - } else { - res[ks] = v - } - } - return res, true - } - - return nil, false -} - -// HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface -// representing the encoded data structure. -func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) { - m := make(map[string]interface{}) - - if datum == nil { - // Package json returns on error on nil input. - // Return an empty map to be consistent with our other supported - // formats. - return m, nil - } - - err := json.Unmarshal(datum, &m) - return m, err -} - -// HandleJSONData unmarshals JSON-encoded datum and returns a Go interface -// representing the encoded data structure. -func HandleJSONData(datum []byte) (interface{}, error) { - if datum == nil { - // Package json returns on error on nil input. - // Return an empty map to be consistent with our other supported - // formats. - return make(map[string]interface{}), nil - } - - var f interface{} - err := json.Unmarshal(datum, &f) - return f, err -} - -// HandleOrgMetaData unmarshals org-mode encoded datum and returns a Go -// interface representing the encoded data structure. -func HandleOrgMetaData(datum []byte) (map[string]interface{}, error) { - return goorgeous.OrgHeaders(datum) -} diff --git a/parser/frontmatter_test.go b/parser/frontmatter_test.go index 7281ca336..d6e6e79c3 100644 --- a/parser/frontmatter_test.go +++ b/parser/frontmatter_test.go @@ -132,116 +132,6 @@ func TestInterfaceToFrontMatter(t *testing.T) { } } -func TestHandleTOMLMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("title = \"test 1\""), map[string]interface{}{"title": "test 1"}, false}, - {[]byte("a = [1, 2, 3]"), map[string]interface{}{"a": []interface{}{int64(1), int64(2), int64(3)}}, false}, - {[]byte("b = [\n[1, 2],\n[3, 4]\n]"), map[string]interface{}{"b": []interface{}{[]interface{}{int64(1), int64(2)}, []interface{}{int64(3), int64(4)}}}, false}, - // errors - {[]byte("z = [\n[1, 2]\n[3, 4]\n]"), nil, true}, - } - - for i, c := range cases { - res, err := HandleTOMLMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - -func TestHandleYAMLMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {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[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}, - } - - for i, c := range cases { - res, err := HandleYAMLMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - -func TestHandleJSONMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("{\"title\": \"test 1\"}"), map[string]interface{}{"title": "test 1"}, false}, - // errors - {[]byte("{noquotes}"), nil, true}, - } - - for i, c := range cases { - res, err := HandleJSONMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - -func TestHandleOrgMetaData(t *testing.T) { - cases := []struct { - input []byte - want interface{} - isErr bool - }{ - {nil, map[string]interface{}{}, false}, - {[]byte("#+title: test 1\n"), map[string]interface{}{"title": "test 1"}, false}, - } - - for i, c := range cases { - res, err := HandleOrgMetaData(c.input) - if err != nil { - if c.isErr { - continue - } - t.Fatalf("[%d] unexpected error value: %v", i, err) - } - - if !reflect.DeepEqual(res, c.want) { - t.Errorf("[%d] not equal: given %q\nwant %#v,\n got %#v", i, c.input, c.want, res) - } - } -} - func TestFormatToLeadRune(t *testing.T) { for i, this := range []struct { kind string @@ -264,41 +154,6 @@ func TestFormatToLeadRune(t *testing.T) { } } -func TestDetectFrontMatter(t *testing.T) { - cases := []struct { - mark rune - want *FrontmatterType - }{ - // funcs are uncomparable, so we ignore FrontmatterType.Parse in these tests - {'-', &FrontmatterType{nil, []byte(YAMLDelim), []byte(YAMLDelim), false}}, - {'+', &FrontmatterType{nil, []byte(TOMLDelim), []byte(TOMLDelim), false}}, - {'{', &FrontmatterType{nil, []byte("{"), []byte("}"), true}}, - {'#', &FrontmatterType{nil, []byte("#+"), []byte("\n"), false}}, - {'$', nil}, - } - - for _, c := range cases { - res := DetectFrontMatter(c.mark) - if res == nil { - if c.want == nil { - continue - } - - t.Fatalf("want %v, got %v", *c.want, res) - } - - if !reflect.DeepEqual(res.markstart, c.want.markstart) { - t.Errorf("markstart mismatch: want %v, got %v", c.want.markstart, res.markstart) - } - if !reflect.DeepEqual(res.markend, c.want.markend) { - t.Errorf("markend mismatch: want %v, got %v", c.want.markend, res.markend) - } - if !reflect.DeepEqual(res.includeMark, c.want.includeMark) { - t.Errorf("includeMark mismatch: want %v, got %v", c.want.includeMark, res.includeMark) - } - } -} - func TestRemoveTOMLIdentifier(t *testing.T) { cases := []struct { input string @@ -321,64 +176,6 @@ func TestRemoveTOMLIdentifier(t *testing.T) { } } -func TestStringifyYAMLMapKeys(t *testing.T) { - cases := []struct { - 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, 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) - } - } -} - func BenchmarkFrontmatterTags(b *testing.B) { for _, frontmatter := range []string{"JSON", "YAML", "YAML2", "TOML"} { @@ -388,69 +185,6 @@ func BenchmarkFrontmatterTags(b *testing.B) { } } -func BenchmarkStringifyMapKeysStringsOnlyInterfaceMaps(b *testing.B) { - maps := make([]map[interface{}]interface{}, b.N) - for i := 0; i < b.N; i++ { - maps[i] = map[interface{}]interface{}{ - "a": map[interface{}]interface{}{ - "b": 32, - "c": 43, - "d": map[interface{}]interface{}{ - "b": 32, - "c": 43, - }, - }, - "b": []interface{}{"a", "b"}, - "c": "d", - } - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - stringifyMapKeys(maps[i]) - } -} - -func BenchmarkStringifyMapKeysStringsOnlyStringMaps(b *testing.B) { - m := map[string]interface{}{ - "a": map[string]interface{}{ - "b": 32, - "c": 43, - "d": map[string]interface{}{ - "b": 32, - "c": 43, - }, - }, - "b": []interface{}{"a", "b"}, - "c": "d", - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - stringifyMapKeys(m) - } -} - -func BenchmarkStringifyMapKeysIntegers(b *testing.B) { - maps := make([]map[interface{}]interface{}, b.N) - for i := 0; i < b.N; i++ { - maps[i] = map[interface{}]interface{}{ - 1: map[interface{}]interface{}{ - 4: 32, - 5: 43, - 6: map[interface{}]interface{}{ - 7: 32, - 8: 43, - }, - }, - 2: []interface{}{"a", "b"}, - 3: "d", - } - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - stringifyMapKeys(maps[i]) - } -} func doBenchmarkFrontmatter(b *testing.B, fileformat string, numTags int) { yamlTemplate := `--- name: "Tags" diff --git a/parser/metadecoders/decoder.go b/parser/metadecoders/decoder.go index 280361a84..e409b7665 100644 --- a/parser/metadecoders/decoder.go +++ b/parser/metadecoders/decoder.go @@ -15,81 +15,139 @@ package metadecoders import ( "encoding/json" + "fmt" "github.com/BurntSushi/toml" "github.com/chaseadamsio/goorgeous" - "github.com/gohugoio/hugo/parser/pageparser" "github.com/pkg/errors" + "github.com/spf13/cast" yaml "gopkg.in/yaml.v2" ) -type Format string - -const ( - // These are the supported metdata formats in Hugo. Most of these are also - // supported as /data formats. - ORG Format = "org" - JSON Format = "json" - TOML Format = "toml" - YAML Format = "yaml" -) - -// FormatFromFrontMatterType will return empty if not supported. -func FormatFromFrontMatterType(typ pageparser.ItemType) Format { - switch typ { - case pageparser.TypeFrontMatterJSON: - return JSON - case pageparser.TypeFrontMatterORG: - return ORG - case pageparser.TypeFrontMatterTOML: - return TOML - case pageparser.TypeFrontMatterYAML: - return YAML - default: - return "" - } -} - // UnmarshalToMap will unmarshall data in format f into a new map. This is // what's needed for Hugo's front matter decoding. func UnmarshalToMap(data []byte, f Format) (map[string]interface{}, error) { m := make(map[string]interface{}) - if data == nil { return m, nil } + err := unmarshal(data, f, &m) + + return m, err + +} + +// Unmarshal will unmarshall data in format f into an interface{}. +// This is what's needed for Hugo's /data handling. +func Unmarshal(data []byte, f Format) (interface{}, error) { + if data == nil { + return make(map[string]interface{}), nil + } + var v interface{} + err := unmarshal(data, f, &v) + + return v, err +} + +// unmarshal unmarshals data in format f into v. +func unmarshal(data []byte, f Format, v interface{}) error { + var err error switch f { case ORG: - m, err = goorgeous.OrgHeaders(data) + vv, err := goorgeous.OrgHeaders(data) + if err != nil { + return err + } + switch v.(type) { + case *map[string]interface{}: + *v.(*map[string]interface{}) = vv + default: + *v.(*interface{}) = vv + } case JSON: - err = json.Unmarshal(data, &m) + err = json.Unmarshal(data, v) case TOML: - _, err = toml.Decode(string(data), &m) + err = toml.Unmarshal(data, v) case YAML: - err = yaml.Unmarshal(data, &m) + err = yaml.Unmarshal(data, v) - // To support boolean keys, the `yaml` package unmarshals maps to + // 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 { - if vv, changed := stringifyMapKeys(v); changed { - m[k] = vv - } + var ptr interface{} + switch v.(type) { + case *map[string]interface{}: + ptr = *v.(*map[string]interface{}) + case *interface{}: + ptr = *v.(*interface{}) + default: + return errors.Errorf("unknown type %T in YAML unmarshal", v) + } + + if mm, changed := stringifyMapKeys(ptr); changed { + switch v.(type) { + case *map[string]interface{}: + *v.(*map[string]interface{}) = mm.(map[string]interface{}) + case *interface{}: + *v.(*interface{}) = mm } } default: - return nil, errors.Errorf("unmarshal of format %q is not supported", f) + return errors.Errorf("unmarshal of format %q is not supported", f) } - if err != nil { - return nil, errors.Wrapf(err, "unmarshal failed for format %q", f) - } + return err - return m, nil +} + +// stringifyMapKeys 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 stringifyMapKeys(in interface{}) (interface{}, bool) { + + switch in := in.(type) { + case []interface{}: + for i, v := range in { + if vv, replaced := stringifyMapKeys(v); replaced { + in[i] = vv + } + } + case map[string]interface{}: + for k, v := range in { + if vv, changed := stringifyMapKeys(v); changed { + in[k] = vv + } + } + case map[interface{}]interface{}: + res := make(map[string]interface{}) + var ( + ok bool + err error + ) + for k, v := range in { + var ks string + + if ks, ok = k.(string); !ok { + 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, true + } + return nil, false } diff --git a/parser/metadecoders/decoder_test.go b/parser/metadecoders/decoder_test.go new file mode 100644 index 000000000..94cfd5a9a --- /dev/null +++ b/parser/metadecoders/decoder_test.go @@ -0,0 +1,207 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package metadecoders + +import ( + "fmt" + "reflect" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestUnmarshalToMap(t *testing.T) { + assert := require.New(t) + + expect := map[string]interface{}{"a": "b"} + + for i, test := range []struct { + data string + format Format + expect interface{} + }{ + {`a = "b"`, TOML, expect}, + {`a: "b"`, YAML, expect}, + // Make sure we get all string keys, even for YAML + {"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}}, + {"a:\n true: 1\n false: 2", YAML, map[string]interface{}{"a": map[string]interface{}{"true": 1, "false": 2}}}, + {`{ "a": "b" }`, JSON, expect}, + {`#+a: b`, ORG, expect}, + // errors + {`a = b`, TOML, false}, + } { + msg := fmt.Sprintf("%d: %s", i, test.format) + m, err := UnmarshalToMap([]byte(test.data), test.format) + if b, ok := test.expect.(bool); ok && !b { + assert.Error(err, msg) + } else { + assert.NoError(err, msg) + assert.Equal(test.expect, m, msg) + } + } +} + +func TestUnmarshalToInterface(t *testing.T) { + assert := require.New(t) + + expect := map[string]interface{}{"a": "b"} + + for i, test := range []struct { + data string + format Format + expect interface{} + }{ + {`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}}, + {`{ "a": "b" }`, JSON, expect}, + {`#+a: b`, ORG, expect}, + {`a = "b"`, TOML, expect}, + {`a: "b"`, YAML, expect}, + {"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]interface{}{"a": "Easy!", "b": map[string]interface{}{"c": 2, "d": []interface{}{3, 4}}}}, + // errors + {`a = "`, TOML, false}, + } { + msg := fmt.Sprintf("%d: %s", i, test.format) + m, err := Unmarshal([]byte(test.data), test.format) + if b, ok := test.expect.(bool); ok && !b { + assert.Error(err, msg) + } else { + assert.NoError(err, msg) + assert.Equal(test.expect, m, msg) + } + + } + +} + +func TestStringifyYAMLMapKeys(t *testing.T) { + cases := []struct { + 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}, |