-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
287 lines (265 loc) · 7.36 KB
/
util.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package simplets
import (
"crypto/md5"
"encoding/base64"
"fmt"
"reflect"
"strings"
. "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)
func addHashPrefix(str string) string {
sum := md5.Sum([]byte(str))
prefix := base64.StdEncoding.EncodeToString(sum[0:3])
return fmt.Sprintf("%s...%s", prefix, str)
}
func trimHashPrefix(str string) string {
return str[4+len("..."):]
}
func IsObjectNotExist(err error) bool {
return strings.Contains(err.Error(), "OTSObjectNotExist")
}
func IsConditionCheckFail(err error) bool {
return strings.Contains(err.Error(), "OTSConditionCheckFail")
}
func substantiateError(err error) error {
if IsObjectNotExist(err) {
return fmt.Errorf("err is %w: %v", ErrObjectNotExist, err)
}
if IsConditionCheckFail(err) {
return fmt.Errorf("err is %w: %v", ErrConditionCheckFail, err)
}
return err
}
var typeOfBytes = reflect.TypeOf([]byte(nil))
type fieldInfo struct {
structFieldInfo
kind reflect.Kind
value interface{}
isZero bool
values map[string]interface{} // for prefix field
}
func fillStructFromFields(t reflect.Type, v reflect.Value, fields map[string]*fieldInfo) {
for i := 0; i < t.NumField(); i++ {
value := v.Field(i)
field := t.Field(i)
si := getStructFieldInfo(field)
fieldInfo, ok := fields[si.fieldName]
if !ok {
continue
}
if fieldInfo.isPrefixCol {
if value.Kind() != reflect.Map { //todo failfast in Check function if kind is not map
continue
}
valueType := value.Type().Elem()
if value.IsNil() {
value.Set(reflect.MakeMap(value.Type()))
}
for k, v := range fieldInfo.values {
k1 := reflect.ValueOf(k)
v1 := reflect.ValueOf(v)
// we must ensure the field type in table is same as type defined in struct, otherwise, it will panic
if valueType.Kind() == v1.Type().Kind() || valueType.Kind() == reflect.Interface {
if v1.Interface() != nil {
value.SetMapIndex(k1, v1)
}
}
}
} else {
// we can't simply value.Set(reflect.ValueOf(structFieldInfo.value)) because if the field type in table is not match our type
// defined in struct, it will panic
if value.Kind() == reflect.ValueOf(fieldInfo.value).Kind() || value.Kind() == reflect.Interface {
if fieldInfo.value != nil {
value.Set(reflect.ValueOf(fieldInfo.value))
}
}
}
}
}
func getTableInfoFromStruct(r interface{}) (table string, pkFields, colFields []*fieldInfo) {
v := reflect.ValueOf(r).Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
value := v.Field(i)
kind := value.Kind()
si := getStructFieldInfo(t.Field(i))
if si.tableName != "" {
table = si.tableName
}
if si.fieldName == "" {
continue
}
f := &fieldInfo{
structFieldInfo: si,
kind: kind,
value: value.Interface(),
isZero: value.IsZero(),
}
if f.isPk {
pkFields = append(pkFields, f)
} else {
colFields = append(colFields, f)
}
}
if table == "" {
panic("no ts_table tag found")
}
return
}
func generateInfo(v reflect.Value, t reflect.Type) (*PrimaryKey, map[string]*fieldInfo, string) {
table := ""
pk := new(PrimaryKey)
fields := make(map[string]*fieldInfo)
for i := 0; i < t.NumField(); i++ {
value := v.Field(i)
kind := value.Kind()
si := getStructFieldInfo(t.Field(i))
if si.tableName != "" {
table = si.tableName
}
if si.fieldName == "" {
// this field is not relate to ts, just ignore
continue
}
//if !value.IsZero() && si.isAutoIncPk {
// panic("autoInc pk do not accept any value")
//}
//todo: make sure the struct field type is bool int64 string float []byte, otherwise fail fast
v := value.Interface()
f := &fieldInfo{
structFieldInfo: si,
kind: kind,
value: v,
isZero: value.IsZero(),
}
if f.isPrefixCol {
if kind != reflect.Map {
panic("ts_col_prefix field must be a map")
}
values := make(map[string]interface{})
iter := value.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
values[k.String()] = v.Interface()
}
f.values = values
}
fields[si.fieldName] = f
if !si.isPk {
continue
}
if si.isHashPk {
pk.AddPrimaryKeyColumn(si.fieldName, addHashPrefix(v.(string)))
} else if si.isAutoIncPk && value.IsZero() {
pk.AddPrimaryKeyColumnWithAutoIncrement(si.fieldName)
} else {
pk.AddPrimaryKeyColumn(si.fieldName, v)
}
}
return pk, fields, table
}
type structFieldInfo struct {
tableName string
fieldName string
isPk bool
isHashPk bool
isAutoIncPk bool
isPrefixCol bool
isAtomicIncCol bool
columnPrefix string
}
//Pk1 string `ts_pk:"pk1,hash" ts_table:"test_auto_inc"`
//Pk2 int64 `ts_pk:"pk2,auto_inc"`
//ColAny int64 `ts_col:"col1,atomic"`
//ColsAtomic map[string]int64 `ts_col_prefix:"c_,atomic"`
func getStructFieldInfo(field reflect.StructField) structFieldInfo {
pkStr := field.Tag.Get("ts_pk")
colStr := field.Tag.Get("ts_col")
colPrefixStr := field.Tag.Get("ts_col_prefix")
info := structFieldInfo{}
if pkStr != "" {
splits := strings.Split(pkStr, ",")
pkName := splits[0]
isHashPk := strings.Contains(pkStr, "hash")
isAutoIncPk := strings.Contains(pkStr, "auto_inc")
tabName := field.Tag.Get("ts_table")
info = structFieldInfo{
tableName: tabName,
fieldName: pkName,
isPk: true,
isHashPk: isHashPk,
isAutoIncPk: isAutoIncPk,
}
} else if colStr != "" {
splits := strings.Split(colStr, ",")
colName := splits[0]
isAtomicIncCol := strings.Contains(colStr, "atomic")
info = structFieldInfo{
fieldName: colName,
isAtomicIncCol: isAtomicIncCol,
}
} else if colPrefixStr != "" {
splits := strings.Split(colPrefixStr, ",")
colName := splits[0]
isAtomicIncCol := strings.Contains(colPrefixStr, "atomic")
info = structFieldInfo{
fieldName: colName,
isAtomicIncCol: isAtomicIncCol,
isPrefixCol: true,
columnPrefix: colName,
}
} else {
info = structFieldInfo{}
}
//todo: 1. hash must be first field, 2. cannot isHashPk and isAutoIncPk simultaneously
//todo: ensure field type is int64/bytes/string/float64
//todo 3. panic("table tag must only be defined in first field")
if info.isAtomicIncCol {
kind := field.Type.Kind()
if kind != reflect.Int64 && kind != reflect.Map {
panic("atomic increment column type must be int64")
}
if kind == reflect.Map {
elemKind := field.Type.Elem().Kind()
if elemKind != reflect.Int64 {
panic("atomic increment column type must be int64")
}
}
}
return info
}
func getKnownPrefixFieldOfColumn(fields map[string]*fieldInfo, col string) (*fieldInfo, string) {
for _, f := range fields {
if !f.isPrefixCol {
continue
}
if strings.HasPrefix(col, f.columnPrefix) {
return f, strings.TrimPrefix(col, f.columnPrefix)
}
}
return nil, ""
}
func fillColsToFieldInfos(columns []*AttributeColumn, fields map[string]*fieldInfo) {
for _, column := range columns {
if field, ok := fields[column.ColumnName]; ok {
field.value = column.Value
continue
}
field, realKey := getKnownPrefixFieldOfColumn(fields, column.ColumnName)
if field == nil {
continue
}
field.values[realKey] = column.Value
}
}
func fillPKsToFieldInfos(primaryKey PrimaryKey, fields map[string]*fieldInfo) {
for _, key := range primaryKey.PrimaryKeys {
field := fields[key.ColumnName]
if field.isHashPk {
field.value = trimHashPrefix(key.Value.(string))
} else {
field.value = key.Value
}
}
}