Skip to content

Commit

Permalink
Merge pull request #237 from zoncoen/node-to-value-anchor
Browse files Browse the repository at this point in the history
Fix decode nodes included anchors
  • Loading branch information
goccy authored Jul 20, 2021
2 parents 4aa259b + 416f941 commit 52e99f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,8 @@ func (d *Decoder) DecodeFromNodeContext(ctx context.Context, node ast.Node, v in
return errors.Wrapf(err, "failed to decodInit")
}
}
// resolve references to the anchor on the same file
d.nodeToValue(node)
if err := d.decodeValue(ctx, rv.Elem(), node); err != nil {
return errors.Wrapf(err, "failed to decode value")
}
Expand Down
26 changes: 25 additions & 1 deletion decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,31 @@ func Test_UnmarshalerContext(t *testing.T) {
}

func TestDecoder_DecodeFromNode(t *testing.T) {
t.Run("with reference", func(t *testing.T) {
t.Run("has reference", func(t *testing.T) {
str := `
anchor: &map
text: hello
map: *map`
var buf bytes.Buffer
dec := yaml.NewDecoder(&buf)
f, err := parser.ParseBytes([]byte(str), 0)
if err != nil {
t.Fatalf("failed to parse: %s", err)
}
type T struct {
Map map[string]string
}
var v T
if err := dec.DecodeFromNode(f.Docs[0].Body, &v); err != nil {
t.Fatalf("failed to decode: %s", err)
}
actual := fmt.Sprintf("%+v", v)
expect := fmt.Sprintf("%+v", T{map[string]string{"text": "hello"}})
if actual != expect {
t.Fatalf("actual=[%s], expect=[%s]", actual, expect)
}
})
t.Run("with reference option", func(t *testing.T) {
anchor := strings.NewReader(`
map: &map
text: hello`)
Expand Down

0 comments on commit 52e99f4

Please sign in to comment.