Skip to content

Commit b6d8b45

Browse files
committed
fix leading newline issue
1 parent 330e1e5 commit b6d8b45

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

goyaml.v3/emitterc.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,15 @@ func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
11601160
}
11611161

11621162
// Write an line comment.
1163-
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
1163+
func yaml_emitter_process_line_comment_linebreak(emitter *yaml_emitter_t, linebreak bool) bool {
11641164
if len(emitter.line_comment) == 0 {
1165+
// The next 3 lines are needed to resolve an issue with leading newlines
1166+
// See https://github.com/go-yaml/yaml/issues/755
1167+
// When linebreak is set to true, put_break will be called and will add
1168+
// the needed newline.
1169+
if linebreak && !put_break(emitter) {
1170+
return false
1171+
}
11651172
return true
11661173
}
11671174
if !emitter.whitespace {
@@ -1910,7 +1917,7 @@ func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bo
19101917
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
19111918
return false
19121919
}
1913-
if !yaml_emitter_process_line_comment(emitter) {
1920+
if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
19141921
return false
19151922
}
19161923
//emitter.indention = true
@@ -1947,7 +1954,7 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
19471954
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
19481955
return false
19491956
}
1950-
if !yaml_emitter_process_line_comment(emitter) {
1957+
if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
19511958
return false
19521959
}
19531960

goyaml.v3/patch.go

+6
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ func (e *Encoder) CompactSeqIndent() {
3131
func (e *Encoder) DefaultSeqIndent() {
3232
e.encoder.emitter.compact_sequence_indent = false
3333
}
34+
35+
// yaml_emitter_process_line_comment preserves the original signature and delegates to
36+
// yaml_emitter_process_line_comment_linebreak passing false for linebreak
37+
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
38+
return yaml_emitter_process_line_comment_linebreak(emitter, false)
39+
}

goyaml.v3/patch_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,17 @@ func (s *S) TestEncoderCompactIndents(c *C) {
144144
c.Assert(buf.String(), Equals, expected)
145145
}
146146
}
147+
148+
func (s *S) TestNewLinePreserved(c *C) {
149+
obj := &marshalerValue{}
150+
obj.Field.value = "a:\n b:\n c: d\n"
151+
data, err := yaml.Marshal(obj)
152+
c.Assert(err, IsNil)
153+
c.Assert(string(data), Equals, "_: |\n a:\n b:\n c: d\n")
154+
155+
obj.Field.value = "\na:\n b:\n c: d\n"
156+
data, err = yaml.Marshal(obj)
157+
c.Assert(err, IsNil)
158+
// the newline at the start of the file should be preserved
159+
c.Assert(string(data), Equals, "_: |4\n\n a:\n b:\n c: d\n")
160+
}

0 commit comments

Comments
 (0)