-
Notifications
You must be signed in to change notification settings - Fork 2
/
proto.go
241 lines (211 loc) · 5.42 KB
/
proto.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
package gtly
import (
"github.com/viant/toolbox/format"
"github.com/viant/xunsafe"
"reflect"
"strings"
"time"
"unicode"
)
const (
defaultPackage = "github.com/viant/gtly"
)
var defaultEmptyValues = map[interface{}]bool{
"": true,
nil: true,
}
//Proto represents generic type prototype
type Proto struct {
Name string
fieldNames map[string]int
fields []Field
accessors []Accessor
mutators []Mutator
OmitEmpty bool
emptyValues map[interface{}]bool
timeLayout string
caseFormat format.Case
outputCaseFormat format.Case
inputCaseFormat format.Case
dataType reflect.Type
xType *xunsafe.Type
kind reflect.Kind
}
//Type returns proto data type
func (p *Proto) Type() reflect.Type {
return p.dataType
}
func (p *Proto) buildType() reflect.Type {
structFields := make([]reflect.StructField, len(p.fields))
for i, field := range p.fields {
if field.Type == nil {
field.Type = typeString
}
pkg := ""
if unicode.IsLower(rune(field.Name[0])) {
pkg = defaultPackage
}
structFields[i] = reflect.StructField{
Name: field.Name,
Type: field.Type,
PkgPath: pkg,
}
p.fields[i].kind = structFields[i].Type.Kind()
}
dataType := reflect.StructOf(structFields)
for i := range p.fields {
p.initField(&p.fields[i], dataType)
}
return dataType
}
func (p *Proto) initField(field *Field, dataType reflect.Type) {
xField := xunsafe.FieldByName(dataType, field.Name)
if p.caseFormat != p.outputCaseFormat {
field.outputName = p.caseFormat.Format(field.Name, p.outputCaseFormat)
}
p.accessors[field.Index].init(field.Index, xField)
p.mutators[field.Index].init(field.Index, xField)
p.indexByNames(field)
}
func (p *Proto) indexByNames(field *Field) {
p.fieldNames[field.Name] = field.Index
if field.InputName != "" && field.InputName != field.Name {
p.fieldNames[field.InputName] = field.Index
}
if field.outputName != "" && field.outputName != field.Name {
p.fieldNames[field.outputName] = field.Index
}
}
//SimpleName returns simple name
func (p *Proto) SimpleName() string {
if index := strings.LastIndex(p.Name, "."); index != -1 {
return p.Name[index+1:]
}
return p.Name
}
//SetOmitEmpty sets omit empty flag
func (p *Proto) SetOmitEmpty(omitEmpty bool) {
p.OmitEmpty = true
if omitEmpty {
if len(p.emptyValues) == 0 {
p.emptyValues = defaultEmptyValues
}
}
}
//SetEmptyValues sets empty values, use only if empty values are non in default map: nil, empty string
func (p *Proto) SetEmptyValues(values ...interface{}) {
p.emptyValues = make(map[interface{}]bool)
for i := range values {
p.emptyValues[values[i]] = true
}
}
//OutputCaseFormat set output case format
func (p *Proto) OutputCaseFormat(source, output format.Case) error {
for i, field := range p.fields {
p.fields[i].outputName = source.Format(field.Name, output)
}
return nil
}
//InputCaseFormat set output case format
func (p *Proto) InputCaseFormat(source, input format.Case) error {
p.inputCaseFormat = source
for i, field := range p.fields {
p.fields[i].outputName = source.Format(field.Name, input)
}
return nil
}
//Hide set hidden flag for the Field
func (p *Proto) Hide(name string) {
field := p.Field(name)
if field == nil {
return
}
field.hidden = true
}
//Show remove hidden flag for supplied Field
func (p *Proto) Show(name string) {
field := p.Field(name)
if field == nil {
return
}
field.hidden = false
}
//Size returns proto size
func (p *Proto) Size() int {
return len(p.fields)
}
func (p *Proto) asMap(values []interface{}) map[string]interface{} {
var result = make(map[string]interface{})
for _, field := range p.fields {
if field.hidden {
continue
}
var value interface{}
if field.Index < len(values) {
value = values[field.Index]
}
value = Value(value)
fieldName := field.Name
if field.outputName != "" {
fieldName = field.outputName
}
result[fieldName] = value
}
return result
}
func reallocateIfNeeded(size int, data []interface{}) []interface{} {
if size >= len(data) {
for i := len(data); i < size; i++ {
data = append(data, nil)
}
}
return data
}
//Fields returns fields list
func (p *Proto) Fields() []Field {
return p.fields
}
//Field returns Field for specified Name
func (p *Proto) Field(name string) *Field {
index := p.fieldNames[name]
return &p.fields[index]
}
//FieldAt returns Field at position
func (p *Proto) FieldAt(index int) *Field {
return &p.fields[index]
}
//Mutator returns field mutator
func (p *Proto) Mutator(fieldName string) *Mutator {
field := p.Field(fieldName)
return &p.mutators[field.Index]
}
//MutatorAt returns field mutator
func (p *Proto) MutatorAt(index int) *Mutator {
return &p.mutators[index]
}
//Accessor returns a field accessor
func (p *Proto) Accessor(fieldName string) *Accessor {
field := p.Field(fieldName)
return &p.accessors[field.Index]
}
//AccessorAt returns a field accessor
func (p *Proto) AccessorAt(index int) *Accessor {
return &p.accessors[index]
}
//newProto create a data type prototype
func newProto(name string, fields []*Field) *Proto {
result := &Proto{
Name: name,
fieldNames: make(map[string]int),
fields: make([]Field, len(fields)),
accessors: make([]Accessor, len(fields)),
mutators: make([]Mutator, len(fields)),
}
result.timeLayout = time.RFC3339
for i := range fields {
result.fields[i] = *fields[i]
}
result.dataType = result.buildType()
result.xType = xunsafe.NewType(result.dataType)
return result
}