From 129c27ee6e9fed98dbfebeaa272fd52757b475b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 20 Oct 2018 11:16:18 +0200 Subject: parser/metadecoders: Consolidate the metadata decoders See #5324 --- tpl/transform/remarshal.go | 25 ++++++------------------- tpl/transform/remarshal_test.go | 15 ++++++++------- 2 files changed, 14 insertions(+), 26 deletions(-) (limited to 'tpl/transform') diff --git a/tpl/transform/remarshal.go b/tpl/transform/remarshal.go index 490def5f3..d5fe96ac6 100644 --- a/tpl/transform/remarshal.go +++ b/tpl/transform/remarshal.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/gohugoio/hugo/parser" + "github.com/gohugoio/hugo/parser/metadecoders" "github.com/spf13/cast" ) @@ -38,21 +39,7 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) return "", err } - var metaHandler func(d []byte) (map[string]interface{}, error) - - switch fromFormat { - case "yaml": - metaHandler = parser.HandleYAMLMetaData - case "toml": - metaHandler = parser.HandleTOMLMetaData - case "json": - metaHandler = parser.HandleJSONMetaData - } - - meta, err := metaHandler([]byte(from)) - if err != nil { - return "", err - } + meta, err := metadecoders.UnmarshalToMap([]byte(from), fromFormat) var result bytes.Buffer if err := parser.InterfaceToConfig(meta, mark, &result); err != nil { @@ -76,21 +63,21 @@ func toFormatMark(format string) (rune, error) { return 0, errors.New("failed to detect target data serialization format") } -func detectFormat(data string) (string, error) { +func detectFormat(data string) (metadecoders.Format, error) { jsonIdx := strings.Index(data, "{") yamlIdx := strings.Index(data, ":") tomlIdx := strings.Index(data, "=") if jsonIdx != -1 && (yamlIdx == -1 || jsonIdx < yamlIdx) && (tomlIdx == -1 || jsonIdx < tomlIdx) { - return "json", nil + return metadecoders.JSON, nil } if yamlIdx != -1 && (tomlIdx == -1 || yamlIdx < tomlIdx) { - return "yaml", nil + return metadecoders.YAML, nil } if tomlIdx != -1 { - return "toml", nil + return metadecoders.TOML, nil } return "", errors.New("failed to detect data serialization format") diff --git a/tpl/transform/remarshal_test.go b/tpl/transform/remarshal_test.go index 07c51c3b0..1416afff3 100644 --- a/tpl/transform/remarshal_test.go +++ b/tpl/transform/remarshal_test.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/gohugoio/hugo/helpers" + "github.com/gohugoio/hugo/parser/metadecoders" "github.com/spf13/viper" "github.com/stretchr/testify/require" ) @@ -179,12 +180,12 @@ func TestRemarshalDetectFormat(t *testing.T) { data string expect interface{} }{ - {`foo = "bar"`, "toml"}, - {` foo = "bar"`, "toml"}, - {`foo="bar"`, "toml"}, - {`foo: "bar"`, "yaml"}, - {`foo:"bar"`, "yaml"}, - {`{ "foo": "bar"`, "json"}, + {`foo = "bar"`, metadecoders.TOML}, + {` foo = "bar"`, metadecoders.TOML}, + {`foo="bar"`, metadecoders.TOML}, + {`foo: "bar"`, metadecoders.YAML}, + {`foo:"bar"`, metadecoders.YAML}, + {`{ "foo": "bar"`, metadecoders.JSON}, {`asdfasdf`, false}, {``, false}, } { @@ -198,6 +199,6 @@ func TestRemarshalDetectFormat(t *testing.T) { } assert.NoError(err, errMsg) - assert.Equal(test.expect, result, errMsg) + assert.Equal(test.expect, result) } } -- cgit v1.2.3