/
struct.go
74 lines (63 loc) · 1.33 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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)
}
}