Skip to content

Commit

Permalink
Merge pull request #63 from kitagry/fix-inline-array
Browse files Browse the repository at this point in the history
Fix DisallowUnknownField error and Refactoring
  • Loading branch information
goccy authored Dec 18, 2019
2 parents 29d8e1d + 3032e43 commit 4f369ea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
30 changes: 16 additions & 14 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,20 @@ func (d *Decoder) decodeStruct(dst reflect.Value, src ast.Node) error {
continue
}
newFieldValue := d.createDecodableValue(fieldValue.Type())
if err := d.decodeValue(newFieldValue, src); err != nil {
err := d.decodeValue(newFieldValue, src)

if d.disallowUnknownField {
var ufe *unknownFieldError
if xerrors.As(err, &ufe) {
err = nil
}

if err = d.deleteStructKeys(fieldValue, unknownFields); err != nil {
return errors.Wrapf(err, "cannot delete struct keys")
}
}

if err != nil {
if foundErr != nil {
continue
}
Expand All @@ -618,21 +631,10 @@ func (d *Decoder) decodeStruct(dst reflect.Value, src ast.Node) error {
}
foundErr = te
continue
}

if d.disallowUnknownField {
var ufe *unknownFieldError
if !xerrors.As(err, &ufe) {
foundErr = err
continue
}

if err = d.deleteStructKeys(fieldValue, unknownFields); err != nil {
return errors.Wrapf(err, "cannot delete struct keys")
}
} else {
continue
foundErr = err
}
continue
}
d.setDefaultValueIfConflicted(newFieldValue, structFieldMap)
fieldValue.Set(d.castToAssignableValue(newFieldValue, fieldValue.Type()))
Expand Down
31 changes: 31 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,37 @@ b: 1
t.Fatalf("v.C should be 0, got %d", v.C)
}
})
t.Run("list", func(t *testing.T) {
type C struct {
Child `yaml:",inline"`
}

var v struct {
Children []C `yaml:"children"`
}

yml := `---
children:
- b: 1
- b: 2
`

if err := yaml.NewDecoder(strings.NewReader(yml), yaml.DisallowUnknownField()).Decode(&v); err != nil {
t.Fatalf(`parsing should succeed: %s`, err)
}

if len(v.Children) != 2 {
t.Fatalf(`len(v.Children) should be 2, got %d`, len(v.Children))
}

if v.Children[0].B != 1 {
t.Fatalf(`v.Children[0].B should be 1, got %d`, v.Children[0].B)
}

if v.Children[1].B != 2 {
t.Fatalf(`v.Children[1].B should be 2, got %d`, v.Children[1].B)
}
})
}

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

0 comments on commit 4f369ea

Please sign in to comment.