From 7649d4548cb53a614db133b2a8ac1f31859dda8c Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Tue, 17 Nov 2020 15:46:20 +0000 Subject: [PATCH] Revert v2 line length change as discussed in #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. --- apic.go | 6 +++++- encode_test.go | 26 +++++++++++++++++++++----- go.mod | 8 ++++---- yaml.go | 12 ++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/apic.go b/apic.go index d2c2308f..acf71402 100644 --- a/apic.go +++ b/apic.go @@ -79,6 +79,8 @@ func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) { parser.encoding = encoding } +var disableLineWrapping = false + // Create a new emitter object. func yaml_emitter_initialize(emitter *yaml_emitter_t) { *emitter = yaml_emitter_t{ @@ -86,7 +88,9 @@ func yaml_emitter_initialize(emitter *yaml_emitter_t) { raw_buffer: make([]byte, 0, output_raw_buffer_size), states: make([]yaml_emitter_state_t, 0, initial_stack_size), events: make([]yaml_event_t, 0, initial_queue_size), - best_width: -1, + } + if disableLineWrapping { + emitter.best_width = -1 } } diff --git a/encode_test.go b/encode_test.go index f7c91ff0..c8caedc5 100644 --- a/encode_test.go +++ b/encode_test.go @@ -397,11 +397,27 @@ var marshalTests = []struct { map[string]interface{}{"a": jsonNumberT("bogus")}, "a: bogus\n", }, - // Ensure that strings do not wrap - { - map[string]string{"a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 "}, - "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n", - }, +} + +func (s *S) TestLineWrapping(c *C) { + var v = map[string]string{ + "a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ", + } + data, err := yaml.Marshal(v) + c.Assert(err, IsNil) + c.Assert(string(data), Equals, + "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz\n" + + " ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n") + + // The API does not allow this process to be reversed as it's intended + // for migration only. v3 drops this method and instead offers more + // control on a per encoding basis. + yaml.FutureLineWrap() + + data, err = yaml.Marshal(v) + c.Assert(err, IsNil) + c.Assert(string(data), Equals, + "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n") } func (s *S) TestMarshal(c *C) { diff --git a/go.mod b/go.mod index 1934e876..2cbb85ae 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ -module "gopkg.in/yaml.v2" +module gopkg.in/yaml.v2 -require ( - "gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405 -) +go 1.15 + +require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 diff --git a/yaml.go b/yaml.go index 65aa9dd0..30813884 100644 --- a/yaml.go +++ b/yaml.go @@ -464,3 +464,15 @@ func isZero(v reflect.Value) bool { } return false } + +// FutureLineWrap globally disables line wrapping when encoding long strings. +// This is a temporary and thus deprecated method introduced to faciliate +// migration towards v3, which offers more control of line lengths on +// individual encodings, and has a default matching the behavior introduced +// by this function. +// +// The default formatting of v2 was erroneously changed in v2.3.0 and reverted +// in v2.4.0, at which point this function was introduced to help migration. +func FutureLineWrap() { + disableLineWrapping = true +}