improve function Struct for package gconv

This commit is contained in:
John
2020-06-06 15:31:04 +08:00
parent f2f98e1d16
commit 30501a882d
5 changed files with 41 additions and 36 deletions

View File

@ -1,8 +1,9 @@
# MySQL数据库配置
[database]
# debug = true
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
debug = true
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
MaxOpen = 100
#[database]
# [[database.default]]

View File

@ -39,10 +39,10 @@ type ConfigNode struct {
DryRun bool // (Optional) Dry run, which does SELECT but no INSERT/UPDATE/DELETE statements.
Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node.
Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database.
LinkInfo string // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored.
MaxIdleConnCount int // (Optional) Max idle connection configuration for underlying connection pool.
MaxOpenConnCount int // (Optional) Max open connection configuration for underlying connection pool.
MaxConnLifetime time.Duration // (Optional) Max connection TTL configuration for underlying connection pool.
LinkInfo string `json:"link"` // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored.
MaxIdleConnCount int `json:"maxidle"` // (Optional) Max idle connection configuration for underlying connection pool.
MaxOpenConnCount int `json:"maxopen"` // (Optional) Max open connection configuration for underlying connection pool.
MaxConnLifetime time.Duration `json:"maxlifetime"` // (Optional) Max connection TTL configuration for underlying connection pool.
}
// configs is internal used configuration object.

View File

@ -8,6 +8,7 @@ package gins
import (
"fmt"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gutil"
@ -58,6 +59,7 @@ func Database(name ...string) gdb.DB {
}
}
if len(cg) > 0 {
intlog.Printf("%s, %#v", group, cg)
gdb.SetConfigGroup(group, cg)
}
}
@ -69,6 +71,7 @@ func Database(name ...string) gdb.DB {
cg = append(cg, *node)
}
if len(cg) > 0 {
intlog.Printf("%s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg)
}
}

View File

@ -68,7 +68,7 @@ func New(options ...Options) *Manager {
gregex.Quote(opts.Delimiters[1]),
),
}
intlog.Printf(`New: %+v`, m)
intlog.Printf(`New: %#v`, m)
return m
}

View File

@ -50,11 +50,13 @@ func Struct(params interface{}, pointer interface{}, mapping ...map[string]strin
err = gerror.NewfSkip(1, "%v", e)
}
}()
// paramsMap is the map[string]interface{} type variable for params.
paramsMap := Map(params)
if paramsMap == nil {
return fmt.Errorf("invalid params: %v", params)
}
// Using reflect to do the converting,
// it also supports type of reflect.Value for <pointer>(always in internal usage).
elem, ok := pointer.(reflect.Value)
@ -86,7 +88,8 @@ func Struct(params interface{}, pointer interface{}, mapping ...map[string]strin
}
}
// It only performs one converting to the same attribute.
// doneMap is used to check repeated converting, its key is the attribute name of the struct.
// doneMap is used to check repeated converting, its key is the real attribute name
// of the struct.
doneMap := make(map[string]struct{})
// It first checks the passed mapping rules.
if len(mapping) > 0 && len(mapping[0]) > 0 {
@ -100,26 +103,13 @@ func Struct(params interface{}, pointer interface{}, mapping ...map[string]strin
}
}
}
// It secondly checks the tags of attributes.
tagMap := structs.TagMapName(pointer, structTagPriority, true)
for tagK, tagV := range tagMap {
// tagV is the the attribute name of the struct.
if _, ok := doneMap[tagV]; ok {
continue
}
if v, ok := paramsMap[tagK]; ok {
doneMap[tagV] = struct{}{}
if err := bindVarToStructAttr(elem, tagV, v); err != nil {
return err
}
}
}
// It finally do the converting with default rules.
// The key of the map is the attribute name of the struct,
// and the value is its replaced name for later comparison to improve performance.
attrMap := make(map[string]string)
elemType := elem.Type()
tempName := ""
var (
attrMap = make(map[string]string)
elemType = elem.Type()
tempName = ""
)
for i := 0; i < elem.NumField(); i++ {
// Only do converting to public attributes.
if !utils.IsLetterUpper(elemType.Field(i).Name[0]) {
@ -131,12 +121,27 @@ func Struct(params interface{}, pointer interface{}, mapping ...map[string]strin
if len(attrMap) == 0 {
return nil
}
var attrName, checkName string
var (
attrName string
checkName string
tagMap = structs.TagMapName(pointer, structTagPriority, true)
)
for mapK, mapV := range paramsMap {
attrName = ""
checkName = replaceCharReg.ReplaceAllString(mapK, "")
// Loop to find the matched attribute name with or without
// string cases and chars like '-'/'_'/'.'/' '.
// Matching the parameters to struct tag names.
// The <tagV> is the attribute name of the struct.
for tagK, tagV := range tagMap {
if strings.EqualFold(checkName, tagK) {
attrName = tagV
break
}
}
// Matching the parameters to struct attributes.
for attrK, attrV := range attrMap {
// Eg:
// UserName eq user_name
@ -148,19 +153,15 @@ func Struct(params interface{}, pointer interface{}, mapping ...map[string]strin
break
}
}
// If the attribute name is already checked converting, then skip it.
if attrName != "" {
if _, ok := doneMap[attrName]; ok {
continue
}
if _, ok := tagMap[attrName]; ok {
continue
}
}
// No matching, give up this attribute converting.
if attrName == "" {
continue
}
// If the attribute name is already checked converting, then skip it.
if _, ok := doneMap[attrName]; ok {
continue
}
// Mark it done.
doneMap[attrName] = struct{}{}
if err := bindVarToStructAttr(elem, attrName, mapV); err != nil {