From 459eaca8660b9f2e28005e602dc8b91fd0a63010 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 15 Jan 2026 22:53:05 -0500 Subject: [PATCH] poc for sql null types --- cmd/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ lazy.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 cmd/main.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..e14516b --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + + "code.jakeyoungdev.com/go/lazy" +) + +func main() { + + // res, err := lazy.RandomStruct(lazy.StructConfig{ + // Query: "insert into test (one, two) values (?, ?), (?, ?)", + // Example: test{}, + // RowCount: 2, + // }) + + x, y := lazy.RandomGenerate(lazy.Config{ + Query: "select * from table", + Example: lazy.Test{}, + RowCount: 1, + }) + if y != nil { + panic(y) + } + fmt.Printf("%+v", x) + + // if err != nil { + // panic(err) + // } + + // fmt.Println(res) + + // t, ok := res.MockStructs[0].(test) + // if !ok { + // fmt.Println("errrrrrr") + // } + + // fmt.Println(t.One) + // fmt.Println(t.Two) +} diff --git a/lazy.go b/lazy.go index 4a7bcc4..7fe7707 100644 --- a/lazy.go +++ b/lazy.go @@ -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()) }