1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00
yq/pkg/yqlib/yaml_converter.go
Conor Nosal 26a09e6ec0 Move implementation files to yqlib and test packages to allow for imports:
- Move data_navigator, json_converter, merge, and path_parser to pkg/yqlib
- Extract yamlToString from yq to pkg/yqlib/yaml_converter
- Move utils_test to test/utils
2019-12-06 13:58:56 +11:00

32 lines
712 B
Go

package yqlib
import (
yaml "github.com/mikefarah/yaml/v2"
errors "github.com/pkg/errors"
"strings"
)
func YamlToString(context interface{}, trimOutput bool) (string, error) {
switch context := context.(type) {
case string:
return context, nil
default:
return marshalContext(context, trimOutput)
}
}
func marshalContext(context interface{}, trimOutput bool) (string, error) {
out, err := yaml.Marshal(context)
if err != nil {
return "", errors.Wrap(err, "error printing yaml")
}
outStr := string(out)
// trim the trailing new line as it's easier for a script to add
// it in if required than to remove it
if trimOutput {
return strings.Trim(outStr, "\n "), nil
}
return outStr, nil
}