Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/sequence field singleton #974

Open
wants to merge 4 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,13 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool {
out.Set(resolvedv)
return true
}
case reflect.Slice, reflect.Array:
// This (likely) indicates the source value is a sequence with only a singular value (e.g. want `field: [a, b, c]`, got `field: a`),
// but does not match the expected "clues" to fully identify it as a sequence. Attempt to "re-class" the node as a sequence
// and process it as such.
n.Kind = SequenceNode
n.Content = []*Node{{Kind: ScalarNode, Value: n.Value}}
return d.sequence(n, out)
case reflect.Ptr:
panic("yaml internal error: please report the issue")
}
Expand Down
17 changes: 10 additions & 7 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ var unmarshalTests = []struct {
}, {
"seq: [A,1,C]",
map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
},
}, /*{
"seq: [A]",
map[string]string{"seq": "A"},
},*/
// Block sequence
{
"seq:\n - A\n - B",
Expand Down Expand Up @@ -947,7 +950,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" +
Expand Down Expand Up @@ -1482,7 +1485,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
Expand All @@ -1498,7 +1501,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"`
}
Expand All @@ -1516,10 +1519,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,
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module "gopkg.in/yaml.v3"
module gopkg.in/yaml.v3

go 1.20

require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c

require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.1.0 // indirect
)