summaryrefslogtreecommitdiffstats
path: root/tpl/transform
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-10-20 11:16:18 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-10-22 20:46:13 +0200
commit129c27ee6e9fed98dbfebeaa272fd52757b475b2 (patch)
treeba931600714e354f0c7d05ad0a598f591b0258f6 /tpl/transform
parent44da60d869578423dea529db62ed613588a2a560 (diff)
parser/metadecoders: Consolidate the metadata decoders
See #5324
Diffstat (limited to 'tpl/transform')
-rw-r--r--tpl/transform/remarshal.go25
-rw-r--r--tpl/transform/remarshal_test.go15
2 files changed, 14 insertions, 26 deletions
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)
}
}