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 error reporting on inline structs in strict mode (#243) #244

Merged
merged 1 commit into from
Aug 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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