Browse Source

Double-quote multi-line strings in flow style.

Gustavo Niemeyer 6 years ago
parent
commit
513397e5a7
2 changed files with 10 additions and 1 deletions
  1. 5 1
      encode.go
  2. 5 0
      encode_test.go

+ 5 - 1
encode.go

@@ -326,7 +326,11 @@ func (e *encoder) stringv(tag string, in reflect.Value) {
 	// text that's incompatible with that tag.
 	// text that's incompatible with that tag.
 	switch {
 	switch {
 	case strings.Contains(s, "\n"):
 	case strings.Contains(s, "\n"):
-		style = yaml_LITERAL_SCALAR_STYLE
+		if e.flow {
+			style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
+		} else {
+			style = yaml_LITERAL_SCALAR_STYLE
+		}
 	case canUsePlain:
 	case canUsePlain:
 		style = yaml_PLAIN_SCALAR_STYLE
 		style = yaml_PLAIN_SCALAR_STYLE
 	default:
 	default:

+ 5 - 0
encode_test.go

@@ -261,6 +261,11 @@ var marshalTests = []struct {
 			} "a,flow"
 			} "a,flow"
 		}{struct{ B, D string }{"c", "e"}},
 		}{struct{ B, D string }{"c", "e"}},
 		"a: {b: c, d: e}\n",
 		"a: {b: c, d: e}\n",
+	}, {
+		&struct {
+			A string "a,flow"
+		}{"b\nc"},
+		"a: \"b\\nc\"\n",
 	},
 	},
 
 
 	// Unexported field
 	// Unexported field