Преглед изворни кода

Double-quote multi-line strings in flow style.

Gustavo Niemeyer пре 6 година
родитељ
комит
513397e5a7
2 измењених фајлова са 10 додато и 1 уклоњено
  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.
 	switch {
 	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:
 		style = yaml_PLAIN_SCALAR_STYLE
 	default:

+ 5 - 0
encode_test.go

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