-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_desc.go
220 lines (181 loc) · 4.81 KB
/
field_desc.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
package sod
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
)
var (
timeType = reflect.TypeOf(time.Time{})
ErrFieldDescModif = errors.New("field descriptor changed")
)
type FieldDescriptor struct {
Path string `json:"path"`
Type string `json:"type"`
Constraints Constraints `json:"constraints"`
}
func (d *FieldDescriptor) cast() string {
switch d.Type {
case "int", "int8", "int16", "int32", "int64", "time.Time":
return "int64"
case "uint", "uint8", "uint16", "uint32", "uint64":
return "uint64"
case "float32", "float64":
return "float64"
case "string":
return d.Type
default:
panic(fmt.Sprintf("unkwnown type to cast %s", d.Type))
}
}
func (d *FieldDescriptor) Transform(o interface{}) {
switch i := o.(type) {
case Object:
d.Constraints.TransformField(d.Path, i)
default:
d.Constraints.Transform(i)
}
}
func (d *FieldDescriptor) FieldEqual(other *FieldDescriptor) bool {
return d.Path == other.Path && d.Type == other.Type
}
func (d *FieldDescriptor) DeepEqual(other *FieldDescriptor) bool {
return reflect.DeepEqual(d, other)
}
func (d FieldDescriptor) String() string {
return fmt.Sprintf("path=%s type=%s constraints=(%s)", d.Path, d.Type, d.Constraints)
}
type FieldDescMap map[string]FieldDescriptor
func FieldDescriptors(from Object) (desc FieldDescMap) {
desc = make(FieldDescMap)
sdesc := make([]FieldDescriptor, 0)
recFieldDescriptors(reflect.ValueOf(from), "", &sdesc)
for _, fd := range sdesc {
desc[fd.Path] = fd
}
return
}
func (m FieldDescMap) CompatibleWith(target FieldDescMap) (err error) {
for p, fd := range m {
if ofd, ok := target[p]; !ok {
return fmt.Errorf("target %w %s", ErrUnkownField, p)
} else if !fd.DeepEqual(&ofd) {
return fmt.Errorf("%w %s", ErrFieldDescModif, ofd)
}
}
for p, ofd := range target {
if fd, ok := m[p]; !ok {
return fmt.Errorf("source %w %s", ErrUnkownField, p)
} else if !ofd.DeepEqual(&fd) {
return fmt.Errorf("%w %s", ErrFieldDescModif, fd)
}
}
return
}
func (m FieldDescMap) FieldsCompatibleWith(target FieldDescMap) (err error) {
for p, fd := range m {
if ofd, ok := target[p]; !ok {
return fmt.Errorf("target %w %s", ErrUnkownField, p)
} else if !fd.FieldEqual(&ofd) {
return fmt.Errorf("%w %s", ErrFieldDescModif, ofd)
}
}
for p, ofd := range target {
if fd, ok := m[p]; !ok {
return fmt.Errorf("source %w %s", ErrUnkownField, p)
} else if !ofd.FieldEqual(&fd) {
return fmt.Errorf("%w %s", ErrFieldDescModif, fd)
}
}
return
}
func (m FieldDescMap) Transformers() (t []FieldDescriptor) {
t = make([]FieldDescriptor, 0)
for _, fd := range m {
if fd.Constraints.Transformer() {
t = append(t, fd)
}
}
return
}
func (m FieldDescMap) GetDescriptor(fpath string) (d FieldDescriptor, ok bool) {
d, ok = m[fpath]
return
}
func (m FieldDescMap) Constraint(fpath string, c Constraints) (err error) {
if d, ok := m[fpath]; ok {
d.Constraints = c
m[fpath] = d
return nil
}
return fmt.Errorf("%w %s", ErrUnkownField, fpath)
}
func fdFromType(path string, tag string, fieldType reflect.Type) FieldDescriptor {
fd := FieldDescriptor{
Path: path,
Type: fieldType.String(),
}
for _, tv := range strings.Split(tag, ",") {
switch tv {
case "index":
fd.Constraints.Index = true
case "unique":
fd.Constraints.Index = true
fd.Constraints.Unique = true
case "lower":
fd.Constraints.Lower = true
case "upper":
fd.Constraints.Upper = true
}
}
return fd
}
func joinFieldPath(path, fieldName string) string {
if path == "" {
return fieldName
}
return strings.Join([]string{path, fieldName}, ".")
}
func recFieldDescriptors(v reflect.Value, path string, fds *[]FieldDescriptor) {
typ := v.Type()
switch v.Kind() {
default:
*fds = append(*fds, fdFromType(path, "", typ))
case reflect.Ptr:
if v.Elem().Kind() == reflect.Struct {
recFieldDescriptors(v.Elem(), path, fds)
} else {
*fds = append(*fds, fdFromType(path, "", typ))
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
fieldValue := v.Field(i)
structField := typ.Field(i)
// if field is not exported we continue
if !structField.IsExported() {
continue
}
switch fieldValue.Kind() {
case reflect.Ptr:
// create a new field
fieldValue = reflect.New(structField.Type.Elem())
recFieldDescriptors(fieldValue, joinFieldPath(path, structField.Name), fds)
continue
case reflect.Struct:
// don't treat struct time.Time as a struct
if !fieldValue.Type().AssignableTo(timeType) {
recFieldDescriptors(fieldValue, joinFieldPath(path, structField.Name), fds)
continue
}
}
// process struct field
tag, _ := structField.Tag.Lookup("sod")
fdPath := structField.Name
if path != "" {
fdPath = fmt.Sprintf("%s.%s", path, fdPath)
}
*fds = append(*fds, fdFromType(fdPath, tag, fieldValue.Type()))
}
}
}