2018-02-09 03:21:46 -05:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-02 10:07:52 -04:00
|
|
|
"errors"
|
2024-01-28 16:34:10 -05:00
|
|
|
"strings"
|
2018-12-21 10:21:13 -05:00
|
|
|
|
2018-02-09 03:21:46 -05:00
|
|
|
"github.com/gohugoio/hugo/parser"
|
2018-10-20 05:16:18 -04:00
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
2018-02-09 03:21:46 -05:00
|
|
|
"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.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Remarshal(format string, data any) (string, error) {
|
|
|
|
var meta map[string]any
|
2018-02-09 03:21:46 -05:00
|
|
|
|
|
|
|
format = strings.TrimSpace(strings.ToLower(format))
|
|
|
|
|
|
|
|
mark, err := toFormatMark(format)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
if m, ok := data.(map[string]any); ok {
|
2019-11-06 14:10:47 -05:00
|
|
|
meta = m
|
|
|
|
} else {
|
|
|
|
from, err := cast.ToStringE(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-02-09 03:21:46 -05:00
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
from = strings.TrimSpace(from)
|
|
|
|
if from == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fromFormat := metadecoders.Default.FormatFromContentString(from)
|
|
|
|
if fromFormat == "" {
|
|
|
|
return "", errors.New("failed to detect format from content")
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err = metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-03-24 05:11:16 -04:00
|
|
|
}
|
2018-02-09 03:21:46 -05:00
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
// Make it so 1.0 float64 prints as 1 etc.
|
|
|
|
applyMarshalTypes(meta)
|
|
|
|
|
2018-02-09 03:21:46 -05:00
|
|
|
var result bytes.Buffer
|
|
|
|
if err := parser.InterfaceToConfig(meta, mark, &result); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
// The unmarshal/marshal dance is extremely type lossy, and we need
|
|
|
|
// to make sure that integer types prints as "43" and not "43.0" in
|
|
|
|
// all formats, hence this hack.
|
2022-03-17 17:03:27 -04:00
|
|
|
func applyMarshalTypes(m map[string]any) {
|
2019-11-06 14:10:47 -05:00
|
|
|
for k, v := range m {
|
|
|
|
switch t := v.(type) {
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any:
|
2019-11-06 14:10:47 -05:00
|
|
|
applyMarshalTypes(t)
|
|
|
|
case float64:
|
|
|
|
i := int64(t)
|
|
|
|
if t == float64(i) {
|
|
|
|
m[k] = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
func toFormatMark(format string) (metadecoders.Format, error) {
|
|
|
|
if f := metadecoders.FormatFromString(format); f != "" {
|
|
|
|
return f, nil
|
2018-02-09 03:21:46 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
return "", errors.New("failed to detect target data serialization format")
|
2018-02-09 03:21:46 -05:00
|
|
|
}
|