-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathobject.go
557 lines (462 loc) · 12.3 KB
/
object.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* Licensed to the AcmeStack under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reflection
import (
"reflect"
"github.com/acmestack/gobatis/common"
"github.com/acmestack/gobatis/errors"
"github.com/acmestack/gobatis/logging"
)
const (
ObjectUnknown = iota
ObjectSimpletype
ObjectStruct
ObjectSlice
ObjectMap
ObjectCustom = 50000
)
type Object interface {
Kind() int
// New 生成克隆空对象
New() Object
// NewElem 获得对象的元素
NewElem() Object
// SetField 设置字段
SetField(name string, v interface{})
// AddValue 添加元素值
AddValue(v reflect.Value)
// GetClassName 获得对象名称
GetClassName() string
// CanSetField 是否能设置field
CanSetField() bool
// CanAddValue 是否能添加值
CanAddValue() bool
// NewValue 获得值
NewValue() reflect.Value
// CanSet 是否能够设置
CanSet(v reflect.Value) bool
// SetValue 设置值
SetValue(v reflect.Value)
// GetValue 获得值
GetValue() reflect.Value
// ResetValue 变换value对象
ResetValue(v reflect.Value)
}
var modelNameType reflect.Type
func SetModelNameType(mtype reflect.Type) {
modelNameType = mtype
}
type Settable struct {
//值
Value reflect.Value
}
type Newable struct {
Type reflect.Type
}
type StructInfo struct {
//包含pkg的名称
ClassName string
//Model名称(目前用于xml解析是struct的前缀:#{x.username} 中的x)
Name string
//表字段和实体字段映射关系
FieldNameMap map[string]string
Settable
Newable
}
type SliceInfo struct {
//包含pkg的名称
ClassName string
Elem Object
Settable
Newable
}
type SimpleTypeInfo struct {
//包含pkg的名称
ClassName string
Settable
Newable
}
type MapInfo struct {
//包含pkg的名称
ClassName string
//元素类型
ElemType reflect.Type
Settable
Newable
}
func (settable *Settable) CanSet(v reflect.Value) bool {
if settable.Value.Kind() != v.Kind() {
logging.Warn("Set value failed")
return false
}
destClass := GetTypeClassName(settable.Value.Type())
srcClass := GetTypeClassName(v.Type())
if destClass != srcClass {
logging.Warn("different type: %settable %settable\n", destClass, srcClass)
return false
}
return true
}
func (settable *Settable) SetValue(v reflect.Value) {
if !settable.Value.IsValid() {
settable.Value = v
}
if settable.CanSet(v) {
settable.Value.Set(v)
}
}
func (settable *Settable) ResetValue(v reflect.Value) {
settable.Value = v
}
func (settable *Settable) GetValue() reflect.Value {
return settable.Value
}
func (newable *Newable) NewValue() reflect.Value {
return reflect.New(newable.Type).Elem()
}
func (structInfo *StructInfo) New() Object {
ret := &StructInfo{
ClassName: structInfo.ClassName,
Name: structInfo.Name,
FieldNameMap: structInfo.FieldNameMap,
}
ret.Type = structInfo.Type
ret.Value = reflect.New(structInfo.Type).Elem()
return ret
}
func (structInfo *StructInfo) NewElem() Object {
return nil
}
func (structInfo *StructInfo) SetField(name string, ov interface{}) {
fieldName := structInfo.FieldNameMap[name]
if fieldName != "" {
f := structInfo.Value.FieldByName(fieldName)
if f.IsValid() {
SetValue(f, ov)
}
}
}
func (structInfo *StructInfo) AddValue(v reflect.Value) {
}
func (structInfo *StructInfo) GetClassName() string {
return structInfo.ClassName
}
func (structInfo *StructInfo) Kind() int {
return ObjectStruct
}
func (structInfo *StructInfo) CanSetField() bool {
return true
}
func (structInfo *StructInfo) CanAddValue() bool {
return false
}
func (sliceInfo *SliceInfo) New() Object {
ret := &SliceInfo{
Elem: sliceInfo.Elem.New(),
}
ret.Type = sliceInfo.Type
ret.Value = reflect.New(sliceInfo.Type).Elem()
return ret
}
func (sliceInfo *SliceInfo) NewElem() Object {
return sliceInfo.Elem.New()
}
func (sliceInfo *SliceInfo) SetField(name string, v interface{}) {
logging.Info("slice not support SetField")
}
func (sliceInfo *SliceInfo) AddValue(v reflect.Value) {
if !sliceInfo.Elem.CanSet(v) {
logging.Warn("Add value failed, different kind")
return
}
newValue := reflect.Append(sliceInfo.Value, v)
//直接设置,不使用o.SetValue,效率更高
sliceInfo.Value.Set(newValue)
}
func (sliceInfo *SliceInfo) GetClassName() string {
return sliceInfo.ClassName
}
func (sliceInfo *SliceInfo) Kind() int {
return ObjectSlice
}
func (sliceInfo *SliceInfo) CanSetField() bool {
return false
}
func (sliceInfo *SliceInfo) CanAddValue() bool {
return true
}
func (simpleTypeInfo *SimpleTypeInfo) New() Object {
ret := &SimpleTypeInfo{
ClassName: simpleTypeInfo.ClassName,
}
ret.Type = simpleTypeInfo.Type
ret.Value = reflect.New(simpleTypeInfo.Type).Elem()
return ret
}
func (simpleTypeInfo *SimpleTypeInfo) NewElem() Object {
return nil
}
func (simpleTypeInfo *SimpleTypeInfo) SetField(name string, ov interface{}) {
SetValue(simpleTypeInfo.Value, ov)
}
func (simpleTypeInfo *SimpleTypeInfo) AddValue(v reflect.Value) {
}
func (simpleTypeInfo *SimpleTypeInfo) GetClassName() string {
return simpleTypeInfo.ClassName
}
// CanSet 直接返回true,需要通过SetValue判断
func (simpleTypeInfo *SimpleTypeInfo) CanSet(v reflect.Value) bool {
return true
}
func (simpleTypeInfo *SimpleTypeInfo) SetValue(v reflect.Value) {
if !simpleTypeInfo.Value.IsValid() {
simpleTypeInfo.Value = v
}
if !SetValue(simpleTypeInfo.Value, v.Interface()) {
logging.Warn("SimpleTypeInfo SetValue failed")
}
}
func (simpleTypeInfo *SimpleTypeInfo) Kind() int {
return ObjectSimpletype
}
func (simpleTypeInfo *SimpleTypeInfo) CanSetField() bool {
return false
}
func (simpleTypeInfo *SimpleTypeInfo) CanAddValue() bool {
return false
}
func (mapInfo *MapInfo) CanSet(v reflect.Value) bool {
if mapInfo.Value.Kind() != v.Kind() {
logging.Warn("Set value failed")
return false
}
rt := v.Type()
if rt.Key().Kind() != reflect.String || rt.Elem().Kind() != reflect.Interface {
logging.Warn("Map type support map[string]interface{} only")
return false
}
return true
}
// SetValue TODO: 目前仅支持map[string]interface{},需增加其他类型支持
func (mapInfo *MapInfo) SetValue(v reflect.Value) {
if mapInfo.CanSet(v) {
mapInfo.Value.Set(v)
}
}
func (mapInfo *MapInfo) New() Object {
ret := &MapInfo{
ClassName: mapInfo.ClassName,
ElemType: mapInfo.ElemType,
}
ret.Value = reflect.New(mapInfo.Type).Elem()
return ret
}
// NewElem FIXME: return nil,需要对map元素解析
func (mapInfo *MapInfo) NewElem() Object {
return nil
}
func (mapInfo *MapInfo) SetField(name string, ov interface{}) {
v := reflect.New(mapInfo.ElemType).Elem()
if SetValue(v, ov) {
mapInfo.Value.SetMapIndex(reflect.ValueOf(name), v)
}
}
func (mapInfo *MapInfo) AddValue(v reflect.Value) {
}
func (mapInfo *MapInfo) CanSetField() bool {
return true
}
func (mapInfo *MapInfo) CanAddValue() bool {
return false
}
func (mapInfo *MapInfo) Kind() int {
return ObjectMap
}
func (mapInfo *MapInfo) GetClassName() string {
return mapInfo.ClassName
}
func GetObjectInfo(model interface{}) (Object, error) {
rt := reflect.TypeOf(model)
rv := reflect.ValueOf(model)
if err := MustPtrValue(rv); err != nil {
return nil, err
}
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
rv = rv.Elem()
}
return GetReflectObjectInfo(rt, rv)
}
func GetReflectObjectInfo(rt reflect.Type, rv reflect.Value) (Object, error) {
if IsSimpleType(rt) {
return GetReflectSimpleTypeInfo(rt, rv)
}
switch rt.Kind() {
case reflect.Struct:
return GetReflectStructInfo(rt, rv)
case reflect.Slice:
return GetReflectSliceInfo(rt, rv)
case reflect.Map:
return GetReflectMapInfo(rt, rv)
}
return nil, errors.ObjectNotSupport
}
func GetReflectSimpleTypeInfo(rt reflect.Type, rv reflect.Value) (Object, error) {
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
ret := SimpleTypeInfo{
ClassName: GetTypeClassName(rt),
}
ret.Type = rt
ret.Value = rv
return &ret, nil
}
func GetReflectSliceInfo(rt reflect.Type, rv reflect.Value) (Object, error) {
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
rv = rv.Elem()
}
kind := rt.Kind()
if kind != reflect.Slice {
return nil, errors.ParseObjectNotSlice
}
//获得元素类型
et := rt.Elem()
ev := reflect.New(et).Elem()
elemObj, err := GetReflectObjectInfo(et, ev)
if err != nil {
return nil, err
}
if elemObj.CanAddValue() {
return nil, errors.SliceSliceNotSupport
}
ret := SliceInfo{Elem: elemObj, ClassName: GetTypeClassName(rt)}
ret.Type = rt
ret.Value = rv
return &ret, nil
}
func GetReflectMapInfo(rt reflect.Type, rv reflect.Value) (Object, error) {
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
rv = rv.Elem()
}
kind := rt.Kind()
if kind != reflect.Map {
return nil, errors.ParseObjectNotMap
}
if rt.Key().Kind() != reflect.String {
return nil, errors.GetObjectInfoFailed
}
//TODO: 目前仅支持map[string]interface{},需增加其他类型支持
if rt.Elem().Kind() != reflect.Interface {
logging.Warn("Map type support map[string]interface{} only, but get map[%v]%v \n", rt.Key(), rt.Elem())
return nil, errors.GetObjectInfoFailed
}
ret := MapInfo{ElemType: rt.Elem(), ClassName: GetTypeClassName(rt)}
ret.Type = rt
ret.Value = rv
return &ret, nil
}
//GetStructInfo 解析结构体,使用:
//1、如果结构体中含有gobatis.ModelName类型的字段,则:
// a)、如果含有tag,则使用tag作为tablename;
// b)、如果不含有tag,则使用fieldName作为tablename。
//2、如果结构体中不含有gobatis.ModelName类型的字段,则使用结构体名称作为tablename
//3、如果结构体中含有column的tag,则:
// a)、如果tag为‘-’,则不进行columne与field的映射;
// b)、如果tag不为‘-’使用tag name作为column名称与field映射。
//4、如果结构体中不含有column的tag,则使用field name作为column名称与field映射
//5、如果字段的tag为‘-’,则不进行columne与field的映射;
func GetStructInfo(bean interface{}) (*StructInfo, error) {
return GetReflectStructInfo(reflect.TypeOf(bean), reflect.ValueOf(bean))
}
func GetReflectStructInfo(rt reflect.Type, rv reflect.Value) (*StructInfo, error) {
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
if rv.IsValid() {
rv = rv.Elem()
}
}
kind := rt.Kind()
if kind != reflect.Struct {
return nil, errors.ParseObjectNotStruct
}
objInfo := StructInfo{
FieldNameMap: map[string]string{},
}
objInfo.Type = rt
objInfo.Value = rv
//Default name is struct name
objInfo.Name = rt.Name()
objInfo.ClassName = GetTypeClassName(rt)
//字段解析
for i, j := 0, rt.NumField(); i < j; i++ {
rtf := rt.Field(i)
//if rtf.Type == modelNameType {
// if rtf.Tag != "" {
// objInfo.Name = string(rtf.Tag)
// } else {
// objInfo.Name = rtf.Name
// }
// continue
//}
//没有tag,表字段名与实体字段名一致
if rtf.Tag == "" {
objInfo.FieldNameMap[rtf.Name] = rtf.Name
continue
}
if rtf.Tag == "-" {
continue
}
fieldName := rtf.Name
tagName := rtf.Tag.Get(common.ColumnName)
if tagName == "-" {
continue
} else if tagName != "" {
fieldName = tagName
}
objInfo.FieldNameMap[fieldName] = rtf.Name
continue
}
return &objInfo, nil
}
func (structInfo *StructInfo) MapValue() map[string]interface{} {
paramMap := map[string]interface{}{}
structInfo.FillMapValue(¶mMap)
return paramMap
}
func (structInfo *StructInfo) FillMapValue(paramMap *map[string]interface{}) {
for k, v := range structInfo.FieldNameMap {
f := structInfo.Value.FieldByName(v)
if !f.CanInterface() {
f = reflect.Indirect(f)
}
(*paramMap)[k] = f.Interface()
}
//(*paramMap)["tablename"] = structInfo.Name
}
func GetBeanClassName(model interface{}) string {
rt := reflect.TypeOf(model)
return GetTypeClassName(rt)
}
func GetTypeClassName(rt reflect.Type) string {
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
return rt.String()
}