/
utils_field.go
180 lines (138 loc) · 3.65 KB
/
utils_field.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package builder
import (
"context"
"database/sql/driver"
"fmt"
"go/ast"
"reflect"
"strings"
"sync"
reflectx "github.com/go-courier/x/reflect"
typesx "github.com/go-courier/x/types"
)
func StructFieldsFor(ctx context.Context, typ typesx.Type) []*StructField {
return defaultStructFieldsFactory.TableFieldsFor(ctx, typ)
}
var defaultStructFieldsFactory = &StructFieldsFactory{}
type StructFieldsFactory struct {
cache sync.Map
}
func (tf *StructFieldsFactory) TableFieldsFor(ctx context.Context, typ typesx.Type) []*StructField {
typ = typesx.Deref(typ)
underlying := typ.Unwrap()
if v, ok := tf.cache.Load(underlying); ok {
return v.([]*StructField)
}
tfs := make([]*StructField, 0)
EachStructField(ctx, typ, func(f *StructField) bool {
tagDB := f.Tags["db"]
if tagDB != "" && tagDB != "-" {
tfs = append(tfs, f)
}
return true
})
tf.cache.Store(underlying, tfs)
return tfs
}
var typeModel = reflect.TypeOf((*Model)(nil)).Elem()
var driverValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
func EachStructField(ctx context.Context, tpe typesx.Type, each func(p *StructField) bool) {
if tpe.Kind() != reflect.Struct {
panic(fmt.Errorf("model %s must be a struct", tpe.Name()))
}
var walk func(tpe typesx.Type, modelLoc []int, parents ...int)
walk = func(tpe typesx.Type, modelLoc []int, parents ...int) {
if ok := tpe.Implements(typesx.FromRType(typeModel)); ok {
modelLoc = parents
}
for i := 0; i < tpe.NumField(); i++ {
f := tpe.Field(i)
if !ast.IsExported(f.Name()) {
continue
}
loc := append(parents, i)
tags := reflectx.ParseStructTags(string(f.Tag()))
displayName := f.Name()
tagDB, hasDB := tags["db"]
if hasDB {
if name := tagDB.Name(); name == "-" {
// skip name:"-"
continue
} else {
if name != "" {
displayName = name
}
}
}
if (f.Anonymous() || f.Type().Name() == f.Name()) && (!hasDB) {
fieldType := f.Type()
if !fieldType.Implements(typesx.FromRType(driverValuer)) {
for fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
if fieldType.Kind() == reflect.Struct {
walk(fieldType, modelLoc, loc...)
continue
}
}
}
p := &StructField{}
p.FieldName = f.Name()
p.Type = f.Type()
p.Field = f
p.Tags = tags
p.Name = strings.ToLower(displayName)
p.Loc = make([]int, len(loc))
copy(p.Loc, loc)
p.ModelLoc = make([]int, len(modelLoc))
copy(p.ModelLoc, modelLoc)
p.ColumnType = *ColumnTypeFromTypeAndTag(p.Type, string(tagDB))
if !each(p) {
break
}
}
}
walk(tpe, []int{})
}
type StructField struct {
Name string
FieldName string
Type typesx.Type
Field typesx.StructField
Tags map[string]reflectx.StructTag
Loc []int
ModelLoc []int
ColumnType ColumnType
}
func fieldValue(structReflectValue reflect.Value, locs []int) reflect.Value {
n := len(locs)
if n == 0 {
return structReflectValue
}
if n < 0 {
return reflect.Value{}
}
structReflectValue = reflectx.Indirect(structReflectValue)
fv := structReflectValue
for i := 0; i < n; i++ {
loc := locs[i]
fv = fv.Field(loc)
// last loc should keep ptr value
if i < n-1 {
for fv.Kind() == reflect.Ptr {
// notice the ptr struct ensure only for Ptr Anonymous StructField
if fv.IsNil() {
fv.Set(reflectx.New(fv.Type()))
}
fv = fv.Elem()
}
}
}
return fv
}
func (p *StructField) FieldValue(structReflectValue reflect.Value) reflect.Value {
return fieldValue(structReflectValue, p.Loc)
}
func (p *StructField) FieldModelValue(structReflectValue reflect.Value) reflect.Value {
return fieldValue(structReflectValue, p.ModelLoc)
}