From ee8ffd458a0d9580e88cdfd51455f44bea36fe7a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 27 Mar 2018 16:22:24 +1100 Subject: [PATCH] Multiline value fix - multi line strings no longer printed as a yaml block Although printing the string as a yaml block can be argued to be technically correct, in practical terms it's more useful to just print out the multiline string as is. --- examples/multiline-text.yaml | 5 +++++ snap/snapcraft.yaml | 2 +- version.go | 2 +- yq.go | 11 +++++++++++ yq_test.go | 8 ++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 examples/multiline-text.yaml diff --git a/examples/multiline-text.yaml b/examples/multiline-text.yaml new file mode 100644 index 0000000..8ecccac --- /dev/null +++ b/examples/multiline-text.yaml @@ -0,0 +1,5 @@ +test: | + abcdefg + hijklmno + + diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index dc4b8ef..8b16e37 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: yq -version: 1.14.0 +version: 1.14.1 summary: A lightweight and portable command-line YAML processor description: | The aim of the project is to be the jq or sed of yaml files. diff --git a/version.go b/version.go index ca4adcd..9218e80 100644 --- a/version.go +++ b/version.go @@ -11,7 +11,7 @@ var ( GitDescribe string // Version is main version number that is being run at the moment. - Version = "1.14.0" + Version = "1.14.1" // VersionPrerelease is a pre-release marker for the version. If this is "" (empty string) // then it means that it is a final release. Otherwise, this is a pre-release diff --git a/yq.go b/yq.go index 8dde704..6472010 100644 --- a/yq.go +++ b/yq.go @@ -417,10 +417,21 @@ func toString(context interface{}) (string, error) { } func yamlToString(context interface{}) (string, error) { + switch context.(type) { + case string: + return context.(string), nil + default: + return marshalContext(context) + } +} + +func marshalContext(context interface{}) (string, error) { out, err := yaml.Marshal(context) + if err != nil { return "", fmt.Errorf("error printing yaml: %v", err) } + 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 diff --git a/yq_test.go b/yq_test.go index fa0dd3e..b00cc4b 100644 --- a/yq_test.go +++ b/yq_test.go @@ -47,6 +47,14 @@ application: MyApp`, formattedResult) } +func TestMultilineString(t *testing.T) { + testString := ` + abcd + efg` + formattedResult, _ := yamlToString(testString) + assertResult(t, testString, formattedResult) +} + func TestNewYaml(t *testing.T) { result, _ := newYaml([]string{"b.c", "3"}) formattedResult := fmt.Sprintf("%v", result)