From 367f635c8375b209ad569b9f4effabda8438cc70 Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Tue, 10 Aug 2021 00:33:19 -0400 Subject: [PATCH] Fix error reporting on inline structs in strict mode (#243) --- decode.go | 2 +- decode_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/decode.go b/decode.go index ab85e74..cb07a84 100644 --- a/decode.go +++ b/decode.go @@ -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") } } diff --git a/decode_test.go b/decode_test.go index 46317fd..d6da735 100644 --- a/decode_test.go +++ b/decode_test.go @@ -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: