poc for sql null types

This commit is contained in:
2026-01-15 22:53:05 -05:00
parent 3b40f8061f
commit 459eaca866
2 changed files with 70 additions and 0 deletions

30
lazy.go
View File

@@ -1,6 +1,7 @@
package lazy
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
@@ -11,6 +12,10 @@ 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
@@ -34,6 +39,7 @@ type Config struct {
// generates mock data based on configuration to be used for sqlmock
func RandomGenerate(m Config) (*Mock, error) {
fmt.Println("huh")
//example struct cannot be nil and must be a struct
if m.Example == nil {
return nil, errors.New("example value cannot be nil")
@@ -81,9 +87,33 @@ func RandomGenerate(m Config) (*Mock, error) {
continue
}
//attempt other types
//generate random values
nv := kindToRandom(field)
if nv == nil {
//this will catch the sql.Null* types, although not slices of them. In these statements it should be
//50/50 chance of null vs value and then psuedo random from there
//1. Write simple 50/50 function
//2. move this logic to nullKindToRandom function
//3. add bind type option to allow for more than just AT now that I'm using postgres
switch field.Type {
case reflect.TypeOf(sql.NullTime{}):
//
case reflect.TypeOf(sql.NullInt16{}):
//
case reflect.TypeOf(sql.NullInt32{}):
//
case reflect.TypeOf(sql.NullInt64{}):
//
case reflect.TypeOf(sql.NullBool{}):
//
case reflect.TypeOf(sql.NullFloat64{}):
//
case reflect.TypeOf(sql.NullString{}):
//
case reflect.TypeOf(sql.NullByte{}):
//
}
return nil, fmt.Errorf("could not match type: %s", retType.Name())
}