adding ok check to prevent panics
This commit is contained in:
10
lazy.go
10
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,
|
||||
|
Reference in New Issue
Block a user