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

Fix decode nodes included anchors #237

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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