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.
This commit is contained in:
Mike Farah 2018-03-27 16:22:24 +11:00
parent 8f15dba812
commit ee8ffd458a
5 changed files with 26 additions and 2 deletions

View File

@ -0,0 +1,5 @@
test: |
abcdefg
hijklmno

View File

@ -1,5 +1,5 @@
name: yq name: yq
version: 1.14.0 version: 1.14.1
summary: A lightweight and portable command-line YAML processor summary: A lightweight and portable command-line YAML processor
description: | description: |
The aim of the project is to be the jq or sed of yaml files. The aim of the project is to be the jq or sed of yaml files.

View File

@ -11,7 +11,7 @@ var (
GitDescribe string GitDescribe string
// Version is main version number that is being run at the moment. // 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) // 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 // then it means that it is a final release. Otherwise, this is a pre-release

11
yq.go
View File

@ -417,10 +417,21 @@ func toString(context interface{}) (string, error) {
} }
func yamlToString(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) out, err := yaml.Marshal(context)
if err != nil { if err != nil {
return "", fmt.Errorf("error printing yaml: %v", err) return "", fmt.Errorf("error printing yaml: %v", err)
} }
outStr := string(out) outStr := string(out)
// trim the trailing new line as it's easier for a script to add // trim the trailing new line as it's easier for a script to add
// it in if required than to remove it // it in if required than to remove it

View File

@ -47,6 +47,14 @@ application: MyApp`,
formattedResult) formattedResult)
} }
func TestMultilineString(t *testing.T) {
testString := `
abcd
efg`
formattedResult, _ := yamlToString(testString)
assertResult(t, testString, formattedResult)
}
func TestNewYaml(t *testing.T) { func TestNewYaml(t *testing.T) {
result, _ := newYaml([]string{"b.c", "3"}) result, _ := newYaml([]string{"b.c", "3"})
formattedResult := fmt.Sprintf("%v", result) formattedResult := fmt.Sprintf("%v", result)