Skip to content

Commit

Permalink
add Encoder.SetLineLength API and change imports
Browse files Browse the repository at this point in the history
This adds the SetLineLength method to Encoder and changes the imports to the
jmhodges/yaml.v2 config.

The SetLineLength patch is adapated from
go-yaml#455

The README got an update for folks needing this.
  • Loading branch information
jmhodges committed Oct 17, 2019
1 parent 970885f commit f2db5cd
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ go:
- 1.9
- tip

go_import_path: gopkg.in/yaml.v2
go_import_path: gopkg.in/jmhodges/yaml.v2
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Introduction
------------

This is a fork of gopkg.in/yaml.v2 that adds the `SetLineLength` method to
`Encoder`. The patch was taken from https://github.com/go-yaml/yaml/pull/455

The yaml package enables Go programs to comfortably encode and decode YAML
values. It was developed within [Canonical](https://www.canonical.com) as
part of the [juju](https://juju.ubuntu.com) project, and is based on a
Expand All @@ -20,18 +23,18 @@ supported since they're a poor design and are gone in YAML 1.2.
Installation and usage
----------------------

The import path for the package is *gopkg.in/yaml.v2*.
The import path for the package is *gopkg.in/jmhodges/yaml.v2*.

To install it, run:

go get gopkg.in/yaml.v2
go get gopkg.in/jmhodges/yaml.v2

API documentation
-----------------

If opened in a browser, the import path itself leads to the API documentation:

* [https://gopkg.in/yaml.v2](https://gopkg.in/yaml.v2)
* [https://gopkg.in/jmhodges/yaml.v2](https://gopkg.in/jmhodges/yaml.v2)

API stability
-------------
Expand All @@ -55,7 +58,7 @@ import (
"fmt"
"log"

"gopkg.in/yaml.v2"
"gopkg.in/jmhodges/yaml.v2"
)

var data = `
Expand Down
18 changes: 9 additions & 9 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"gopkg.in/jmhodges/yaml.v2"
)

var unmarshalIntTest = 123
Expand Down Expand Up @@ -856,14 +856,14 @@ var unmarshalErrorTests = []struct {
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
{
"a: &a [00,00,00,00,00,00,00,00,00]\n" +
"b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" +
"c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" +
"d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" +
"e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" +
"f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" +
"g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" +
"h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" +
"i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n",
"b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" +
"c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" +
"d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" +
"e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" +
"f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" +
"g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" +
"h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" +
"i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n",
"yaml: document contains excessive aliasing",
},
}
Expand Down
4 changes: 4 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,7 @@ func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_
e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
e.emit()
}

func (e *encoder) setWidth(width int) {
yaml_emitter_set_width(&e.emitter, width)
}
29 changes: 28 additions & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"gopkg.in/jmhodges/yaml.v2"
)

type jsonNumberT string
Expand Down Expand Up @@ -397,6 +397,11 @@ var marshalTests = []struct {
map[string]interface{}{"a": jsonNumberT("bogus")},
"a: bogus\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 @@ -548,6 +553,28 @@ func (s *S) TestMarshalerError(c *C) {
c.Assert(err, Equals, failingErr)
}

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
2 changes: 1 addition & 1 deletion example_embedded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"gopkg.in/yaml.v2"
"gopkg.in/jmhodges/yaml.v2"
)

// An example showing how to unmarshal embedded
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module "gopkg.in/yaml.v2"
module gopkg.in/jmhodges/yaml.v2

require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
)
require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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=
2 changes: 1 addition & 1 deletion limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"gopkg.in/jmhodges/yaml.v2"
)

var limitTests = []struct {
Expand Down
6 changes: 6 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ func (e *Encoder) Encode(v interface{}) (err error) {
return nil
}

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

0 comments on commit f2db5cd

Please sign in to comment.