Added bind checkType function
* Added select and join querymod
This commit is contained in:
parent
4b12c849fc
commit
95d91f17f3
4 changed files with 108 additions and 10 deletions
45
boil/bind.go
45
boil/bind.go
|
@ -1,5 +1,48 @@
|
|||
package boil
|
||||
|
||||
func (q *Query) Bind() {
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func (q *Query) Bind(obj interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkType(obj interface{}) (reflect.Type, bool, error) {
|
||||
val := reflect.ValueOf(obj)
|
||||
typ := val.Type()
|
||||
kind := val.Kind()
|
||||
|
||||
if kind != reflect.Ptr {
|
||||
return nil, false, fmt.Errorf("Bind must be given pointers to structs but got type: %s, kind: %s", typ.String(), kind)
|
||||
}
|
||||
|
||||
typ = typ.Elem()
|
||||
kind = typ.Kind()
|
||||
isSlice := false
|
||||
|
||||
switch kind {
|
||||
case reflect.Slice:
|
||||
typ = typ.Elem()
|
||||
kind = typ.Kind()
|
||||
isSlice = true
|
||||
case reflect.Struct:
|
||||
return typ, isSlice, nil
|
||||
default:
|
||||
return nil, false, fmt.Errorf("Bind was given an invalid object must be []*T or *T but got type: %s, kind: %s", typ.String(), kind)
|
||||
}
|
||||
|
||||
if kind != reflect.Ptr {
|
||||
return nil, false, fmt.Errorf("Bind must be given pointers to structs but got type: %s, kind: %s", typ.String(), kind)
|
||||
}
|
||||
|
||||
typ = typ.Elem()
|
||||
kind = typ.Kind()
|
||||
|
||||
if kind != reflect.Struct {
|
||||
return nil, false, fmt.Errorf("Bind must be a struct but got type: %s, kind: %s", typ.String(), kind)
|
||||
}
|
||||
|
||||
return typ, isSlice, nil
|
||||
}
|
||||
|
|
|
@ -1 +1,42 @@
|
|||
package boil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCheckType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type Thing struct {
|
||||
}
|
||||
|
||||
validTest := []struct {
|
||||
Input interface{}
|
||||
IsSlice bool
|
||||
TypeName string
|
||||
}{
|
||||
{&[]*Thing{}, true, "boil.Thing"},
|
||||
{[]Thing{}, false, ""},
|
||||
{&[]Thing{}, false, ""},
|
||||
{Thing{}, false, ""},
|
||||
{new(int), false, ""},
|
||||
{5, false, ""},
|
||||
{&Thing{}, false, "boil.Thing"},
|
||||
}
|
||||
|
||||
for i, test := range validTest {
|
||||
typ, isSlice, err := checkType(test.Input)
|
||||
if err != nil {
|
||||
if len(test.TypeName) > 0 {
|
||||
t.Errorf("%d) Type: %T %#v - should have succeded but got err: %v", i, test.Input, test.Input, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if isSlice != test.IsSlice {
|
||||
t.Errorf("%d) Type: %T %#v - succeded but wrong isSlice value: %t, want %t", i, test.Input, test.Input, isSlice, test.IsSlice)
|
||||
}
|
||||
|
||||
if got := typ.String(); got != test.TypeName {
|
||||
t.Errorf("%d) Type: %T %#v - succeded but wrong type name: %s, want: %s", i, test.Input, test.Input, got, test.TypeName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,13 +6,15 @@ type where struct {
|
|||
}
|
||||
|
||||
type Query struct {
|
||||
limit int
|
||||
where []where
|
||||
executor Executor
|
||||
groupBy []string
|
||||
orderBy []string
|
||||
having []string
|
||||
from string
|
||||
executor Executor
|
||||
selectCols []string
|
||||
from string
|
||||
joins []string
|
||||
where []where
|
||||
groupBy []string
|
||||
orderBy []string
|
||||
having []string
|
||||
limit int
|
||||
}
|
||||
|
||||
func (q *Query) buildQuery() string {
|
||||
|
|
|
@ -20,6 +20,18 @@ func Limit(limit int) QueryMod {
|
|||
}
|
||||
}
|
||||
|
||||
func Join(join string) QueryMod {
|
||||
return func(q *Query) {
|
||||
q.joins = append(q.joins, join)
|
||||
}
|
||||
}
|
||||
|
||||
func Select(columns ...string) QueryMod {
|
||||
return func(q *Query) {
|
||||
q.selectCols = append(q.selectCols, columns...)
|
||||
}
|
||||
}
|
||||
|
||||
func Where(clause string, args ...interface{}) QueryMod {
|
||||
return func(q *Query) {
|
||||
w := where{
|
||||
|
@ -49,8 +61,8 @@ func Having(clause string) QueryMod {
|
|||
}
|
||||
}
|
||||
|
||||
func From(clause string) QueryMod {
|
||||
func From(table string) QueryMod {
|
||||
return func(q *Query) {
|
||||
q.from = clause
|
||||
q.from = table
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue