Skip to content

Commit 7649d45

Browse files
committed
Revert v2 line length change as discussed in go-yaml#670
It was clearly a mistake to accept the default formatting change in v2, and now there's no ideal choice. Either we revert the change and break some projects twice, or we keep the change and break some projects once. Given the report comes from Kubernetes, which has a relevant community and code size, we'll revert it. At the same time, to simplify the life of those that already started migrating towards the v3 behavior, a new FutureLineWrap function is being introduced to trivially preserve the new behavior where desired. The v3 branch is not affected by this, and will retain the default non-wrapping behavior. It will also be changed soon to support per arbitrary line-wrapping for individual encoding operations. Thanks to everyone who offered code and ideas, and apologies for the trouble.
1 parent b893565 commit 7649d45

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

apic.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,18 @@ func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) {
7979
parser.encoding = encoding
8080
}
8181

82+
var disableLineWrapping = false
83+
8284
// Create a new emitter object.
8385
func yaml_emitter_initialize(emitter *yaml_emitter_t) {
8486
*emitter = yaml_emitter_t{
8587
buffer: make([]byte, output_buffer_size),
8688
raw_buffer: make([]byte, 0, output_raw_buffer_size),
8789
states: make([]yaml_emitter_state_t, 0, initial_stack_size),
8890
events: make([]yaml_event_t, 0, initial_queue_size),
89-
best_width: -1,
91+
}
92+
if disableLineWrapping {
93+
emitter.best_width = -1
9094
}
9195
}
9296

encode_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,27 @@ var marshalTests = []struct {
397397
map[string]interface{}{"a": jsonNumberT("bogus")},
398398
"a: bogus\n",
399399
},
400-
// Ensure that strings do not wrap
401-
{
402-
map[string]string{"a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 "},
403-
"a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n",
404-
},
400+
}
401+
402+
func (s *S) TestLineWrapping(c *C) {
403+
var v = map[string]string{
404+
"a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ",
405+
}
406+
data, err := yaml.Marshal(v)
407+
c.Assert(err, IsNil)
408+
c.Assert(string(data), Equals,
409+
"a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz\n" +
410+
" ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n")
411+
412+
// The API does not allow this process to be reversed as it's intended
413+
// for migration only. v3 drops this method and instead offers more
414+
// control on a per encoding basis.
415+
yaml.FutureLineWrap()
416+
417+
data, err = yaml.Marshal(v)
418+
c.Assert(err, IsNil)
419+
c.Assert(string(data), Equals,
420+
"a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n")
405421
}
406422

407423
func (s *S) TestMarshal(c *C) {

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module "gopkg.in/yaml.v2"
1+
module gopkg.in/yaml.v2
22

3-
require (
4-
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
5-
)
3+
go 1.15
4+
5+
require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405

yaml.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,15 @@ func isZero(v reflect.Value) bool {
464464
}
465465
return false
466466
}
467+
468+
// FutureLineWrap globally disables line wrapping when encoding long strings.
469+
// This is a temporary and thus deprecated method introduced to faciliate
470+
// migration towards v3, which offers more control of line lengths on
471+
// individual encodings, and has a default matching the behavior introduced
472+
// by this function.
473+
//
474+
// The default formatting of v2 was erroneously changed in v2.3.0 and reverted
475+
// in v2.4.0, at which point this function was introduced to help migration.
476+
func FutureLineWrap() {
477+
disableLineWrapping = true
478+
}

0 commit comments

Comments
 (0)