1 Star 1 Fork 0

1701 / gpa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
table-name.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
1701 提交于 2021-07-14 17:25 . 解析参数结构体
// Copyright 2020 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package names
import (
"reflect"
"sync"
)
// TableName table name interface to define customerize table name
type TableName interface {
TableName() string
}
var (
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
tvCache sync.Map
)
func getTableName(mapper Mapper, v reflect.Value) string {
if v.Type().Implements(tpTableName) {
return v.Interface().(TableName).TableName()
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
if v.Type().Implements(tpTableName) {
return v.Interface().(TableName).TableName()
}
} else if v.CanAddr() {
v1 := v.Addr()
if v1.Type().Implements(tpTableName) {
return v1.Interface().(TableName).TableName()
}
} else {
name, ok := tvCache.Load(v.Type())
if ok {
if name.(string) != "" {
return name.(string)
}
} else {
v2 := reflect.New(v.Type())
if v2.Type().Implements(tpTableName) {
tableName := v2.Interface().(TableName).TableName()
tvCache.Store(v.Type(), tableName)
return tableName
}
tvCache.Store(v.Type(), "")
}
}
return mapper.Obj2Table(v.Type().Name())
}
Go
1
https://gitee.com/knowgo/gpa.git
git@gitee.com:knowgo/gpa.git
knowgo
gpa
gpa
v0.2.14

搜索帮助