adding ok check to prevent panics

This commit is contained in:
2025-09-30 11:47:38 -04:00
parent 6a9cdafcf1
commit 6326711424

10
lazy.go
View File

@@ -20,6 +20,7 @@ const (
var ( var (
ErrBadExample = errors.New("example field must be a non-nil struct") 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") 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 //contains placeholder to inject type on runtime
ErrUnsupportedType = errors.New("type: %s is not yet supported") ErrUnsupportedType = errors.New("type: %s is not yet supported")
) )
@@ -42,7 +43,7 @@ type Config struct {
type StructMock struct { type StructMock struct {
Query string Query string
Args []driver.Value Args []driver.Value
MockStructs any MockStructs []any
Result driver.Result 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{ return &StructMock{
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)), Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)),
Args: args, Args: args,
MockStructs: reflect.ValueOf(filled).Interface(), MockStructs: ms,
Result: &sqlResult{ Result: &sqlResult{
rowsAffected: int64(c.RowCount), rowsAffected: int64(c.RowCount),
err: nil, err: nil,