Skip to content

Commit

Permalink
Fix error reporting on inline structs in strict mode (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinmit committed Aug 10, 2021
1 parent 578512e commit 367f635
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ func (d *Decoder) decodeStruct(ctx context.Context, dst reflect.Value, src ast.N
err = nil
}

if err = d.deleteStructKeys(fieldValue.Type(), 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 @@ -1454,6 +1454,32 @@ c: true
}
}

func TestDecoder_InlineAndWrongTypeStrict(t *testing.T) {
type Base struct {
A int
B string
}
yml := `---
a: notanint
b: hello
c: true
`
var v struct {
*Base `yaml:",inline"`
C bool
}
err := yaml.NewDecoder(strings.NewReader(yml), yaml.Strict()).Decode(&v)
if err == nil {
t.Fatalf("expected error")
}

//TODO: properly check if errors are colored/have source
t.Logf("%s", err)
t.Logf("%s", yaml.FormatError(err, true, false))
t.Logf("%s", yaml.FormatError(err, false, true))
t.Logf("%s", yaml.FormatError(err, true, true))
}

func TestDecoder_InvalidCases(t *testing.T) {
const src = `---
a:
Expand Down

0 comments on commit 367f635

Please sign in to comment.