-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
358 lines (322 loc) · 8.48 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
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
package main
import (
"encoding/json"
"fmt"
"html"
// "regexp"
"strconv"
ttemplate "text/template"
"strings"
)
// var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
type exporterTemplate ttemplate.Template
func (tmpl *exporterTemplate) MarshalText() (text []byte, err error) {
return []byte(tmpl.Tree.Root.String()), nil
}
type Field struct {
raw string
tmpl *exporterTemplate
}
// create a new key or value Field that can be a GO template
func NewField(name string, customTemplate *exporterTemplate) (*Field, error) {
var tmpl *ttemplate.Template
var err error
if strings.Contains(name, "{") {
if customTemplate != nil {
ptr := (*ttemplate.Template)(customTemplate)
tmpl, err = ptr.Clone()
if err != nil {
return nil, fmt.Errorf("field template clone for %s is invalid: %s", name, err)
}
} else {
// tmpl = template.New("field").Funcs(sprig.FuncMap())
tmpl = ttemplate.New("field").Funcs(mymap())
}
tmpl, err = tmpl.Parse(name)
if err != nil {
return nil, fmt.Errorf("field template %s is invalid: %s", name, err)
}
}
return &Field{
raw: name,
tmpl: (*exporterTemplate)(tmpl),
}, nil
}
// obtain float64 from a var of any type
func RawGetValueFloat(curval any) float64 {
var f_value float64
var err error
if curval == nil {
return 0.0
}
// it is a raw value not a template look in "item"
switch curval := curval.(type) {
case int64:
f_value = float64(curval)
case float64:
f_value = curval
case string:
if f_value, err = strconv.ParseFloat(strings.Trim(curval, "\r\n "), 64); err != nil {
f_value = 0
}
default:
f_value = 0
// value := row[v].(float64)
}
return f_value
}
// obtain string from a var of any type
func RawGetValueString(curval any) string {
res := ""
if curval == nil {
return res
}
switch curval := curval.(type) {
case string:
res = curval
// case map[any]any:
// bytes_res, err := json.MarshalIndent(curval, "", "")
// if err != nil {
// fmt.Printf("error: %s\n", string(err.Error()))
// }
// res = string(bytes_res)
default:
res = fmt.Sprintf("%v", curval)
}
return strings.Trim(res, "\r\n ")
}
// obtain a final string value from Field
// use template if one is defined using item to symbols table
// else
// check if value must be sustituted using provided sub map
// if check_item set to true, check if the resulting value exists in item symbols table
// else return raw value (simple string)
func (f *Field) GetValueString(
item map[string]interface{},
sub map[string]string,
check_item bool) (res string, err error) {
if f == nil {
return "", nil
}
if f.tmpl != nil {
defer func() {
// res and err are named out parameters, so if we set value for them in defer
// set the returned values
ok := false
if r := recover(); r != nil {
if err, ok = r.(error); !ok {
err = fmt.Errorf("panic in GetValueString template with undefined error")
}
res = ""
}
}()
tmp_res := new(strings.Builder)
err = ((*ttemplate.Template)(f.tmpl)).Execute(tmp_res, &item)
if err != nil {
return "", err
}
// obtain final string from builder
tmp := tmp_res.String()
// remove before and after blank chars
tmp = strings.Trim(tmp, "\r\n ")
// unescape string
return html.UnescapeString(tmp), nil
} else {
val := f.raw
// check if there is a transformation value in sub[stitution] map
if sub != nil {
if _, ok := sub[val]; ok {
val = sub[val]
}
}
if check_item {
if curval, ok := item[val]; ok {
return RawGetValueString(curval), nil
}
}
return RawGetValueString(val), nil
}
}
// obtain a final float64 value from Field
// use template if one is defined using item to symbols table
// else if the resulting value exists in item symbols table return it
// else return raw value (simple float64 constant)
func (f *Field) GetValueFloat(
item map[string]interface{}) (res float64, err error) {
var str_value any
if f == nil {
return 0, nil
}
if f.tmpl != nil {
defer func() {
// res and err are named out parameters, so if we set value for them in defer
// set the returned values
ok := false
if r := recover(); r != nil {
if err, ok = r.(error); !ok {
err = fmt.Errorf("panic in GetValueFloat template with undefined error")
}
res = 0
}
}()
tmp_res := new(strings.Builder)
err := ((*ttemplate.Template)(f.tmpl)).Execute(tmp_res, &item)
if err != nil {
return 0, err
}
str_value = html.UnescapeString(tmp_res.String())
} else {
val := f.raw
// check if value exists in symbol table
if curval, ok := item[val]; ok {
str_value = curval
} else {
str_value = val
}
}
return RawGetValueFloat(str_value), nil
}
func (f *Field) GetValueObject(
// item map[string]interface{}) ([]any, error) {
item any) (res any, err error) {
res_slice := make([]any, 0)
if f == nil {
return res_slice, nil
}
if f.tmpl != nil {
defer func() {
// res and err are named out parameters, so if we set value for them in defer
// set the returned values
ok := false
if r := recover(); r != nil {
if err, ok = r.(error); !ok {
err = fmt.Errorf("panic in GetValueObject template with undefined error")
}
res = res_slice
}
}()
tmp_res := new(strings.Builder)
err := ((*ttemplate.Template)(f.tmpl)).Execute(tmp_res, &item)
if err != nil {
return res_slice, err
}
var data any
json_obj := tmp_res.String()
if json_obj == "<no value>" || json_obj == "" || json_obj == "null" {
data = ""
} else {
// json_obj = strings.ReplaceAll(json_obj, """, "\"")
json_obj = html.UnescapeString(json_obj)
// json_obj = strings.TrimSuffix(json_obj, "\n")
// fmt.Println(json_obj)
err = json.Unmarshal([]byte(json_obj), &data)
if err != nil {
if _, ok := err.(*json.SyntaxError); ok {
// invalid character 'X' in literal true
return json_obj, nil
}
return res_slice, err
}
}
return data, nil
} else {
datas := &ResultElement{
raw: item,
}
if data, found := datas.GetSlice(f.raw); found {
return data, nil
} else {
return nil, nil
}
}
}
func (f *Field) String() string {
if f == nil {
return ""
}
if f.tmpl != nil {
// f.tmpl.
return f.tmpl.Tree.Root.String()
} else {
return f.raw
}
}
func (f *Field) MarshalText() (text []byte, err error) {
return []byte(f.String()), nil
}
func (f *Field) AddDefaultTemplate(customTemplate *exporterTemplate) error {
if f.tmpl != nil && customTemplate != nil {
if tmpl, err := AddDefaultTemplate(f.tmpl, customTemplate); err != nil {
f.tmpl = tmpl
} else {
return err
}
}
return nil
}
func AddDefaultTemplate(dest_tmpl *exporterTemplate, customTemplate *exporterTemplate) (*exporterTemplate, error) {
if dest_tmpl != nil && customTemplate != nil {
cc_tmpl, err := ((*ttemplate.Template)(customTemplate)).Clone()
if err != nil {
return nil, fmt.Errorf("field template clone for %s is invalid: %s", ((*ttemplate.Template)(customTemplate)).Name(), err)
}
for _, tmpl := range cc_tmpl.Templates() {
name := tmpl.Name()
if name == "default" {
continue
}
_, err = ((*ttemplate.Template)(dest_tmpl)).AddParseTree(tmpl.Name(), tmpl.Tree)
if err != nil {
return nil, fmt.Errorf("field template %s is invalid: %s", tmpl.Name(), err)
}
}
}
return dest_tmpl, nil
}
type ResultElement struct {
raw any
}
func (r *ResultElement) GetSlice(field string) ([]any, bool) {
var myslice []any
var found bool
if r_type, ok := r.raw.(map[string]any); ok {
if myvar, ok := r_type[field]; ok {
switch curval := myvar.(type) {
case []any:
myslice = curval
found = true
case map[string]any:
n_slice := make([]any, 1)
n_slice[0] = myvar
myslice = n_slice
found = true
}
// myvart := reflect.ValueOf(myvar).Kind()
// if myvart == reflect.Slice {
// myslice = myvar.([]interface{})
// // for i, myvar := range myslice {
// // myvart := reflect.ValueOf(myvar).Kind()
// // if myvart == reflect.Map {
// // mymap := make(map[string]interface{})
// // for k, v := range myvar.(map[string]interface{}) {
// // fmt.Printf("k: %s, v %+v\n", k, v)
// // }
// // }
// // }
// } else if myvart == reflect.Map {
// n_slice := make([]interface{}, 1)
// n_slice[0] = myvar
// myslice = n_slice
// }
}
}
return myslice, found
}
// func (r *ResultElement) GetMap(field string) map[string]interface{} {
// var mymap map[string]interface{}
// myvart := reflect.ValueOf(r.raw).Kind()
// if myvart != reflect.Map {
// mymap = nil
// }
// return mymap
// }