Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a flag to indent or not indent the sequence #750

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VS Code is a popular editor. I was surprised that there was not a gitignore for this.

4 changes: 2 additions & 2 deletions emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool
emitter.indent += 2
} else {
// Everything else aligns to the chosen indentation.
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent)
}
}
return true
Expand Down Expand Up @@ -728,7 +728,7 @@ 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) {
if !yaml_emitter_increase_indent(emitter, false, emitter.sameLevelSequence) {
return false
}
}
Expand Down
14 changes: 8 additions & 6 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import (
)

type encoder struct {
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
doneInit bool
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
sameLevelSequence bool
doneInit bool
}

func newEncoder() *encoder {
Expand All @@ -60,6 +61,7 @@ func (e *encoder) init() {
if e.indent == 0 {
e.indent = 4
}
e.emitter.sameLevelSequence = e.sameLevelSequence
e.emitter.best_indent = e.indent
yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)
e.emit()
Expand Down
12 changes: 12 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,18 @@ func (s *S) TestSetIndent(c *C) {
c.Assert(buf.String(), Equals, "a:\n b:\n c: d\n")
}

// This test is providing coverage for SetIndentSequences function.
// When we are setting that to false the sequences are not indented.
func (s *S) TestSetIndentSequences(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndentSequences(false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be a test for setting it to true?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's actually true (well the inverse under the hood) by default so the above tests for sequences cover it. We can add an explicit set to true and make sure it works. It's not a bad idea.


err := enc.Encode(map[string][]string{"a": {"b", "c"}})
c.Assert(err, Equals, nil)
c.Assert(buf.String(), Equals, "a:\n- b\n- c\n")
}

func (s *S) TestSortedOutput(c *C) {
order := []interface{}{
false,
Expand Down
27 changes: 24 additions & 3 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,28 @@ func (e *Encoder) SetIndent(spaces int) {
e.encoder.indent = spaces
}

// SetIndentSequences allows you to control how the sequences are indented
// in in v3 sequences started getting indented like this:
//
// fruits
// - apple
// - orange
//
// set this flag to false in order to stop indenting sequences and get output
// like this:
//
// fruits
// - apple
// - orange
func (e *Encoder) SetIndentSequences(indent bool) {
// NOTE: why is this backwards?
// The reason is that encoders get initialized at various places
// and when people use Marshall they don't get a chance to call this
// function. So we choose a boolean with a meaningful value of false
// for backwards compatibility with exiting v3 clients.
e.encoder.sameLevelSequence = !indent
}

// 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 Expand Up @@ -363,7 +385,7 @@ const (
// Address yaml.Node
// }
// err := yaml.Unmarshal(data, &person)
//
//
// Or by itself:
//
// var person Node
Expand All @@ -373,7 +395,7 @@ type Node struct {
// Kind defines whether the node is a document, a mapping, a sequence,
// a scalar value, or an alias to another node. The specific data type of
// scalar nodes may be obtained via the ShortTag and LongTag methods.
Kind Kind
Kind Kind

// Style allows customizing the apperance of the node in the tree.
Style Style
Expand Down Expand Up @@ -421,7 +443,6 @@ func (n *Node) IsZero() bool {
n.HeadComment == "" && n.LineComment == "" && n.FootComment == "" && n.Line == 0 && n.Column == 0
}


// LongTag returns the long form of the tag that indicates the data type for
// the node. If the Tag field isn't explicitly defined, one will be computed
// based on the node properties.
Expand Down
4 changes: 2 additions & 2 deletions yamlh.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,6 @@ type yaml_parser_t struct {
}

type yaml_comment_t struct {

scan_mark yaml_mark_t // Position where scanning for comments started
token_mark yaml_mark_t // Position after which tokens will be associated with this comment
start_mark yaml_mark_t // Position of '#' comment mark
Expand Down Expand Up @@ -740,7 +739,8 @@ type yaml_emitter_t struct {

tag_directives []yaml_tag_directive_t // The list of tag directives.

indent int // The current indentation level.
indent int // The current indentation level.
sameLevelSequence bool // Keep sequences at same level fo indent

flow_level int // The current flow level.

Expand Down