Skip to content

Commit

Permalink
fix leading newline issue
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Oct 23, 2023
1 parent 330e1e5 commit b6d8b45
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions goyaml.v3/emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,8 +1160,15 @@ func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
}

// Write an line comment.
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
func yaml_emitter_process_line_comment_linebreak(emitter *yaml_emitter_t, linebreak bool) bool {
if len(emitter.line_comment) == 0 {
// The next 3 lines are needed to resolve an issue with leading newlines
// See https://github.com/go-yaml/yaml/issues/755
// When linebreak is set to true, put_break will be called and will add
// the needed newline.
if linebreak && !put_break(emitter) {
return false
}
return true
}
if !emitter.whitespace {
Expand Down Expand Up @@ -1910,7 +1917,7 @@ func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bo
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
return false
}
//emitter.indention = true
Expand Down Expand Up @@ -1947,7 +1954,7 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
return false
}
if !yaml_emitter_process_line_comment(emitter) {
if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
return false
}

Expand Down
6 changes: 6 additions & 0 deletions goyaml.v3/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ func (e *Encoder) CompactSeqIndent() {
func (e *Encoder) DefaultSeqIndent() {
e.encoder.emitter.compact_sequence_indent = false
}

// yaml_emitter_process_line_comment preserves the original signature and delegates to
// yaml_emitter_process_line_comment_linebreak passing false for linebreak
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
return yaml_emitter_process_line_comment_linebreak(emitter, false)
}
14 changes: 14 additions & 0 deletions goyaml.v3/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,17 @@ func (s *S) TestEncoderCompactIndents(c *C) {
c.Assert(buf.String(), Equals, expected)
}
}

func (s *S) TestNewLinePreserved(c *C) {
obj := &marshalerValue{}
obj.Field.value = "a:\n b:\n c: d\n"
data, err := yaml.Marshal(obj)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, "_: |\n a:\n b:\n c: d\n")

obj.Field.value = "\na:\n b:\n c: d\n"
data, err = yaml.Marshal(obj)
c.Assert(err, IsNil)
// the newline at the start of the file should be preserved
c.Assert(string(data), Equals, "_: |4\n\n a:\n b:\n c: d\n")
}

0 comments on commit b6d8b45

Please sign in to comment.