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

Feature (v3): Add a function to set the desired line length of an Encoder #455

Closed
wants to merge 8 commits into from
4 changes: 4 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,7 @@ func (e *encoder) node(node *Node, tail string) {
e.emitScalar(value, node.Anchor, tag, style, []byte(node.HeadComment), []byte(node.LineComment), []byte(node.FootComment), []byte(tail))
}
}

func (e *encoder) setWidth(width int) {
yaml_emitter_set_width(&e.emitter, width)
}
29 changes: 27 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,18 +410,21 @@ var marshalTests = []struct {
&marshalerType{&marshalerType{true}},
"true\n",
},

// Check indentation of maps inside sequences inside maps.
{
map[string]interface{}{"a": map[string]interface{}{"b": []map[string]int{{"c": 1, "d": 2}}}},
"a:\n b:\n - c: 1\n d: 2\n",
},

// Strings with tabs were disallowed as literals (issue #471).
{
map[string]string{"a": "\tB\n\tC\n"},
"a: |\n \tB\n \tC\n",
},
// Ensure that strings wrap
{
map[string]string{"a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 "},
"a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz\n ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n",
},
}

func (s *S) TestMarshal(c *C) {
Expand Down Expand Up @@ -584,6 +587,28 @@ func (s *S) TestSetIndent(c *C) {
c.Assert(buf.String(), Equals, "a:\n b:\n c: d\n")
}

func (s *S) TestSetSmallWrapLength(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetLineLength(10)
err := enc.Encode(map[string]interface{}{"a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 "})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
c.Assert(buf.String(), Equals, "a: 'abcdefghijklmnopqrstuvwxyz\n ABCDEFGHIJKLMNOPQRSTUVWXYZ\n 1234567890\n abcdefghijklmnopqrstuvwxyz\n ABCDEFGHIJKLMNOPQRSTUVWXYZ\n 1234567890 '\n")
}

func (s *S) TestSetNegativeWrapLength(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetLineLength(-1)
err := enc.Encode(map[string]interface{}{"a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 "})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
c.Assert(buf.String(), Equals, "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n")
}

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

// SetLineLength changes the desired line wrapping length.
// if characters is negative, '1<<31 - 1' is used.
func (e *Encoder) SetLineLength(characters int) {
e.encoder.setWidth(characters)
}

// 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