[new] types and logic

- addressing array type
- slice type now does random amount of entries
- better comments/readme
This commit is contained in:
2025-09-26 18:57:53 -04:00
parent 4febf3d877
commit 09749029b4
2 changed files with 186 additions and 44 deletions

127
lazy.go
View File

@@ -12,35 +12,45 @@ import (
)
const (
DB_TAG = "db"
LAZY_TAG = "lazy"
KEY_VALUE = "key"
DB_TAG = "db" //tag used to parse sql fields in example struct
LAZY_TAG = "lazy" //tag label for KEY_VALUE
KEY_VALUE = "key" //tag value for LAZY_TAG
)
type MockResults struct {
// mock data generated based on config
type Mock struct {
Query string
Columns []string
Rows [][]driver.Value
}
// configuration for RandomGenerate
type Config struct {
Query string
Example any
Keys []any
Keys []any //length of keys must = RowCount, if set
RowCount int
}
func GenerateRandomResults(m Config) (*MockResults, error) {
// generates mock data based on configuration to be used for sqlmock
func RandomGenerate(m Config) (*Mock, error) {
//example struct cannot be nil and must be a struct
if m.Example == nil {
return nil, errors.New("example value cannot be nil")
}
if reflect.ValueOf(m.Example).Kind() != reflect.Struct {
return nil, errors.New("example value must be a struct")
}
if m.RowCount == 0 {
//any weirdness, just pull one row
if m.RowCount <= 0 {
m.RowCount = 1
}
if len(m.Keys) != 0 {
//if keys are set, ensure there are enough to populate requested rows
primaryKey := false
if len(m.Keys) > 0 {
primaryKey = true
if len(m.Keys) != m.RowCount {
return nil, errors.New("you must provide a key for each row")
}
@@ -60,15 +70,18 @@ func GenerateRandomResults(m Config) (*MockResults, error) {
continue
}
//track columns only once
if y == 0 {
columns = append(columns, dbTag)
}
if field.Tag.Get(LAZY_TAG) == KEY_VALUE {
//if field has lazy:"key" tag and tags are present inject primary key
if field.Tag.Get(LAZY_TAG) == KEY_VALUE && primaryKey {
rows[y] = append(rows[y], m.Keys[y])
continue
}
//generate random values
nv := kindToRandom(field)
if nv == nil {
return nil, fmt.Errorf("could not match type: %s", retType.Name())
@@ -78,13 +91,15 @@ func GenerateRandomResults(m Config) (*MockResults, error) {
}
}
return &MockResults{
return &Mock{
//sql is rebound and escaped for sqlmock.ExpectQuery
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(m.Query)),
Columns: columns,
Rows: rows,
}, nil
}
// converts basic reflect Kind's to psuedo-random data, slices are given a random amount of entries
func kindToRandom(field reflect.StructField) any {
kind := field.Type.Kind()
switch kind {
@@ -102,23 +117,99 @@ func kindToRandom(field reflect.StructField) any {
return fmt.Sprintf("%d", rand.Int())
case reflect.Bool:
return rand.Int()%2 == 0
case reflect.Slice:
case reflect.Array:
underlying := field.Type.Elem().Kind()
count := reflect.ValueOf(field).Len() //fill entire length
switch underlying {
case reflect.Int:
return []int{rand.Int()}
var intslice []int
for x := 0; x < count; x++ {
intslice = append(intslice, rand.Int())
}
return intslice
case reflect.Int32:
return []int32{rand.Int32()}
var int32slice []int32
for x := 0; x < count; x++ {
int32slice = append(int32slice, rand.Int32())
}
return int32slice
case reflect.Int64:
return []int64{rand.Int64()}
var int64slice []int64
for x := 0; x < count; x++ {
int64slice = append(int64slice, rand.Int64())
}
return int64slice
case reflect.Float32:
return []float32{rand.Float32()}
var float32slice []float32
for x := 0; x < count; x++ {
float32slice = append(float32slice, rand.Float32())
}
return float32slice
case reflect.Float64:
return []float64{rand.Float64()}
var float64slice []float64
for x := 0; x < count; x++ {
float64slice = append(float64slice, rand.Float64())
}
return float64slice
case reflect.String:
return []string{fmt.Sprintf("%d", rand.Int())}
var strslice []string
for x := 0; x < count; x++ {
strslice = append(strslice, fmt.Sprintf("%d", rand.Int()))
}
return strslice
case reflect.Bool:
return []bool{rand.Int()%2 == 0}
var boolslice []bool
for x := 0; x < count; x++ {
boolslice = append(boolslice, (rand.Int()%2 == 0))
}
return boolslice
}
case reflect.Slice:
underlying := field.Type.Elem().Kind()
count := (rand.Int() % 10) + 1 //amount of entries to append to slice
switch underlying {
case reflect.Int:
var intslice []int
for x := 0; x < count; x++ {
intslice = append(intslice, rand.Int())
}
return intslice
case reflect.Int32:
var int32slice []int32
for x := 0; x < count; x++ {
int32slice = append(int32slice, rand.Int32())
}
return int32slice
case reflect.Int64:
var int64slice []int64
for x := 0; x < count; x++ {
int64slice = append(int64slice, rand.Int64())
}
return int64slice
case reflect.Float32:
var float32slice []float32
for x := 0; x < count; x++ {
float32slice = append(float32slice, rand.Float32())
}
return float32slice
case reflect.Float64:
var float64slice []float64
for x := 0; x < count; x++ {
float64slice = append(float64slice, rand.Float64())
}
return float64slice
case reflect.String:
var strslice []string
for x := 0; x < count; x++ {
strslice = append(strslice, fmt.Sprintf("%d", rand.Int()))
}
return strslice
case reflect.Bool:
var boolslice []bool
for x := 0; x < count; x++ {
boolslice = append(boolslice, (rand.Int()%2 == 0))
}
return boolslice
}
}