Ai
1 Star 1 Fork 0

bigbase/pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
struct.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
Vladimir Mihailenco 提交于 2019-02-09 19:49 +08:00 . Struct filter table name
package struct_filter
import (
"reflect"
"github.com/go-pg/pg/internal"
"github.com/go-pg/pg/internal/tag"
)
type Struct struct {
TableName string
Fields []*Field
}
func NewStruct(typ reflect.Type) *Struct {
s := &Struct{
Fields: make([]*Field, 0, typ.NumField()),
}
addFields(s, typ, nil)
return s
}
func (s *Struct) Field(name string) *Field {
col, opCode, _ := splitColumnOperator(name, "__")
for _, f := range s.Fields {
if f.Column == col && f.opCode == opCode {
return f
}
}
return nil
}
func addFields(s *Struct, typ reflect.Type, baseIndex []int) {
if baseIndex != nil {
baseIndex = baseIndex[:len(baseIndex):len(baseIndex)]
}
for i := 0; i < typ.NumField(); i++ {
sf := typ.Field(i)
if sf.Anonymous {
pgTag := sf.Tag.Get("pg")
if pgTag == "-" {
continue
}
sfType := sf.Type
if sfType.Kind() == reflect.Ptr {
sfType = sfType.Elem()
}
if sfType.Kind() != reflect.Struct {
continue
}
addFields(s, sfType, sf.Index)
continue
}
if sf.Name == "tableName" {
sqlTag := tag.Parse(sf.Tag.Get("sql"))
name, _ := tag.Unquote(sqlTag.Name)
s.TableName = internal.QuoteTableName(name)
continue
}
f := newField(sf)
if f == nil {
continue
}
if len(baseIndex) > 0 {
f.index = append(baseIndex, f.index...)
}
s.Fields = append(s.Fields, f)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v8.0.7

搜索帮助