new/null-types #2

Merged
jake merged 10 commits from new/null-types into main 2026-01-23 19:59:06 +00:00
2 changed files with 70 additions and 0 deletions
Showing only changes of commit 459eaca866 - Show all commits

40
cmd/main.go Normal file
View File

@@ -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)
}

30
lazy.go
View File

@@ -1,6 +1,7 @@
package lazy package lazy
import ( import (
"database/sql"
"database/sql/driver" "database/sql/driver"
"errors" "errors"
"fmt" "fmt"
@@ -11,6 +12,10 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
type Test struct {
Timer sql.NullTime `db:"timer"`
}
const ( const (
DB_TAG = "db" //tag used to parse sql fields in example struct DB_TAG = "db" //tag used to parse sql fields in example struct
LAZY_TAG = "lazy" //tag label for KEY_VALUE 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 // generates mock data based on configuration to be used for sqlmock
func RandomGenerate(m Config) (*Mock, error) { func RandomGenerate(m Config) (*Mock, error) {
fmt.Println("huh")
//example struct cannot be nil and must be a struct //example struct cannot be nil and must be a struct
if m.Example == nil { if m.Example == nil {
return nil, errors.New("example value cannot be nil") return nil, errors.New("example value cannot be nil")
@@ -81,9 +87,33 @@ func RandomGenerate(m Config) (*Mock, error) {
continue continue
} }
//attempt other types
//generate random values //generate random values
nv := kindToRandom(field) nv := kindToRandom(field)
if nv == nil { 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()) return nil, fmt.Errorf("could not match type: %s", retType.Name())
} }