diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-12-21 16:21:13 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-12-23 10:02:42 +0100 |
commit | 822dc627a1cfdf1f97882f27761675ac6ace7669 (patch) | |
tree | b453158c329495fa59dc38374eb8296995ba0ce0 /tpl/transform/remarshal.go | |
parent | 43f9df0194d229805d80b13c9e38a7a0fec12cf4 (diff) |
tpl/transform: Add transform.Unmarshal func
Fixes #5428
Diffstat (limited to 'tpl/transform/remarshal.go')
-rw-r--r-- | tpl/transform/remarshal.go | 30 |
1 files changed, 5 insertions, 25 deletions
diff --git a/tpl/transform/remarshal.go b/tpl/transform/remarshal.go index fd0742b7f..144964f0a 100644 --- a/tpl/transform/remarshal.go +++ b/tpl/transform/remarshal.go @@ -2,9 +2,10 @@ package transform import ( "bytes" - "errors" "strings" + "github.com/pkg/errors" + "github.com/gohugoio/hugo/parser" "github.com/gohugoio/hugo/parser/metadecoders" "github.com/spf13/cast" @@ -34,9 +35,9 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) return "", err } - fromFormat, err := detectFormat(from) - if err != nil { - return "", err + fromFormat := metadecoders.FormatFromContentString(from) + if fromFormat == "" { + return "", errors.New("failed to detect format from content") } meta, err := metadecoders.UnmarshalToMap([]byte(from), fromFormat) @@ -56,24 +57,3 @@ func toFormatMark(format string) (metadecoders.Format, error) { return "", errors.New("failed to detect target data serialization format") } - -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 metadecoders.JSON, nil - } - - if yamlIdx != -1 && (tomlIdx == -1 || yamlIdx < tomlIdx) { - return metadecoders.YAML, nil - } - - if tomlIdx != -1 { - return metadecoders.TOML, nil - } - - return "", errors.New("failed to detect data serialization format") - -} |