|
@@ -5,9 +5,8 @@ import (
|
|
|
"math"
|
|
|
"reflect"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
-
|
|
|
- "github.com/go-openapi/inflect"
|
|
|
)
|
|
|
|
|
|
type table struct {
|
|
@@ -24,8 +23,7 @@ func (t *table) Make() MappableInterface {
|
|
|
return reflect.New(t.getType()).Interface().(MappableInterface)
|
|
|
}
|
|
|
|
|
|
-func (t *table) CallMethod(i MappableInterface, n string, args ...interface{}) ([]interface{}, error) {
|
|
|
- var ret []interface{}
|
|
|
+func (t *table) CallMethod(i MappableInterface, n string, args ...interface{}) (ret []interface{}, err error) {
|
|
|
if t.getValue(true).MethodByName(n).IsValid() {
|
|
|
fn := reflect.ValueOf(i).MethodByName(n)
|
|
|
fnt := fn.Type()
|
|
@@ -77,22 +75,38 @@ func (t *table) CallMethod(i MappableInterface, n string, args ...interface{}) (
|
|
|
in = append(in, argv.Convert(inType))
|
|
|
}
|
|
|
|
|
|
- var err error = nil
|
|
|
- out := fn.Call(in)[0:fnt.NumOut()]
|
|
|
-
|
|
|
- for _, val := range out {
|
|
|
- switch val.Kind() {
|
|
|
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
- ret = append(ret, val.Uint())
|
|
|
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
- ret = append(ret, val.Int())
|
|
|
- case reflect.Float32, reflect.Float64:
|
|
|
- ret = append(ret, val.Float())
|
|
|
- case reflect.String:
|
|
|
- ret = append(ret, val.String())
|
|
|
- case reflect.Interface:
|
|
|
- if !val.IsNil() && val.CanInterface() && val.Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
|
|
|
- err = val.Interface().(error)
|
|
|
+ var out []reflect.Value
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ fmt.Println("Recovered: ", r)
|
|
|
+ err = fmt.Errorf("Recovered: %v", r)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ out = fn.Call(in)[0:fnt.NumOut()]
|
|
|
+
|
|
|
+ if strings.HasPrefix(n, "Get") {
|
|
|
+ for _, val := range out {
|
|
|
+ switch val.Kind() {
|
|
|
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
+ ret = append(ret, val.Uint())
|
|
|
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
+ ret = append(ret, val.Int())
|
|
|
+ case reflect.Float32, reflect.Float64:
|
|
|
+ ret = append(ret, val.Float())
|
|
|
+ case reflect.String:
|
|
|
+ ret = append(ret, val.String())
|
|
|
+ case reflect.Interface:
|
|
|
+ if !val.IsNil() && val.CanInterface() && val.Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
|
|
|
+ err = val.Interface().(error)
|
|
|
+ }
|
|
|
+ case reflect.Struct, reflect.Ptr:
|
|
|
+ if val.CanInterface() && !val.IsNil() {
|
|
|
+ ret = append(ret, val.Interface())
|
|
|
+ } else if val.IsNil() {
|
|
|
+ ret = append(ret, nil)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -201,7 +215,12 @@ func (t *table) addField(f field, cbCh chan<- mapperCallback) {
|
|
|
to: rtbl,
|
|
|
fn: func(tbl *table) {
|
|
|
key := tbl.getPrimaryKey()
|
|
|
- self.rels.addRelation(belongsTo, relation{tbl, f, *key, *c})
|
|
|
+ switch f.MakeType().Kind() {
|
|
|
+ case reflect.Map, reflect.Slice:
|
|
|
+ self.rels.addRelation(belongsTo, relation{tbl, f, *c, *key})
|
|
|
+ default:
|
|
|
+ self.rels.addRelation(belongsTo, relation{tbl, f, *key, *c})
|
|
|
+ }
|
|
|
},
|
|
|
}
|
|
|
} else {
|
|
@@ -223,24 +242,12 @@ func (t *table) addField(f field, cbCh chan<- mapperCallback) {
|
|
|
// Check for relation on column mane
|
|
|
if c := tbl.hasColumn(cn); c != nil {
|
|
|
|
|
|
- // Predict the relations is «hasOne»
|
|
|
has := hasOne
|
|
|
-
|
|
|
- // Try to predict (or simply guess) with pluralization, if «hasMany»
|
|
|
- if inflect.Pluralize(f.getFieldName()) == f.getFieldName() {
|
|
|
+ switch f.MakeType().Kind() {
|
|
|
+ case reflect.Map, reflect.Slice:
|
|
|
has = hasMany
|
|
|
}
|
|
|
|
|
|
- // Override with tagging if specified
|
|
|
- if tag, ok := f.getTag("has"); ok {
|
|
|
- switch tag {
|
|
|
- case "many":
|
|
|
- has = hasMany
|
|
|
- case "one":
|
|
|
- has = hasOne
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
self.rels.addRelation(has, relation{tbl, f, *c, *pkey})
|
|
|
}
|
|
|
},
|
|
@@ -258,7 +265,7 @@ func (t *table) addField(f field, cbCh chan<- mapperCallback) {
|
|
|
fallthrough // Support all Int types
|
|
|
case reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
|
|
|
fallthrough // Support all Float and Complex types
|
|
|
- case reflect.String, reflect.Bool:
|
|
|
+ case reflect.String, reflect.Bool, reflect.Struct:
|
|
|
// Support string and boolean
|
|
|
|
|
|
// Map column name
|
|
@@ -272,6 +279,11 @@ func (t *table) addField(f field, cbCh chan<- mapperCallback) {
|
|
|
t.cols = append(t.cols, column{
|
|
|
f, dbf,
|
|
|
})
|
|
|
+ case reflect.Ptr:
|
|
|
+ f.t = f.t.Elem()
|
|
|
+ f.v = f.v.Elem()
|
|
|
+ t.addField(f, cbCh)
|
|
|
+
|
|
|
default:
|
|
|
fmt.Println(t.getStructName(), "not supporting", f)
|
|
|
}
|