From 8e1a12b8c2f960e814ecaa118c287316ddd2a1b2 Mon Sep 17 00:00:00 2001 From: Natasha Sarkar Date: Fri, 25 Jun 2021 11:02:58 -0700 Subject: [PATCH] fix leading newline issue --- emitterc.go | 25 ++++++++++++++----------- encode_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/emitterc.go b/emitterc.go index caba6e33..f0f3d186 100644 --- a/emitterc.go +++ b/emitterc.go @@ -491,7 +491,7 @@ func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_eve if !yaml_emitter_emit_node(emitter, event, true, false, false, false) { return false } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -560,7 +560,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) { return false } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -605,7 +605,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e return false } } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -646,7 +646,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) { return false } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -719,7 +719,7 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e return false } } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -757,7 +757,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_ if !yaml_emitter_emit_node(emitter, event, false, true, false, false) { return false } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -833,7 +833,7 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_ } else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) { // An indented block follows, so write the comment right now. emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment @@ -843,7 +843,7 @@ func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_ if !yaml_emitter_emit_node(emitter, event, false, false, true, false) { return false } - if !yaml_emitter_process_line_comment(emitter) { + if !yaml_emitter_process_line_comment(emitter, false) { return false } if !yaml_emitter_process_foot_comment(emitter) { @@ -1149,8 +1149,11 @@ 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(emitter *yaml_emitter_t, linebreak bool) bool { if len(emitter.line_comment) == 0 { + if linebreak && !put_break(emitter) { + return false + } return true } if !emitter.whitespace { @@ -1899,7 +1902,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(emitter, true) { return false } //emitter.indention = true @@ -1936,7 +1939,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(emitter, true) { return false } diff --git a/encode_test.go b/encode_test.go index c941e0de..3de4b14c 100644 --- a/encode_test.go +++ b/encode_test.go @@ -790,6 +790,20 @@ func (s *S) TestCompactSequenceWithSetIndent(c *C) { `) } +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") +} + func (s *S) TestSortedOutput(c *C) { order := []interface{}{ false,