From 6326711424a5e4caaba25b69ffe34d688fbe73bf Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 30 Sep 2025 11:47:38 -0400 Subject: [PATCH] adding ok check to prevent panics --- lazy.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lazy.go b/lazy.go index 70c534f..eff2890 100644 --- a/lazy.go +++ b/lazy.go @@ -20,6 +20,7 @@ const ( var ( ErrBadExample = errors.New("example field must be a non-nil struct") ErrMissingKeys = errors.New("there must be a key for each row requested with RowCount") + ErrTypeCast = errors.New("unable to type cast the mock structs") //contains placeholder to inject type on runtime ErrUnsupportedType = errors.New("type: %s is not yet supported") ) @@ -42,7 +43,7 @@ type Config struct { type StructMock struct { Query string Args []driver.Value - MockStructs any + MockStructs []any Result driver.Result } @@ -156,10 +157,15 @@ func RandomStruct(c Config) (*StructMock, error) { } } + ms, ok := reflect.ValueOf(filled).Interface().([]any) + if !ok { + return nil, ErrTypeCast + } + return &StructMock{ Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)), Args: args, - MockStructs: reflect.ValueOf(filled).Interface(), + MockStructs: ms, Result: &sqlResult{ rowsAffected: int64(c.RowCount), err: nil,