Skip to content

Commit

Permalink
fix embedded pointers
Browse files Browse the repository at this point in the history
Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com>
  • Loading branch information
inteon committed Oct 21, 2021
1 parent 9cce9fd commit 2a9c1a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ cast_type:
// struct field.
jtf := t
for _, i := range f.index {
if jtf.Kind() == reflect.Ptr {
if jtf.IsNil() {
jtf = reflect.New(jtf.Type().Elem())
}
jtf = jtf.Elem()
}
jtf = jtf.Field(i)
}

Expand Down
13 changes: 13 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ type WithEmbed struct {
B string `json:"b"`
}

type WithPtrEmbed struct {
*Inner
B string `json:"b"`
}

type unmarshalTestCase struct {
encoded []byte
decodeInto interface{}
Expand Down Expand Up @@ -205,6 +210,14 @@ func TestUnmarshal(t *testing.T) {
B: "11",
},
},
{
encoded: []byte("a: 10\nb: 11\n"),
decodeInto: new(WithPtrEmbed),
decoded: WithPtrEmbed{
Inner: &Inner{A: "10"},
B: "11",
},
},
{
encoded: []byte("a: 3.0\nb: 3\n"),
decodeInto: new(interface{}),
Expand Down

0 comments on commit 2a9c1a2

Please sign in to comment.