Skip to content

Commit

Permalink
disable seq indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jun 22, 2021
1 parent 496545a commit e965076
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
17 changes: 11 additions & 6 deletions emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_
}

// Increase the indentation level.
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool, seq bool) bool {
emitter.indents = append(emitter.indents, emitter.indent)
if emitter.indent < 0 {
if flow {
Expand All @@ -243,6 +243,9 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool
// Everything else aligns to the chosen indentation.
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
}
if seq {
emitter.indent = emitter.indent - 2
}
}
return true
}
Expand Down Expand Up @@ -534,7 +537,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
emitter.flow_level++
Expand Down Expand Up @@ -617,7 +620,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve
if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
emitter.flow_level++
Expand Down Expand Up @@ -728,7 +731,9 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e
// Expect a block item node.
func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if first {
if !yaml_emitter_increase_indent(emitter, false, false) {
seq := emitter.mapping_context && (emitter.column == 0 || !emitter.indention) &&
emitter.disable_sequence_indent
if !yaml_emitter_increase_indent(emitter, false, false, seq){
return false
}
}
Expand Down Expand Up @@ -764,7 +769,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_
// Expect a block key node.
func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if first {
if !yaml_emitter_increase_indent(emitter, false, false) {
if !yaml_emitter_increase_indent(emitter, false, false, false) {
return false
}
}
Expand Down Expand Up @@ -896,7 +901,7 @@ func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool
if !yaml_emitter_process_tag(emitter) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
if !yaml_emitter_process_scalar(emitter) {
Expand Down
31 changes: 31 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,37 @@ func (s *S) TestSetIndent(c *C) {
c.Assert(buf.String(), Equals, "a:\n b:\n c: d\n")
}

func (s *S) TestDisableSeqIndentDefault(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.DisableSeqIndent()
err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
// The default indent is 4, so these sequence elements get 2 indents as before
c.Assert(buf.String(), Equals, `a:
- b
- c
`)
}

func (s *S) TestDisableSequenceWithSetIndent(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.DisableSeqIndent()
enc.SetIndent(2)
err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
// The sequence indent is 2, so these sequence elements don't get indented at all
c.Assert(buf.String(), Equals, `a:
- b
- c
`)
}

func (s *S) TestSortedOutput(c *C) {
order := []interface{}{
false,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module "gopkg.in/yaml.v3"

require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
)
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
5 changes: 5 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ func (e *Encoder) SetIndent(spaces int) {
e.encoder.indent = spaces
}

// DisableSeqIndent disables the indentation for sequence items.
func (e *Encoder) DisableSeqIndent() {
e.encoder.emitter.disable_sequence_indent = true
}

// Close closes the encoder by writing any remaining data.
// It does not write a stream terminating string "...".
func (e *Encoder) Close() (err error) {
Expand Down
2 changes: 2 additions & 0 deletions yamlh.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@ type yaml_emitter_t struct {

indent int // The current indentation level.

disable_sequence_indent bool // Disable the indentation for sequence items

flow_level int // The current flow level.

root_context bool // Is it the document root context?
Expand Down

0 comments on commit e965076

Please sign in to comment.