Internal copy of go-yaml at f6f7691b1fdeb513f56608cd2c32c51f8194bf51

This commit is contained in:
Katrina Verey
2022-08-17 16:09:00 -04:00
parent 26fcafdb57
commit d0ae8fba13
12 changed files with 214 additions and 237 deletions

View File

@@ -27,7 +27,7 @@ import (
"os"
. "gopkg.in/check.v1"
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"
"gopkg.in/yaml.v3"
)
var marshalIntTest = 123
@@ -35,142 +35,109 @@ var marshalIntTest = 123
var marshalTests = []struct {
value interface{}
data string
compact string
}{
{
nil,
"null\n",
"null\n",
}, {
(*marshalerType)(nil),
"null\n",
"null\n",
}, {
&struct{}{},
"{}\n",
"{}\n",
}, {
map[string]string{"v": "hi"},
"v: hi\n",
"v: hi\n",
}, {
map[string]interface{}{"v": "hi"},
"v: hi\n",
"v: hi\n",
}, {
map[string]string{"v": "true"},
"v: \"true\"\n",
"v: \"true\"\n",
}, {
map[string]string{"v": "false"},
"v: \"false\"\n",
"v: \"false\"\n",
}, {
map[string]interface{}{"v": true},
"v: true\n",
"v: true\n",
}, {
map[string]interface{}{"v": false},
"v: false\n",
"v: false\n",
}, {
map[string]interface{}{"v": 10},
"v: 10\n",
"v: 10\n",
}, {
map[string]interface{}{"v": -10},
"v: -10\n",
"v: -10\n",
}, {
map[string]uint{"v": 42},
"v: 42\n",
"v: 42\n",
}, {
map[string]interface{}{"v": int64(4294967296)},
"v: 4294967296\n",
"v: 4294967296\n",
}, {
map[string]int64{"v": int64(4294967296)},
"v: 4294967296\n",
"v: 4294967296\n",
}, {
map[string]uint64{"v": 4294967296},
"v: 4294967296\n",
"v: 4294967296\n",
}, {
map[string]interface{}{"v": "10"},
"v: \"10\"\n",
"v: \"10\"\n",
}, {
map[string]interface{}{"v": 0.1},
"v: 0.1\n",
"v: 0.1\n",
}, {
map[string]interface{}{"v": float64(0.1)},
"v: 0.1\n",
"v: 0.1\n",
}, {
map[string]interface{}{"v": float32(0.99)},
"v: 0.99\n",
"v: 0.99\n",
}, {
map[string]interface{}{"v": -0.1},
"v: -0.1\n",
"v: -0.1\n",
}, {
map[string]interface{}{"v": math.Inf(+1)},
"v: .inf\n",
"v: .inf\n",
}, {
map[string]interface{}{"v": math.Inf(-1)},
"v: -.inf\n",
"v: -.inf\n",
}, {
map[string]interface{}{"v": math.NaN()},
"v: .nan\n",
"v: .nan\n",
}, {
map[string]interface{}{"v": nil},
"v: null\n",
"v: null\n",
}, {
map[string]interface{}{"v": ""},
"v: \"\"\n",
"v: \"\"\n",
}, {
map[string][]string{"v": {"A", "B"}},
map[string][]string{"v": []string{"A", "B"}},
"v:\n - A\n - B\n",
"v:\n - A\n - B\n",
}, {
map[string][]string{"v": {"A", "B\nC"}},
map[string][]string{"v": []string{"A", "B\nC"}},
"v:\n - A\n - |-\n B\n C\n",
"v:\n - A\n - |-\n B\n C\n",
}, {
map[string][]interface{}{"v": {"A", 1, map[string][]int{"B": {2, 3}}}},
map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}},
"v:\n - A\n - 1\n - B:\n - 2\n - 3\n",
"v:\n - A\n - 1\n - B:\n - 2\n - 3\n",
}, {
map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
"a:\n b: c\n",
"a:\n b: c\n",
}, {
map[string]interface{}{"a": "-"},
"a: '-'\n",
"a: '-'\n",
},
// Simple values.
{
&marshalIntTest,
"123\n",
"123\n",
},
// Structures
{
&struct{ Hello string }{"world"},
"hello: world\n",
"hello: world\n",
}, {
&struct {
A struct {
@@ -178,7 +145,6 @@ var marshalTests = []struct {
}
}{struct{ B string }{"c"}},
"a:\n b: c\n",
"a:\n b: c\n",
}, {
&struct {
A *struct {
@@ -186,7 +152,6 @@ var marshalTests = []struct {
}
}{&struct{ B string }{"c"}},
"a:\n b: c\n",
"a:\n b: c\n",
}, {
&struct {
A *struct {
@@ -194,37 +159,29 @@ var marshalTests = []struct {
}
}{},
"a: null\n",
"a: null\n",
}, {
&struct{ A int }{1},
"a: 1\n",
"a: 1\n",
}, {
&struct{ A []int }{[]int{1, 2}},
"a:\n - 1\n - 2\n",
"a:\n - 1\n - 2\n",
}, {
&struct{ A [2]int }{[2]int{1, 2}},
"a:\n - 1\n - 2\n",
"a:\n - 1\n - 2\n",
}, {
&struct {
B int "a"
}{1},
"a: 1\n",
"a: 1\n",
}, {
&struct{ A bool }{true},
"a: true\n",
"a: true\n",
}, {
&struct{ A string }{"true"},
"a: \"true\"\n",
"a: \"true\"\n",
}, {
&struct{ A string }{"off"},
"a: \"off\"\n",
"a: \"off\"\n",
},
// Conditional flag
@@ -234,51 +191,43 @@ var marshalTests = []struct {
B int "b,omitempty"
}{1, 0},
"a: 1\n",
"a: 1\n",
}, {
&struct {
A int "a,omitempty"
B int "b,omitempty"
}{0, 0},
"{}\n",
"{}\n",
}, {
&struct {
A *struct{ X, y int } "a,omitempty,flow"
}{&struct{ X, y int }{1, 2}},
"a: {x: 1}\n",
"a: {x: 1}\n",
}, {
&struct {
A *struct{ X, y int } "a,omitempty,flow"
}{nil},
"{}\n",
"{}\n",
}, {
&struct {
A *struct{ X, y int } "a,omitempty,flow"
}{&struct{ X, y int }{}},
"a: {x: 0}\n",
"a: {x: 0}\n",
}, {
&struct {
A struct{ X, y int } "a,omitempty,flow"
}{struct{ X, y int }{1, 2}},
"a: {x: 1}\n",
"a: {x: 1}\n",
}, {
&struct {
A struct{ X, y int } "a,omitempty,flow"
}{struct{ X, y int }{0, 1}},
"{}\n",
"{}\n",
}, {
&struct {
A float64 "a,omitempty"
B float64 "b,omitempty"
}{1, 0},
"a: 1\n",
"a: 1\n",
},
{
&struct {
@@ -291,7 +240,6 @@ var marshalTests = []struct {
T4: newTime(time.Date(2098, 1, 9, 10, 40, 47, 0, time.UTC)),
},
"t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n",
"t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n",
},
// Nil interface that implements Marshaler.
{
@@ -299,7 +247,6 @@ var marshalTests = []struct {
"a": nil,
},
"a: null\n",
"a: null\n",
},
// Flow flag
@@ -308,13 +255,11 @@ var marshalTests = []struct {
A []int "a,flow"
}{[]int{1, 2}},
"a: [1, 2]\n",
"a: [1, 2]\n",
}, {
&struct {
A map[string]string "a,flow"
}{map[string]string{"b": "c", "d": "e"}},
"a: {b: c, d: e}\n",
"a: {b: c, d: e}\n",
}, {
&struct {
A struct {
@@ -322,13 +267,11 @@ var marshalTests = []struct {
} "a,flow"
}{struct{ B, D string }{"c", "e"}},
"a: {b: c, d: e}\n",
"a: {b: c, d: e}\n",
}, {
&struct {
A string "a,flow"
}{"b\nc"},
"a: \"b\\nc\"\n",
"a: \"b\\nc\"\n",
},
// Unexported field
@@ -338,7 +281,6 @@ var marshalTests = []struct {
A int
}{0, 1},
"a: 1\n",
"a: 1\n",
},
// Ignored field
@@ -348,7 +290,6 @@ var marshalTests = []struct {
B int "-"
}{1, 2},
"a: 1\n",
"a: 1\n",
},
// Struct inlining
@@ -358,7 +299,6 @@ var marshalTests = []struct {
C inlineB `yaml:",inline"`
}{1, inlineB{2, inlineC{3}}},
"a: 1\nb: 2\nc: 3\n",
"a: 1\nb: 2\nc: 3\n",
},
// Struct inlining as a pointer
{
@@ -367,21 +307,18 @@ var marshalTests = []struct {
C *inlineB `yaml:",inline"`
}{1, &inlineB{2, inlineC{3}}},
"a: 1\nb: 2\nc: 3\n",
"a: 1\nb: 2\nc: 3\n",
}, {
&struct {
A int
C *inlineB `yaml:",inline"`
}{1, nil},
"a: 1\n",
"a: 1\n",
}, {
&struct {
A int
D *inlineD `yaml:",inline"`
}{1, &inlineD{&inlineC{3}, 4}},
"a: 1\nc: 3\nd: 4\n",
"a: 1\nc: 3\nd: 4\n",
},
// Map inlining
@@ -391,21 +328,18 @@ var marshalTests = []struct {
C map[string]int `yaml:",inline"`
}{1, map[string]int{"b": 2, "c": 3}},
"a: 1\nb: 2\nc: 3\n",
"a: 1\nb: 2\nc: 3\n",
},
// Duration
{
map[string]time.Duration{"a": 3 * time.Second},
"a: 3s\n",
"a: 3s\n",
},
// Issue #24: bug in map merging logic.
{
map[string]string{"a": "<foo>"},
"a: <foo>\n",
"a: <foo>\n",
},
// Issue #34: marshal unsupported base 60 floats quoted for compatibility
@@ -413,110 +347,92 @@ var marshalTests = []struct {
{
map[string]string{"a": "1:1"},
"a: \"1:1\"\n",
"a: \"1:1\"\n",
},
// Binary data.
{
map[string]string{"a": "\x00"},
"a: \"\\0\"\n",
"a: \"\\0\"\n",
}, {
map[string]string{"a": "\x80\x81\x82"},
"a: !!binary gIGC\n",
"a: !!binary gIGC\n",
}, {
map[string]string{"a": strings.Repeat("\x90", 54)},
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
},
// Encode unicode as utf-8 rather than in escaped form.
{
map[string]string{"a": "你好"},
"a: 你好\n",
"a: 你好\n",
},
// Support encoding.TextMarshaler.
{
map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)},
"a: 1.2.3.4\n",
"a: 1.2.3.4\n",
},
// time.Time gets a timestamp tag.
{
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
"a: 2015-02-24T18:19:39Z\n",
"a: 2015-02-24T18:19:39Z\n",
},
{
map[string]*time.Time{"a": newTime(time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC))},
"a: 2015-02-24T18:19:39Z\n",
"a: 2015-02-24T18:19:39Z\n",
},
{
// This is confirmed to be properly decoded in Python (libyaml) without a timestamp tag.
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 123456789, time.FixedZone("FOO", -3*60*60))},
"a: 2015-02-24T18:19:39.123456789-03:00\n",
"a: 2015-02-24T18:19:39.123456789-03:00\n",
},
// Ensure timestamp-like strings are quoted.
{
map[string]string{"a": "2015-02-24T18:19:39Z"},
"a: \"2015-02-24T18:19:39Z\"\n",
"a: \"2015-02-24T18:19:39Z\"\n",
},
// Ensure strings containing ": " are quoted (reported as PR #43, but not reproducible).
{
map[string]string{"a": "b: c"},
"a: 'b: c'\n",
"a: 'b: c'\n",
},
// Containing hash mark ('#') in string should be quoted
{
map[string]string{"a": "Hello #comment"},
"a: 'Hello #comment'\n",
"a: 'Hello #comment'\n",
},
{
map[string]string{"a": "你好 #comment"},
"a: '你好 #comment'\n",
"a: '你好 #comment'\n",
},
// Ensure MarshalYAML also gets called on the result of MarshalYAML itself.
{
&marshalerType{marshalerType{true}},
"true\n",
"true\n",
}, {
&marshalerType{&marshalerType{true}},
"true\n",
"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",
"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",
"a: |\n \tB\n \tC\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",
"a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n",
},
// yaml.Node
@@ -532,7 +448,6 @@ var marshalTests = []struct {
},
},
"value: 'foo'\n",
"value: 'foo'\n",
}, {
yaml.Node{
Kind: yaml.ScalarNode,
@@ -541,7 +456,6 @@ var marshalTests = []struct {
Style: yaml.SingleQuotedStyle,
},
"'foo'\n",
"'foo'\n",
},
// Enforced tagging with shorthand notation (issue #616).
@@ -557,7 +471,6 @@ var marshalTests = []struct {
},
},
"value: !!str foo\n",
"value: !!str foo\n",
}, {
&struct {
Value yaml.Node
@@ -569,7 +482,6 @@ var marshalTests = []struct {
},
},
"value: !!map {}\n",
"value: !!map {}\n",
}, {
&struct {
Value yaml.Node
@@ -581,7 +493,6 @@ var marshalTests = []struct {
},
},
"value: !!seq []\n",
"value: !!seq []\n",
},
}
@@ -596,20 +507,6 @@ func (s *S) TestMarshal(c *C) {
}
}
func (s *S) TestEncoderCompactIndents(c *C) {
for i, item := range marshalTests {
c.Logf("test %d. %q", i, item.data)
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.CompactSeqIndent()
err := enc.Encode(item.value)
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
c.Assert(buf.String(), Equals, item.compact)
}
}
func (s *S) TestEncoderSingleDocument(c *C) {
for i, item := range marshalTests {
c.Logf("test %d. %q", i, item.data)
@@ -759,51 +656,6 @@ func (s *S) TestSetIndent(c *C) {
c.Assert(buf.String(), Equals, "a:\n b:\n c: d\n")
}
func (s *S) TestCompactSeqIndentDefault(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.CompactSeqIndent()
err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
// The default indent is 4, so these sequence elements get 2 indents as before
c.Assert(buf.String(), Equals, `a:
- b
- c
`)
}
func (s *S) TestCompactSequenceWithSetIndent(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.CompactSeqIndent()
enc.SetIndent(2)
err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
// The sequence indent is 2, so these sequence elements don't get indented at all
c.Assert(buf.String(), Equals, `a:
- b
- c
`)
}
func (s *S) TestNewLinePreserved(c *C) {
obj := &marshalerValue{}
obj.Field.value = "a:\n b:\n c: d\n"
data, err := yaml.Marshal(obj)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, "_: |\n a:\n b:\n c: d\n")
obj.Field.value = "\na:\n b:\n c: d\n"
data, err = yaml.Marshal(obj)
c.Assert(err, IsNil)
// the newline at the start of the file should be preserved
c.Assert(string(data), Equals, "_: |4\n\n a:\n b:\n c: d\n")
}
func (s *S) TestSortedOutput(c *C) {
order := []interface{}{
false,