From 5ae6b2e987975aa3fa8a084d5a8fe2c9e70d61af Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Sat, 17 Jul 2021 11:08:35 +0200 Subject: [PATCH] Better struct assert (#97) Signed-off-by: Oleg Kovalov --- aconfig_test.go | 7 ++++++- utils.go | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/aconfig_test.go b/aconfig_test.go index efb2cbf..2a91132 100644 --- a/aconfig_test.go +++ b/aconfig_test.go @@ -1061,7 +1061,7 @@ func TestDontFillFlagsIfDisabled(t *testing.T) { } } -func TestPassNonStructs(t *testing.T) { +func TestPassBadStructs(t *testing.T) { f := func(cfg interface{}) { t.Helper() @@ -1080,6 +1080,11 @@ func TestPassNonStructs(t *testing.T) { f([]string{}) f([4]string{}) f(func() {}) + + type S struct { + Foo int + } + f(S{}) } func TestBadRequiredTag(t *testing.T) { diff --git a/utils.go b/utils.go index 1e88eb1..c58fb41 100644 --- a/utils.go +++ b/utils.go @@ -15,6 +15,9 @@ func assertStruct(x interface{}) { panic("aconfig: nil should not be passed to the Loader") } value := reflect.ValueOf(x) + if value.Type().Kind() != reflect.Ptr { + panic("aconfig: destination must be a pointer") + } for value.Type().Kind() == reflect.Ptr { value = value.Elem() }