-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathschema.go
252 lines (215 loc) · 5.61 KB
/
schema.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package portal
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/fatih/structs"
)
type schema struct {
fieldAliasMapTagName string
rawValue interface{}
schemaStruct *structs.Struct
availableFieldNames map[string]bool
fields []*schemaField
parent *schema
cacheDisabled bool
cacheGroup *cacheGroup
}
func newSchema(v interface{}, parent ...*schema) *schema {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
panic("expect a pointer to struct")
}
var schemaValue reflect.Value
switch rv.Elem().Kind() {
case reflect.Struct:
// var schema SchemaStruct
// ptr := &schema
schemaValue = rv.Elem()
case reflect.Ptr:
// var schema *SchemaStruct
// ptr := &schema
if rv.Elem().IsNil() {
typ, err := innerStructType(rv.Type())
if err != nil {
panic(fmt.Errorf("cannot get schema struct: %s", err))
}
schemaValue = reflect.New(typ).Elem()
rv.Elem().Set(schemaValue.Addr())
} else {
schemaValue = rv.Elem().Elem()
}
default:
panic("expect a pointer to struct")
}
rawValue := schemaValue.Addr().Interface()
var cacheDisabled = func(in interface{}) bool {
if ret, ok := in.(cachable); ok {
return ret.PortalDisableCache()
}
return false
}(rawValue)
sch := &schema{
schemaStruct: structs.New(schemaValue.Addr().Interface()),
availableFieldNames: make(map[string]bool),
rawValue: rawValue,
fieldAliasMapTagName: "json",
cacheDisabled: cacheDisabled,
cacheGroup: newCacheGroup(newMapCache()),
}
if len(parent) > 0 {
sch.parent = parent[0]
}
for _, name := range getAvailableFieldNames(sch.schemaStruct.Fields()) {
sch.availableFieldNames[name] = true
sch.fields = append(sch.fields, newField(sch, sch.schemaStruct.Field(name)))
}
return sch
}
func hasAsyncFields(schemaType reflect.Type, onlyFields, excludeFields []string) bool {
// TODO: try to cache the result
schema := newSchema(reflect.New(schemaType).Interface())
schema.setOnlyFields(onlyFields...)
schema.setExcludeFields(excludeFields...)
return len(schema.asyncFields(false)) > 0
}
func (s *schema) withFieldAliasMapTagName(t string) *schema {
s.fieldAliasMapTagName = t
return s
}
func getAvailableFieldNames(fields []*structs.Field) (names []string) {
for _, f := range fields {
if f.IsEmbedded() {
names = append(names, getAvailableFieldNames(f.Fields())...)
} else {
names = append(names, f.Name())
}
}
return names
}
func (s *schema) availableFields() []*schemaField {
fields := make([]*schemaField, 0)
for _, f := range s.fields {
v, ok := s.availableFieldNames[f.Name()]
if ok && v {
fields = append(fields, f)
}
}
return fields
}
func (s *schema) syncFields(disableConcurrency bool) (fields []*schemaField) {
for _, f := range s.availableFields() {
if disableConcurrency {
fields = append(fields, f)
} else if !f.async() {
fields = append(fields, f)
}
}
return
}
func (s *schema) asyncFields(disableConcurrency bool) (fields []*schemaField) {
if disableConcurrency {
return
}
for _, f := range s.availableFields() {
if f.async() {
fields = append(fields, f)
}
}
return
}
func (s *schema) innerStruct() *structs.Struct {
return s.schemaStruct
}
func (s *schema) fieldValueFromSrc(ctx context.Context, field *schemaField, v interface{}, noCache bool) (val interface{}, err error) {
if isNil(v) || !structs.IsStruct(v) {
return nil, fmt.Errorf("failed to get value for field %s, empty input data %v", field, v)
}
if field.hasConstValue() {
val = field.constValue()
} else if field.hasMethod() {
m, attrs := field.method()
if m == "" {
return nil, fmt.Errorf("empty method name")
}
var ret interface{}
var err error
disableCache := noCache || field.isCacheDisabled()
if disableCache {
ret, err = invokeMethodOfAnyType(ctx, s.rawValue, m, v)
} else {
cacheKey := genCacheKey(ctx, s.rawValue, v, m)
ret, err = invokeMethodOfAnyTypeWithCache(ctx, s.rawValue, m, s.cacheGroup, cacheKey, v)
}
if err != nil {
return nil, fmt.Errorf("failed to get value: %s", err)
}
if len(attrs) > 0 {
return nestedValue(ctx, ret, attrs, nil, !disableCache)
}
return ret, nil
} else if field.hasChainingAttrs() {
disableCache := noCache || field.isCacheDisabled()
return nestedValue(ctx, v, field.chainingAttrs(), s.cacheGroup, !disableCache)
} else {
return nestedValue(ctx, v, []string{field.Name()}, nil, false)
}
return
}
func (s *schema) setOnlyFields(fieldNames ...string) {
if len(fieldNames) == 0 {
return
}
for k := range s.availableFieldNames {
s.availableFieldNames[k] = false
}
for _, f := range fieldNames {
field := s.fieldByNameOrAlias(f)
if field == nil {
logger.Warnf("field name '%s.%s' not found", s.name(), f)
} else {
s.availableFieldNames[field.Name()] = true
}
}
}
func (s *schema) setExcludeFields(fieldNames ...string) {
if len(fieldNames) == 0 {
return
}
for _, f := range fieldNames {
field := s.fieldByNameOrAlias(f)
if field == nil {
logger.Warnf("field name '%s.%s' not found", s.name(), f)
} else {
s.availableFieldNames[field.Name()] = false
}
}
}
func (s *schema) name() string {
return structName(s.rawValue)
}
func (s *schema) nameWithParents() string {
var names []string
p := s
for p != nil {
names = append(names, p.name())
p = p.parent
}
// reverse names (two cursors)
i, j := 0, len(names)-1
for i < j {
names[i], names[j] = names[j], names[i]
i++
j--
}
return strings.Join(names, ".")
}
func (s *schema) fieldByNameOrAlias(name string) *schemaField {
for _, f := range s.fields {
if f.alias == name || f.Name() == name {
return f
}
}
return nil
}