-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
239 lines (214 loc) · 6.75 KB
/
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
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
// Copyright 2021 Telefonica Cybersecurity & Cloud Tech SL
//
// 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 golium
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/tidwall/gjson"
)
// KindFormatter with format to apply conversion
type KindFormatter interface {
format(destination reflect.Value, fieldValueStr string, value interface{}) error
}
// ConfigurePattern func to apply transformation into destination
type ConfigurePattern func(destination reflect.Value, fieldValueStr string, value interface{}) error
// FieldConversor
type FieldConversor struct {
Pattern ConfigurePattern
}
// format applies defined pattern to format destination values
func (c FieldConversor) format(
destination reflect.Value,
fieldValueStr string,
value interface{}) error {
return c.Pattern(destination, fieldValueStr, value)
}
var StrategyFormat = map[reflect.Kind]*FieldConversor{
reflect.Slice: NewSliceConverter(),
reflect.String: NewStringConverter(),
reflect.Bool: NewBoolConverter(),
reflect.Int: NewInt64Converter(),
reflect.Int8: NewInt64Converter(),
reflect.Int16: NewInt64Converter(),
reflect.Int32: NewInt64Converter(),
reflect.Int64: NewInt64Converter(),
reflect.Uint: NewUInt64Converter(),
reflect.Uint8: NewUInt64Converter(),
reflect.Uint16: NewUInt64Converter(),
reflect.Uint32: NewUInt64Converter(),
reflect.Uint64: NewUInt64Converter(),
reflect.Float32: NewFloat64Converter(),
reflect.Float64: NewFloat64Converter(),
reflect.Complex64: NewComplex64Converter(),
reflect.Complex128: NewComplex64Converter(),
}
// NewSliceConverter Constructor
func NewSliceConverter() *FieldConversor {
return &FieldConversor{Pattern: sliceType}
}
// sliceType conversion pattern to apply
func sliceType(destination reflect.Value, fieldValueStr string, value interface{}) error {
array, ok := value.([]gjson.Result)
if !ok {
return fmt.Errorf(
"failed parsing value '%v', not a JSON array",
value)
}
length := len(array)
var fv reflect.Value
if length > 0 {
fv = makeSlice(array[0], length)
for i, v := range array {
setSliceValue(fv.Index(i), v)
}
}
destination.Set(fv)
return nil
}
// NewStringConverter Constructor
func NewStringConverter() *FieldConversor {
return &FieldConversor{Pattern: stringType}
}
// stringType conversion pattern to apply
func stringType(destination reflect.Value, fieldValueStr string, value interface{}) error {
destination.SetString(fieldValueStr)
return nil
}
// NewBoolConverter Constructor
func NewBoolConverter() *FieldConversor {
return &FieldConversor{Pattern: boolType}
}
// boolType conversion pattern to apply
func boolType(destination reflect.Value, fieldValueStr string, value interface{}) error {
v, err := strconv.ParseBool(fieldValueStr)
if err != nil {
return fmt.Errorf("failed parsing to boolean the value '%s'",
fieldValueStr)
}
destination.SetBool(v)
return nil
}
// NewInt64Converter Constructor
func NewInt64Converter() *FieldConversor {
return &FieldConversor{Pattern: int64Type}
}
// int64Type conversion pattern to apply
func int64Type(destination reflect.Value, fieldValueStr string, value interface{}) error {
v, err := strconv.ParseInt(fieldValueStr, 10, 64)
if err != nil {
return fmt.Errorf("failed parsing to integer '%s' with destination '%v'",
fieldValueStr,
value)
}
destination.SetInt(v)
return nil
}
// NewUInt64Converter Constructor
func NewUInt64Converter() *FieldConversor {
return &FieldConversor{Pattern: uint64Type}
}
// uint64Type conversion pattern to apply
func uint64Type(destination reflect.Value, fieldValueStr string, value interface{}) error {
v, err := strconv.ParseUint(fieldValueStr, 10, 64)
if err != nil {
return fmt.Errorf("failed parsing to unsigned integer '%s' with destination '%v'",
fieldValueStr,
value)
}
destination.SetUint(v)
return nil
}
// NewFloat64Converter Constructor
func NewFloat64Converter() *FieldConversor {
return &FieldConversor{Pattern: float64Type}
}
// float64Type conversion pattern to apply
func float64Type(destination reflect.Value, fieldValueStr string, value interface{}) error {
v, err := strconv.ParseFloat(fieldValueStr, 64)
if err != nil {
return fmt.Errorf("failed parsing to float '%s' with destination '%v'",
fieldValueStr,
value)
}
destination.SetFloat(v)
return nil
}
// NewComplex64Converter Constructor
func NewComplex64Converter() *FieldConversor {
return &FieldConversor{Pattern: complex64Type}
}
// complex64Type conversion pattern to apply
func complex64Type(destination reflect.Value, fieldValueStr string, value interface{}) error {
v, err := strconv.ParseComplex(fieldValueStr, 128)
if err != nil {
return fmt.Errorf("failed parsing to complex '%s' with destination '%v'",
fieldValueStr,
value)
}
destination.SetComplex(v)
return nil
}
// exctractField to apply pattern
func exctractField(destination *reflect.Value, name string) error {
if destination.Kind() == reflect.Ptr {
*destination = reflect.Indirect(*destination)
}
if destination.Kind() != reflect.Struct {
return fmt.Errorf("destination must be a struct")
}
*destination = destination.FieldByNameFunc(func(n string) bool {
return strings.EqualFold(n, name)
})
if !destination.IsValid() {
return fmt.Errorf("field '%s' is not valid", name)
}
if !destination.CanSet() {
return fmt.Errorf("field '%s' cannot be set", name)
}
if destination.Kind() == reflect.Ptr {
fv := reflect.New(destination.Type().Elem())
destination.Set(fv)
*destination = fv.Elem()
}
return nil
}
// makeSlice from element with selected length
func makeSlice(element gjson.Result, length int) reflect.Value {
var rv reflect.Value
switch element.Type {
case gjson.False, gjson.True:
var b bool
rv = reflect.ValueOf(b)
case gjson.Number:
var i int
rv = reflect.ValueOf(i)
case gjson.String, gjson.JSON, gjson.Null:
var s string
rv = reflect.ValueOf(s)
}
return reflect.MakeSlice(reflect.SliceOf(rv.Type()), length, length)
}
// setSliceValue to field with value
func setSliceValue(field reflect.Value, value gjson.Result) {
switch value.Type {
case gjson.False, gjson.True:
field.Set(reflect.ValueOf(value.Bool()))
case gjson.Number:
field.Set(reflect.ValueOf(value.Int()))
case gjson.String, gjson.JSON, gjson.Null:
field.Set(reflect.ValueOf(value.String()))
}
}