summaryrefslogtreecommitdiffstats
path: root/tpl/transform/remarshal.go
blob: fd0742b7f74ebf11f6446b897b661887431f6b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package transform

import (
	"bytes"
	"errors"
	"strings"

	"github.com/gohugoio/hugo/parser"
	"github.com/gohugoio/hugo/parser/metadecoders"
	"github.com/spf13/cast"
)

// Remarshal is used in the Hugo documentation to convert configuration
// examples from YAML to JSON, TOML (and possibly the other way around).
// The is primarily a helper for the Hugo docs site.
// It is not a general purpose YAML to TOML converter etc., and may
// change without notice if it serves a purpose in the docs.
// Format is one of json, yaml or toml.
func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) {
	from, err := cast.ToStringE(data)
	if err != nil {
		return "", err
	}

	from = strings.TrimSpace(from)
	format = strings.TrimSpace(strings.ToLower(format))

	if from == "" {
		return "", nil
	}

	mark, err := toFormatMark(format)
	if err != nil {
		return "", err
	}

	fromFormat, err := detectFormat(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 {
		return "", err
	}

	return result.String(), nil
}

func toFormatMark(format string) (metadecoders.Format, error) {
	if f := metadecoders.FormatFromString(format); f != "" {
		return f, nil
	}

	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")

}