Skip to content

Commit

Permalink
Merge pull request #126 from goccy/feature/fix-issue125
Browse files Browse the repository at this point in the history
Fix reflection error in deleteStructKeys
  • Loading branch information
goccy authored Jun 9, 2020
2 parents 24e2c3f + c0e22be commit 559f2ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
7 changes: 3 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ func errDuplicateKey(msg string, tk *token.Token) *duplicateKeyError {
return &duplicateKeyError{err: errors.ErrSyntax(msg, tk)}
}

func (d *Decoder) deleteStructKeys(structValue reflect.Value, unknownFields map[string]ast.Node) error {
structType := structValue.Type()
func (d *Decoder) deleteStructKeys(structType reflect.Type, unknownFields map[string]ast.Node) error {
if structType.Kind() == reflect.Ptr {
structType = structType.Elem()
}
Expand All @@ -383,7 +382,7 @@ func (d *Decoder) deleteStructKeys(structValue reflect.Value, unknownFields map[
}

if structField.IsInline {
d.deleteStructKeys(structValue.FieldByName(field.Name), unknownFields)
d.deleteStructKeys(field.Type, unknownFields)
} else {
delete(unknownFields, structField.RenderName)
}
Expand Down Expand Up @@ -785,7 +784,7 @@ func (d *Decoder) decodeStruct(dst reflect.Value, src ast.Node) error {
err = nil
}

if err = d.deleteStructKeys(fieldValue, unknownFields); err != nil {
if err = d.deleteStructKeys(fieldValue.Type(), unknownFields); err != nil {
return errors.Wrapf(err, "cannot delete struct keys")
}
}
Expand Down
26 changes: 26 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,32 @@ c: true
if !v.C {
t.Fatal("failed to decode with inline key")
}

t.Run("multiple inline with strict", func(t *testing.T) {
type Base struct {
A int
B string
}
type Base2 struct {
Base *Base `yaml:",inline"`
}
yml := `---
a: 1
b: hello
`
var v struct {
Base2 *Base2 `yaml:",inline"`
}
if err := yaml.NewDecoder(strings.NewReader(yml), yaml.Strict()).Decode(&v); err != nil {
t.Fatalf("%+v", err)
}
if v.Base2.Base.A != 1 {
t.Fatal("failed to decode with inline key")
}
if v.Base2.Base.B != "hello" {
t.Fatal("failed to decode with inline key")
}
})
}

func TestDecoder_InlineAndConflictKey(t *testing.T) {
Expand Down

0 comments on commit 559f2ab

Please sign in to comment.