-
Notifications
You must be signed in to change notification settings - Fork 15
/
ParamList.go
331 lines (275 loc) · 6.77 KB
/
ParamList.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
package docxplate
import (
"encoding/json"
"log"
"reflect"
"regexp"
"strings"
)
// ParamList ..
type ParamList []*Param
// Get method returns the value associated with the given name
func (params ParamList) Get(key string) any {
for _, p := range params {
if p.Key == key {
return p.Value
}
}
return nil
}
// Len ..
func (params ParamList) Len() int {
return len(params)
}
// AnyToParams - load params from given any struct
// 1) Convert struct to JSON
// 2) Now convert JSON to map[string]any
// 3) Clear params from nil
func AnyToParams(v any) ParamList {
// to JSON output
buf, _ := json.MarshalIndent(v, "", "\t")
return JSONToParams(buf)
}
// JSONToParams - load params from JSON
func JSONToParams(buf []byte) ParamList {
// to map
m := map[string]any{}
if err := json.Unmarshal(buf, &m); err != nil {
log.Printf("JSONToParams: %s", err)
return nil
}
// to filtered/clean map
params := mapToParams(m)
params.Walk(func(p *Param) {
// use Walk func built-in logic to assign keys
})
return params
}
// walk map[string]any and collect valid params
func mapToParams(m map[string]any) ParamList {
var params ParamList
for mKey, mVal := range m {
p := NewParam(mKey)
switch v := mVal.(type) {
case map[string]any:
p.Type = StructParam
p.Params = mapToParams(v)
p.SetValue(v)
case []any:
p.Type = SliceParam
p.Params = sliceToParams(v)
p.SetValue(v)
case *Image:
imgVal, err := processImage(v)
if err != nil {
log.Printf("ProcessImage: %s", err)
continue
}
p.Type = ImageParam
p.SetValue(imgVal)
default:
p.Type = StringParam
p.SetValue(mVal)
}
if mVal == nil && p.Params == nil {
continue
}
params = append(params, p)
}
return params
}
// sliceToParams - slice of unknown - simple slice or complex
func sliceToParams(arr []any) ParamList {
var params ParamList
for i, val := range arr {
// Use index +1 because in template for user not useful see
// 0 as start number. Only programmers will understand
p := NewParam(i + 1)
switch v := val.(type) {
case map[string]any:
p.Type = StructParam
p.Params = mapToParams(v)
default:
p.Type = StringParam
p.SetValue(v)
}
if val == nil && p.Params == nil {
continue
}
params = append(params, p)
}
return params
}
// StructToParams - walk struct and collect valid params
func StructToParams(paramStruct any) ParamList {
var params ParamList
var keys reflect.Type
var vals reflect.Value
var ok bool
if vals, ok = paramStruct.(reflect.Value); !ok {
vals = reflect.ValueOf(paramStruct)
}
keys = vals.Type()
keynum := keys.NumField()
for i := 0; i < keynum; i++ {
key := keys.Field(i)
val := vals.Field(i)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if !val.IsValid() {
continue
}
p := NewParam(key.Name)
switch val.Kind() {
case reflect.Struct:
reflectStructToParams(p, val)
case reflect.Slice:
reflectSliceToParams(p, val)
default:
p.Type = StringParam
p.SetValue(val)
}
if p == nil {
continue
}
params = append(params, p)
}
params.Walk(func(p *Param) {
// use Walk func built-in logic to assign keys
})
return params
}
// reflectStructToParams - map struct of reflect to params include special param type process
func reflectStructToParams(p *Param, val reflect.Value) {
if !val.CanInterface() {
p = nil
return
}
if image, ok := val.Interface().(Image); ok {
imgVal, err := processImage(&image)
if err != nil {
log.Printf("ProcessImage: %s", err)
p = nil
return
}
p.Type = ImageParam
p.SetValue(imgVal)
return
}
p.Type = StructParam
p.Params = StructToParams(val)
}
// reflectSliceToParams - map slice of reflect to params
func reflectSliceToParams(p *Param, val reflect.Value) {
p.Type = SliceParam
for i := 0; i < val.Len(); i++ {
// Use index +1 because in template for user not useful see
// 0 as start number. Only programmers will understand
itemParam := NewParam(i + 1)
itemVal := val.Index(i)
if itemVal.Kind() == reflect.Ptr {
itemVal = itemVal.Elem()
}
if !itemVal.IsValid() {
continue
}
switch itemVal.Kind() {
case reflect.Struct:
reflectStructToParams(itemParam, itemVal)
default:
itemParam.Type = StringParam
itemParam.SetValue(itemVal)
}
if itemParam == nil {
continue
}
p.Params = append(p.Params, itemParam)
}
}
// Parse row content to param list
func rowParams(row []byte) ParamList {
// extract from raw contents
re := regexp.MustCompile(ParamPattern)
matches := re.FindAllSubmatch(row, -1)
if matches == nil || matches[0] == nil {
return nil
}
var params ParamList
for _, match := range matches {
p := NewParam(string(match[2]))
p.RowPlaceholder = string(match[0])
p.Separator = string(match[3])
p.Trigger = NewParamTrigger(match[4])
p.Formatter = NewFormatter(match[4])
params = append(params, p)
}
return params
}
// Walk through params
func (params ParamList) Walk(fn func(*Param)) {
for _, p := range params {
fn(p)
p.Level = 1
p.Walk(fn, p.Level+1)
}
}
// WalkWithEnd - recursively apply fn to each Param in the list
func (params ParamList) WalkWithEnd(fn func(*Param) bool) {
for _, p := range params {
if fn(p) {
continue
}
p.Params.WalkWithEnd(fn)
}
}
// FindAllByKey - returns all Params matching the given key
func (params ParamList) FindAllByKey(key string) ParamList {
keySlice := strings.Split(key, ".")
var ret ParamList
params.findAllByKey(nil, nil, 0, 1, keySlice, &ret)
return ret
}
func (params ParamList) findAllByKey(privParamList, paramList []int, offset, depth int, key []string, paramsIn *ParamList) ([]int, int) {
if depth > len(key) {
return nil, 0
}
currLev := strings.Join(key[:depth], ".")
for i, param := range params {
curr := make([]int, len(paramList))
copy(curr, paramList)
if param.parent != nil && param.parent.Type == SliceParam {
curr = append(curr, i+1)
}
if param.Type == StructParam {
privParamList, offset = param.Params.findAllByKey(privParamList, curr, offset, depth+1, key, paramsIn)
continue
}
if param.CompactKey == currLev || param.AbsoluteKey == currLev {
if param.Type == SliceParam {
privParamList, offset = param.Params.findAllByKey(privParamList, curr, offset, depth, key, paramsIn)
continue
}
if depth == len(key) {
index := 1
if len(curr) > 0 {
index = curr[len(curr)-1]
if len(curr) == len(privParamList) {
for i := range curr[:len(curr)-1] {
if privParamList[i] != curr[i] {
offset += privParamList[len(privParamList)-1]
break
}
}
}
}
param.Index = index + offset
privParamList = curr
*paramsIn = append(*paramsIn, param)
continue
}
privParamList, offset = param.Params.findAllByKey(privParamList, curr, offset, depth+1, key, paramsIn)
}
}
return privParamList, offset
}