diff --git a/kyaml/internal/forked/github.com/go-yaml/yaml/emitterc.go b/kyaml/internal/forked/github.com/go-yaml/yaml/emitterc.go index 0f47c9ca8a..caba6e3380 100644 --- a/kyaml/internal/forked/github.com/go-yaml/yaml/emitterc.go +++ b/kyaml/internal/forked/github.com/go-yaml/yaml/emitterc.go @@ -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, compact_seq bool) bool { emitter.indents = append(emitter.indents, emitter.indent) if emitter.indent < 0 { if flow { @@ -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 compact_seq { + emitter.indent = emitter.indent - 2 + } } return true } @@ -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++ @@ -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++ @@ -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.compact_sequence_indent + if !yaml_emitter_increase_indent(emitter, false, false, seq){ return false } } @@ -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 } } @@ -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) { diff --git a/kyaml/internal/forked/github.com/go-yaml/yaml/encode_test.go b/kyaml/internal/forked/github.com/go-yaml/yaml/encode_test.go index 4a8bf2e264..3137818274 100644 --- a/kyaml/internal/forked/github.com/go-yaml/yaml/encode_test.go +++ b/kyaml/internal/forked/github.com/go-yaml/yaml/encode_test.go @@ -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) TestCompactSeqIndentDefault(c *C) { + var buf bytes.Buffer + enc := yaml.NewEncoder(&buf) + enc.CompactSeqIndent() + 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) TestCompactSequenceWithSetIndent(c *C) { + var buf bytes.Buffer + enc := yaml.NewEncoder(&buf) + enc.CompactSeqIndent() + 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, diff --git a/kyaml/internal/forked/github.com/go-yaml/yaml/go.mod b/kyaml/internal/forked/github.com/go-yaml/yaml/go.mod index f407ea3213..a4b736a5a6 100644 --- a/kyaml/internal/forked/github.com/go-yaml/yaml/go.mod +++ b/kyaml/internal/forked/github.com/go-yaml/yaml/go.mod @@ -2,4 +2,4 @@ module "gopkg.in/yaml.v3" require ( "gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405 -) +) \ No newline at end of file diff --git a/kyaml/internal/forked/github.com/go-yaml/yaml/yaml.go b/kyaml/internal/forked/github.com/go-yaml/yaml/yaml.go index 8cec6da48d..447b7fe2b2 100644 --- a/kyaml/internal/forked/github.com/go-yaml/yaml/yaml.go +++ b/kyaml/internal/forked/github.com/go-yaml/yaml/yaml.go @@ -278,6 +278,11 @@ func (e *Encoder) SetIndent(spaces int) { e.encoder.indent = spaces } +// CompactSeqIndent makes it so that '- ' is considered part of the indentation. +func (e *Encoder) CompactSeqIndent() { + e.encoder.emitter.compact_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) { diff --git a/kyaml/internal/forked/github.com/go-yaml/yaml/yamlh.go b/kyaml/internal/forked/github.com/go-yaml/yaml/yamlh.go index 7c6d007706..40c74de497 100644 --- a/kyaml/internal/forked/github.com/go-yaml/yaml/yamlh.go +++ b/kyaml/internal/forked/github.com/go-yaml/yaml/yamlh.go @@ -742,6 +742,8 @@ type yaml_emitter_t struct { indent int // The current indentation level. + compact_sequence_indent bool // Is '- ' is considered part of the indentation for sequence elements? + flow_level int // The current flow level. root_context bool // Is it the document root context?