From dfaed9d8f96e8e3307eb0361f37212ed7179b2ef Mon Sep 17 00:00:00 2001 From: "Rick Kilgore (TXM)" Date: Mon, 11 Sep 2023 20:04:26 -0700 Subject: [PATCH 1/2] return !!binary as []byte (but still support String as well) --- decode.go | 6 +++++- decode_test.go | 26 +++++++++++++++++--------- go.mod | 8 ++++---- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/decode.go b/decode.go index 0173b698..361dd3ca 100644 --- a/decode.go +++ b/decode.go @@ -575,7 +575,11 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool { if err != nil { failf("!!binary value contains invalid base64 data") } - resolved = string(data) + if out.Kind() == reflect.String { + resolved = string(data) + } else { + resolved = data + } } } if resolved == nil { diff --git a/decode_test.go b/decode_test.go index 0364b0bb..2c219d7c 100644 --- a/decode_test.go +++ b/decode_test.go @@ -633,13 +633,13 @@ var unmarshalTests = []struct { // Binary data. { "a: !!binary gIGC\n", - map[string]string{"a": "\x80\x81\x82"}, + map[string][]byte{"a": {0x80, 0x81, 0x82}}, }, { "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", - map[string]string{"a": strings.Repeat("\x90", 54)}, + map[string][]byte{"a": arrayOf(0x90, 54)}, }, { "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n", - map[string]string{"a": strings.Repeat("\x00", 52)}, + map[string][]byte{"a": arrayOf(0x00, 52)}, }, // Issue #39. @@ -804,6 +804,14 @@ var unmarshalTests = []struct { }, } +func arrayOf(value byte, length int) []byte { + var array []byte + for i := 0; i < length; i++ { + array = append(array, value) + } + return array +} + type M map[string]interface{} type inlineB struct { @@ -947,7 +955,7 @@ var unmarshalErrorTests = []struct { {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"}, {"a:\n 1:\nb\n 2:", ".*could not find expected ':'"}, {"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"}, - {"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665 + {"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665 {"0: [:!00 \xef", "yaml: incomplete UTF-8 octet sequence"}, // Issue #666 { "a: &a [00,00,00,00,00,00,00,00,00]\n" + @@ -1482,7 +1490,7 @@ func (s *S) TestMergeNestedStruct(c *C) { // 2) A simple implementation might attempt to handle the key skipping // directly by iterating over the merging map without recursion, but // there are more complex cases that require recursion. - // + // // Quick summary of the fields: // // - A must come from outer and not overriden @@ -1498,7 +1506,7 @@ func (s *S) TestMergeNestedStruct(c *C) { A, B, C int } type Outer struct { - D, E int + D, E int Inner Inner Inline map[string]int `yaml:",inline"` } @@ -1516,10 +1524,10 @@ func (s *S) TestMergeNestedStruct(c *C) { // Repeat test with a map. var testm map[string]interface{} - var wantm = map[string]interface {} { - "f": 60, + var wantm = map[string]interface{}{ + "f": 60, "inner": map[string]interface{}{ - "a": 10, + "a": 10, }, "d": 40, "e": 50, diff --git a/go.mod b/go.mod index f407ea32..bd62d3a9 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ -module "gopkg.in/yaml.v3" +module gopkg.in/yaml.v3 -require ( - "gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405 -) +go 1.20 + +require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 From 2c9c473a3d745a854a98bc68df1b9b6108b93b90 Mon Sep 17 00:00:00 2001 From: "Rick Kilgore (TXM)" Date: Mon, 11 Sep 2023 20:22:10 -0700 Subject: [PATCH 2/2] put go.mod back like it was --- go.mod | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index bd62d3a9..f407ea32 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ -module gopkg.in/yaml.v3 +module "gopkg.in/yaml.v3" -go 1.20 - -require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 +require ( + "gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405 +)