diff --git a/kstructs/testhelpers.go b/kstructs/testhelpers.go index f4e3181..da5c66d 100644 --- a/kstructs/testhelpers.go +++ b/kstructs/testhelpers.go @@ -44,7 +44,7 @@ func FillStructWith(record interface{}, dbRow map[string]interface{}) error { if t.Kind() != reflect.Struct { return fmt.Errorf( - "FillStructWith: expected input kind to be a struct but got %T", + "FillStructWith: expected input to be a pointer to a struct, but got %T", record, ) } diff --git a/kstructs/testhelpers_test.go b/kstructs/testhelpers_test.go index aa81feb..4f91227 100644 --- a/kstructs/testhelpers_test.go +++ b/kstructs/testhelpers_test.go @@ -228,6 +228,53 @@ func TestFillStructWith(t *testing.T) { tt.AssertEqual(t, 42, user.Age) tt.AssertEqual(t, "should be untouched", user.Missing) }) + + t.Run("should report error if input is not a pointer", func(t *testing.T) { + type User struct { + Name string `ksql:"name"` + Age int `ksql:"age"` + Missing string `ksql:"missing"` + } + var user User + + err := FillStructWith(user, map[string]interface{}{ + "name": "fake name", + "age": 42, + "extra_field": "some value", + }) + + tt.AssertErrContains(t, err, "FillStructWith", "expected input to be a pointer", "User") + }) + + t.Run("should report error if input is not a pointer to struct", func(t *testing.T) { + type User struct { + Name string `ksql:"name"` + Age int `ksql:"age"` + Missing string `ksql:"missing"` + } + var users []User + + err := FillStructWith(&users, map[string]interface{}{ + "name": "fake name", + "age": 42, + "extra_field": "some value", + }) + + tt.AssertErrContains(t, err, "FillStructWith", "expected input to be a pointer to a struct", "User") + }) + + t.Run("should report error if input and target types are incompatible", func(t *testing.T) { + type User struct { + Age int `ksql:"age"` + } + var user User + + err := FillStructWith(&user, map[string]interface{}{ + "age": "not compatible with integer type", + }) + + tt.AssertErrContains(t, err, "FillStructWith", "age", "string", "int") + }) } func TestFillSliceWith(t *testing.T) {