-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel_field.go
202 lines (157 loc) · 4.03 KB
/
model_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package dmod
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/jinzhu/copier"
)
var (
TypeOfModleField = reflect.TypeOf((*ModelField)(nil)).Elem()
TypeOfError = reflect.TypeOf((*error)(nil)).Elem()
)
type ModelField struct {
name string
fieldValue reflect.Value
}
func (p *ModelField) Name() string {
return p.name
}
func (p *ModelField) Field(name string) *ModelField {
if len(name) == 0 {
return p
}
if !p.fieldValue.IsValid() {
return nil
}
names := strings.Split(name, ".")
m := &ModelField{
name: p.name + "." + names[0],
fieldValue: indirect(p.fieldValue).FieldByName(name),
}
newNames := strings.Join(names[1:], ".")
if len(newNames) == 0 {
return m
}
return m.Field(newNames)
}
func (p *ModelField) Value(v interface{}) (err error) {
if !p.fieldValue.IsValid() {
return
}
err = copier.Copy(v, p.fieldValue.Interface())
return
}
func (p *ModelField) Interface() interface{} {
if !p.fieldValue.IsValid() {
return nil
}
return p.fieldValue.Addr().Interface()
}
func (p *ModelField) Set(value interface{}) (err error) {
if !p.fieldValue.IsValid() {
return errors.New("field value not valid")
}
if !p.fieldValue.CanAddr() {
return errors.New("using unaddressable value")
}
reflectValue, ok := value.(reflect.Value)
if !ok {
reflectValue = reflect.ValueOf(value)
}
fieldValue := p.fieldValue
if reflectValue.IsValid() {
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
} else {
if fieldValue.Kind() == reflect.Ptr {
if fieldValue.IsNil() {
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
}
fieldValue = fieldValue.Elem()
}
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
} else {
err = fmt.Errorf("could not convert argument of field %s from %s to %s", p.Name, reflectValue.Type(), fieldValue.Type())
}
}
} else {
p.fieldValue.Set(reflect.Zero(p.fieldValue.Type()))
}
return err
}
func (p *ModelField) Call(fn interface{}) (err error) {
if fn == nil {
err = errors.New("the model filed Call Args fn is nil")
return
}
if !p.fieldValue.IsValid() {
err = errors.New("this field is not valid")
return
}
valFn := reflect.ValueOf(fn)
if valFn.Kind() != reflect.Func {
err = errors.New("the model filed Call Args fn should be func")
return
}
if valFn.Type().NumIn() != 1 && valFn.Type().NumIn() != 2 {
err = errors.New("fn's args should be `func(Any) error` OR `func(*ModleField) error` OR `func(Any, *ModleField) error`")
return
}
if valFn.Type().NumOut() > 1 {
err = errors.New("fn's return value should be void OR error")
return
}
if valFn.Type().NumOut() == 1 {
if valFn.Type().Out(0) != TypeOfError {
err = errors.New("fn's return value should be void OR error")
return
}
}
var argTypes []reflect.Type
var argTypesEle []bool
for i := 0; i < valFn.Type().NumIn(); i++ {
inType := valFn.Type().In(i)
if inType.Kind() == reflect.Ptr {
argTypes = append(argTypes, inType.Elem())
argTypesEle = append(argTypesEle, true)
} else {
argTypes = append(argTypes, inType)
argTypesEle = append(argTypesEle, false)
}
}
if len(argTypes) == 2 {
if argTypes[0] == argTypes[1] {
err = errors.New("arg type is equal")
return
} else if argTypes[0] != TypeOfModleField && argTypes[1] != TypeOfModleField {
err = errors.New("one arg should be type of *ModelField")
return
}
}
var argVals []reflect.Value
for i := 0; i < len(argTypes); i++ {
if argTypes[i] == TypeOfModleField {
argVals = append(argVals, reflect.ValueOf(p))
} else {
argV := reflect.New(argTypes[i])
err = copier.Copy(argV.Interface(), p.fieldValue.Interface())
if err != nil {
return
}
if !argTypesEle[i] {
argV = argV.Elem()
}
argVals = append(argVals, argV)
}
}
retVals := valFn.Call(argVals)
if len(retVals) == 1 {
if retVals[0].IsValid() && !retVals[0].IsNil() {
err = retVals[0].Interface().(error)
return
}
}
return
}