adding inherit struct support for gdb

This commit is contained in:
John
2019-05-16 13:56:49 +08:00
parent 75725db6fb
commit 635d228c86
2 changed files with 40 additions and 4 deletions

View File

@ -20,6 +20,11 @@ import (
"time"
)
// Type assert api for String().
type apiString interface {
String() string
}
// 格式化SQL查询条件
func formatCondition(where interface{}, args []interface{}) (newWhere string, newArgs []interface{}) {
// 条件字符串处理
@ -187,3 +192,32 @@ func getInsertOperationByOption(option int) string {
}
return operator
}
// 将对象转换为map如果对象带有继承对象那么执行递归转换。
// 该方法用于将变量传递给数据库执行之前。
func structToMap(obj interface{}) map[string]interface{} {
data := gconv.Map(obj)
for k, v := range data {
rv := reflect.ValueOf(v)
kind := rv.Kind()
if kind == reflect.Ptr {
rv = rv.Elem()
kind = rv.Kind()
}
switch kind {
case reflect.Struct:
// 底层数据库引擎支持 time.Time 类型
if _, ok := v.(time.Time); ok {
continue
}
// 如果执行String方法那么执行字符串转换
if s, ok := v.(apiString); ok {
data[k] = s.String()
}
for k, v := range structToMap(v) {
data[k] = v
}
}
}
return data
}

View File

@ -13,6 +13,9 @@ import (
"strings"
)
const (
gGCONV_TAG = "gconv"
)
// Map converts any variable <i> to map[string]interface{}.
// If the parameter <i> is not a map type, then the conversion will fail and returns nil.
// If <i> is a struct object, the second parameter <tags> specifies the most priority
@ -102,8 +105,7 @@ func Map(value interface{}, tags...string) map[string]interface{} {
case reflect.Struct:
rt := rv.Type()
name := ""
gconvTag := "gconv"
tagArray := []string{gconvTag, "json"}
tagArray := []string{gGCONV_TAG, "json"}
switch len(tags) {
case 0:
// No need handle.
@ -112,8 +114,8 @@ func Map(value interface{}, tags...string) map[string]interface{} {
default:
tagArray = tags
}
if gstr.SearchArray(tagArray, gconvTag) < 0 {
tagArray = append(tagArray, gconvTag)
if gstr.SearchArray(tagArray, gGCONV_TAG) < 0 {
tagArray = append(tagArray, gGCONV_TAG)
}
for i := 0; i < rv.NumField(); i++ {
// Only convert the public attributes.