Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,9 @@ func (emitter *yamlEmitter) writeDoubleQuotedScalar(value []byte, allow_breaks b
}

func (emitter *yamlEmitter) writeBlockScalarHints(value []byte) bool {
if isSpace(value, 0) || isLineBreak(value, 0) {
if isSpace(value, 0) {
// https://github.com/yaml/go-yaml/issues/65
// isLineBreak(value, 0) removed as the linebreak will only write the indention value.
indent_hint := []byte{'0' + byte(emitter.best_indent)}
if !emitter.writeIndicator(indent_hint, false, false, false) {
return false
Expand Down
23 changes: 22 additions & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var marshalTests = []struct {
value any
data string
}{
// Basic types. First to validate handling newlines at the start.
{
nil,
"null\n",
Expand Down Expand Up @@ -136,6 +137,14 @@ var marshalTests = []struct {
map[string]any{"v": ""},
"v: \"\"\n",
},
{
map[string]any{"v": "\nhi"},
"v: |-\n\n hi\n",
},
{
map[string][]any{"v": {map[string]any{"v1": "\nhi"}}},
"v:\n - v1: |-\n\n hi\n",
},
{
map[string][]string{"v": {"A", "B"}},
"v:\n - A\n - B\n",
Expand Down Expand Up @@ -930,6 +939,18 @@ a:
- c: 1
d: 2
`),

normal(`
v:
- v1: |-

hi
`): compact(`
v:
- v1: |-

hi
`),
}

func TestEncoderCompactIndents(t *testing.T) {
Expand Down Expand Up @@ -966,7 +987,7 @@ func TestNewLinePreserved(t *testing.T) {
data, err = yaml.Marshal(obj)
assert.NoError(t, err)
// the newline at the start of the file should be preserved
assert.Equal(t, "_: |4\n\n a:\n b:\n c: d\n", string(data))
assert.Equal(t, "_: |\n\n a:\n b:\n c: d\n", string(data))
}

func TestScalarStyleRules(t *testing.T) {
Expand Down