added checks for sql null types
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"code.jakeyoungdev.com/go/lazy"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
type Test struct {
|
||||
Timer sql.NullTime `db:"timer"`
|
||||
TestTime time.Time `db:"times"`
|
||||
}
|
||||
// res, err := lazy.RandomStruct(lazy.StructConfig{
|
||||
// Query: "insert into test (one, two) values (?, ?), (?, ?)",
|
||||
// Example: test{},
|
||||
@@ -16,7 +22,7 @@ func main() {
|
||||
|
||||
x, y := lazy.RandomGenerate(lazy.Config{
|
||||
Query: "select * from table",
|
||||
Example: lazy.Test{},
|
||||
Example: Test{},
|
||||
RowCount: 1,
|
||||
})
|
||||
if y != nil {
|
||||
|
||||
132
lazy.go
132
lazy.go
@@ -13,10 +13,6 @@ import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Test struct {
|
||||
Timer sql.NullTime `db:"timer"`
|
||||
}
|
||||
|
||||
const (
|
||||
DB_TAG = "db" //tag used to parse sql fields in example struct
|
||||
LAZY_TAG = "lazy" //tag label for KEY_VALUE
|
||||
@@ -95,67 +91,9 @@ func RandomGenerate(m Config) (*Mock, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
//attempt other types
|
||||
//generate random values
|
||||
nv := kindToRandom(field)
|
||||
if nv == nil {
|
||||
//move this logic to nullKindToRandom function
|
||||
nullType := false
|
||||
switch field.Type {
|
||||
case reflect.TypeOf(sql.NullTime{}):
|
||||
var temp sql.NullTime
|
||||
if !isNull() {
|
||||
temp.Valid = true
|
||||
temp.Time = time.Now()
|
||||
}
|
||||
rows[y] = append(rows[y], temp)
|
||||
nullType = true
|
||||
case reflect.TypeOf(sql.NullInt16{}):
|
||||
if isNull() {
|
||||
rows[y] = append(rows[y], nil)
|
||||
} else {
|
||||
rows[y] = append(rows[y], rand.Int())
|
||||
}
|
||||
nullType = true
|
||||
case reflect.TypeOf(sql.NullInt32{}):
|
||||
if isNull() {
|
||||
rows[y] = append(rows[y], nil)
|
||||
} else {
|
||||
rows[y] = append(rows[y], rand.Int32())
|
||||
}
|
||||
nullType = true
|
||||
case reflect.TypeOf(sql.NullInt64{}):
|
||||
if isNull() {
|
||||
rows[y] = append(rows[y], nil)
|
||||
} else {
|
||||
rows[y] = append(rows[y], rand.Int64())
|
||||
}
|
||||
nullType = true
|
||||
case reflect.TypeOf(sql.NullBool{}):
|
||||
if isNull() {
|
||||
rows[y] = append(rows[y], nil)
|
||||
} else {
|
||||
rows[y] = append(rows[y], rand.Int()%2 == 0)
|
||||
}
|
||||
nullType = true
|
||||
case reflect.TypeOf(sql.NullFloat64{}):
|
||||
if isNull() {
|
||||
rows[y] = append(rows[y], nil)
|
||||
} else {
|
||||
rows[y] = append(rows[y], rand.Float64())
|
||||
}
|
||||
nullType = true
|
||||
case reflect.TypeOf(sql.NullString{}):
|
||||
if isNull() {
|
||||
rows[y] = append(rows[y], nil)
|
||||
} else {
|
||||
rows[y] = append(rows[y], fmt.Sprintf("random %d", rand.Int()))
|
||||
}
|
||||
nullType = true
|
||||
}
|
||||
if !nullType {
|
||||
return nil, fmt.Errorf("could not match type: %s", retType.Name())
|
||||
}
|
||||
return nil, fmt.Errorf("could not match type: %s", retType.Name())
|
||||
}
|
||||
|
||||
rows[y] = append(rows[y], nv)
|
||||
@@ -172,6 +110,72 @@ func RandomGenerate(m Config) (*Mock, error) {
|
||||
|
||||
// converts basic reflect Kind's to psuedo-random data, slices are given a random amount of entries
|
||||
func kindToRandom(field reflect.StructField) any {
|
||||
//this isn't ideal, but since the sql types are structs they were being filled in a weird
|
||||
//manner. For now we check those types first, clean this up later.
|
||||
switch field.Type {
|
||||
case reflect.TypeOf(sql.NullTime{}):
|
||||
fmt.Println("found nulltime")
|
||||
var temp sql.NullTime
|
||||
if !isNull() {
|
||||
temp.Valid = true
|
||||
temp.Time = time.Now()
|
||||
}
|
||||
return temp
|
||||
case reflect.TypeOf(sql.NullInt16{}):
|
||||
var temp sql.NullInt16
|
||||
if isNull() {
|
||||
temp.Valid = false
|
||||
} else {
|
||||
temp.Valid = true
|
||||
temp.Int16 = int16(rand.Int())
|
||||
}
|
||||
return temp
|
||||
case reflect.TypeOf(sql.NullInt32{}):
|
||||
var temp sql.NullInt32
|
||||
if isNull() {
|
||||
temp.Valid = false
|
||||
} else {
|
||||
temp.Valid = true
|
||||
temp.Int32 = rand.Int32()
|
||||
}
|
||||
return temp
|
||||
case reflect.TypeOf(sql.NullInt64{}):
|
||||
var temp sql.NullInt64
|
||||
if isNull() {
|
||||
temp.Valid = false
|
||||
} else {
|
||||
temp.Valid = true
|
||||
temp.Int64 = rand.Int64()
|
||||
}
|
||||
return temp
|
||||
case reflect.TypeOf(sql.NullBool{}):
|
||||
var temp sql.NullBool
|
||||
if isNull() {
|
||||
temp.Valid = false
|
||||
} else {
|
||||
temp.Valid = true
|
||||
temp.Bool = (rand.Int()%2 == 0)
|
||||
}
|
||||
return temp
|
||||
case reflect.TypeOf(sql.NullFloat64{}):
|
||||
var temp sql.NullFloat64
|
||||
if isNull() {
|
||||
temp.Valid = false
|
||||
} else {
|
||||
temp.Valid = true
|
||||
temp.Float64 = rand.Float64()
|
||||
}
|
||||
return temp
|
||||
case reflect.TypeOf(sql.NullString{}):
|
||||
var temp sql.NullString
|
||||
if isNull() {
|
||||
temp.Valid = false
|
||||
} else {
|
||||
temp.Valid = true
|
||||
temp.String = fmt.Sprintf("random %d", rand.Int())
|
||||
}
|
||||
return temp
|
||||
}
|
||||
kind := field.Type.Kind()
|
||||
switch kind {
|
||||
case reflect.Int:
|
||||
@@ -187,8 +191,10 @@ func kindToRandom(field reflect.StructField) any {
|
||||
case reflect.String:
|
||||
return fmt.Sprintf("random %d", rand.Int())
|
||||
case reflect.Bool:
|
||||
fmt.Println("THIS TWO")
|
||||
return rand.Int()%2 == 0
|
||||
case reflect.TypeOf(time.Time{}).Kind():
|
||||
fmt.Println("THIS?")
|
||||
return time.Now()
|
||||
case reflect.Array:
|
||||
underlying := field.Type.Elem().Kind()
|
||||
|
||||
Reference in New Issue
Block a user