forked from SoftwareAG/adabas-go-api
/
definition_restrict.go
697 lines (663 loc) · 23.9 KB
/
definition_restrict.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
* Copyright © 2019-2022 Software AG, Darmstadt, Germany and/or its licensors
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 adatypes
import (
"reflect"
"regexp"
"strconv"
"strings"
)
type fieldQuery struct {
name string
reference bool
fieldRange []*AdaRange
partialRange *AdaRange
}
// field map containing structure and definition
type fieldMap struct {
set map[string]*fieldQuery
strCount map[string]*StructureType
definition *Definition
parentStructure *StructureType
lastStructure *StructureType
stackStructure *Stack
}
var reChanParser1 = make(chan *regexp.Regexp)
const regularFieldParser1 = `(?P<field>[^\[\(\]\)]+)(\[(?P<if>[\dN]+),?(?P<it>[\dN]*)\])?(\((?P<ps>\d+),(?P<pt>\d+)\))?`
var reChanParser2 = make(chan *regexp.Regexp)
const regularFieldParser2 = `(?P<field>[^\[\(\]\),]+(\[[\dN]+\]\[[\dN]*\])?(\[[\dN]+,?[\dN]*\])?(\(\d+,\d+\))?),?`
func init() {
go func() {
for {
reChanParser1 <- regexp.MustCompile(regularFieldParser1)
}
}()
go func() {
for {
reChanParser2 <- regexp.MustCompile(regularFieldParser2)
}
}()
}
// evaluateTopLevelStructure evaluate the structure node which is responsible
// for the given level.
func (fieldMap *fieldMap) evaluateTopLevelStructure(level uint8) {
if Central.IsDebugLevel() {
Central.Log.Debugf("%d check level %d", fieldMap.lastStructure.Level(), level)
}
for fieldMap.lastStructure.Level() >= level {
popElement, _ := fieldMap.stackStructure.Pop()
if popElement == nil {
if Central.IsDebugLevel() {
Central.Log.Debugf("No element in stack")
}
fieldMap.lastStructure = fieldMap.parentStructure
if Central.IsDebugLevel() {
Central.Log.Debugf("Set main structure parent to %v", fieldMap.lastStructure)
}
break
}
fieldMap.lastStructure = popElement.(*StructureType)
if Central.IsDebugLevel() {
Central.Log.Debugf("Set new structure parent to %v", fieldMap.lastStructure)
Central.Log.Debugf("%d check level %d", fieldMap.lastStructure.Level(), level)
}
}
}
// removeStructure set the `remove` flag to define, that the structure is not
// part of the query.
func removeStructure(adaType IAdaType, fieldMap *fieldMap, fq *fieldQuery, ok bool, parentLast bool) {
oldStructure := adaType.(*StructureType)
newStructure := NewStructure()
*newStructure = *oldStructure
if fq != nil && fq.fieldRange != nil {
Central.Log.Debugf("-------<<<< No field Range ")
switch adaType.Type() {
case FieldTypeMultiplefield:
if adaType.HasFlagSet(FlagOptionMUGhost) {
newStructure.peRange = *fq.fieldRange[0]
newStructure.muRange = *fq.fieldRange[1]
if Central.IsDebugLevel() {
Central.Log.Debugf("-------<<<< PE/MU Range %s=[%s,%s]", adaType.Name(),
fq.fieldRange[0].FormatBuffer(), fq.fieldRange[1].FormatBuffer())
}
} else {
if adaType.HasFlagSet(FlagOptionPE) {
newStructure.peRange = *fq.fieldRange[0]
st := newStructure.SubTypes[0].(*AdaType)
st.peRange = *fq.fieldRange[0]
} else {
newStructure.muRange = *fq.fieldRange[0]
st := newStructure.SubTypes[0].(*AdaType)
st.muRange = *fq.fieldRange[0]
}
if Central.IsDebugLevel() {
Central.Log.Debugf("-------<<<< PE Range %s=%s -> %v", adaType.Name(), fq.fieldRange[0].FormatBuffer(),
adaType.HasFlagSet(FlagOptionPE))
}
if len(fq.fieldRange) > 1 {
newStructure.peRange = *fq.fieldRange[0]
st := newStructure.SubTypes[0].(*AdaType)
st.peRange = *fq.fieldRange[0]
newStructure.muRange = *fq.fieldRange[1]
st.muRange = *fq.fieldRange[1]
if Central.IsDebugLevel() {
Central.Log.Debugf("-------<<<< MU Range %s=%s", adaType.Name(), fq.fieldRange[1].FormatBuffer())
}
}
}
case FieldTypePeriodGroup:
newStructure.peRange = *fq.fieldRange[0]
Central.Log.Debugf("-------<<<< PE Range %s=%s", adaType.Name(), fq.fieldRange[0].FormatBuffer())
default:
}
} else {
if Central.IsDebugLevel() {
Central.Log.Debugf("-------<<<< Last Range %s=[%s->%s] last=%s pl=%v -> MU=%s %s", adaType.Name(),
fieldMap.lastStructure.peRange.FormatBuffer(),
newStructure.peRange.FormatBuffer(), fieldMap.lastStructure.Name(), parentLast,
newStructure.muRange.FormatBuffer(), fieldMap.lastStructure.muRange.FormatBuffer())
}
if parentLast {
newStructure.peRange = fieldMap.lastStructure.peRange
//newStructure.muRange = fieldMap.lastStructure.muRange
}
if Central.IsDebugLevel() {
Central.Log.Debugf("-------<<<< Org. Range %s=%s %s", adaType.Name(), newStructure.peRange.FormatBuffer(),
newStructure.muRange.FormatBuffer())
}
}
if Central.IsDebugLevel() {
Central.Log.Debugf("%s current structure parent is %s (%v)", adaType.Name(),
fieldMap.lastStructure.Name(), fieldMap.lastStructure.HasFlagSet(FlagOptionToBeRemoved))
}
newStructure.SubTypes = []IAdaType{}
fieldMap.evaluateTopLevelStructure(newStructure.Level())
fieldMap.lastStructure.SubTypes = append(fieldMap.lastStructure.SubTypes, newStructure)
if Central.IsDebugLevel() {
Central.Log.Debugf("%s -> %s part flag %v", adaType.Name(), fieldMap.lastStructure.Name(), fieldMap.lastStructure.HasFlagSet(FlagOptionPart))
}
if fieldMap.lastStructure.HasFlagSet(FlagOptionPart) {
newStructure.AddFlag(FlagOptionPart)
if Central.IsDebugLevel() {
Central.Log.Debugf("Set %s part flag %v", newStructure.Name(), newStructure.HasFlagSet(FlagOptionPart))
}
}
newStructure.SetParent(fieldMap.lastStructure)
if fieldMap.lastStructure.HasFlagSet(FlagOptionToBeRemoved) {
if !ok {
newStructure.AddFlag(FlagOptionToBeRemoved)
} else {
newStructure.RemoveFlag(FlagOptionToBeRemoved)
}
} else {
newStructure.RemoveFlag(FlagOptionToBeRemoved)
}
switch {
case newStructure.peRange.IsSingleIndex() && newStructure.HasFlagSet(FlagOptionPE):
if Central.IsDebugLevel() {
Central.Log.Debugf("%s/%s(%s): set single index due to PE range",
newStructure.Name(), newStructure.ShortName(), newStructure.Type().name())
}
newStructure.AddFlag(FlagOptionSingleIndex)
case newStructure.muRange.IsSingleIndex() && newStructure.Type() == FieldTypeMultiplefield:
if Central.IsDebugLevel() {
Central.Log.Debugf("%s/%s(%s): set single index due to MU range and Type MU",
newStructure.Name(), newStructure.ShortName(), newStructure.Type().name())
}
newStructure.AddFlag(FlagOptionSingleIndex)
default:
}
if Central.IsDebugLevel() {
Central.Log.Debugf("%s: set flag single index pe=%v(%s) mu=%v(%s)", adaType.Name(),
newStructure.peRange.IsSingleIndex(), newStructure.peRange.FormatBuffer(),
newStructure.muRange.IsSingleIndex(), newStructure.muRange.FormatBuffer())
Central.Log.Debugf("Add structure for active tree %d >%s< remove=%v parent %d >%s<", newStructure.Level(),
adaType.Name(), newStructure.HasFlagSet(FlagOptionToBeRemoved), fieldMap.lastStructure.Level(), fieldMap.lastStructure.Name())
}
fieldMap.lastStructure = newStructure
fieldMap.stackStructure.Push(fieldMap.lastStructure)
fieldMap.strCount[adaType.Name()] = newStructure
if Central.IsDebugLevel() {
Central.Log.Debugf("Create structure %s value=%p to %p parent=%p remove=%v", newStructure.Name(), newStructure,
fieldMap.lastStructure, newStructure.parentType, newStructure.HasFlagSet(FlagOptionToBeRemoved))
}
}
// searchFieldToSetRemoveFlagTrav traverer method search for fields which are not part of the query
// defined by the `fieldMap` structure.
// In addition the range is set and additional single range flags are set
func searchFieldToSetRemoveFlagTrav(adaType IAdaType, parentType IAdaType, level int, x interface{}) error {
fieldMap := x.(*fieldMap)
debug := Central.IsDebugLevel()
if debug {
Central.Log.Debugf("Check remove field on type %s with parent %s(parent remove=%v structure=%v -> %T)", adaType.Name(), parentType.Name(),
parentType.HasFlagSet(FlagOptionToBeRemoved), adaType.IsStructure(), adaType)
}
// Check if field is in request
queryField, isPartOfQuery := fieldMap.set[adaType.Name()]
if isPartOfQuery {
/*if adaType.IsStructure() {
adaType.RemoveFlag(FlagOptionToBeRemoved)
return nil
}*/
delete(fieldMap.set, adaType.Name())
fieldMap.definition.activeFields[adaType.Name()] = adaType
Central.Log.Debugf("Add active field %s because in field map %#v %#v", adaType.Name(), queryField, adaType)
} else {
Central.Log.Debugf("Not active field %s", adaType.Name())
}
// Structure need to be copied each time because of tree to nodes of fields
switch {
case adaType.Type() == FieldTypeRedefinition:
Central.Log.Debugf("Check redefintion %s", adaType.Name())
if isPartOfQuery {
redType := adaType.(*RedefinitionType)
adaType.RemoveFlag(FlagOptionToBeRemoved)
for _, s := range redType.SubTypes {
delete(fieldMap.set, s.Name())
}
fieldMap.lastStructure.SubTypes = append(fieldMap.lastStructure.SubTypes, redType)
}
case adaType.IsStructure():
if adaType.Type() == FieldTypeMultiplefield && !isPartOfQuery && fieldMap.lastStructure.HasFlagSet(FlagOptionToBeRemoved) {
if debug {
Central.Log.Debugf("Skip removing MU field %s", adaType.Name())
}
return nil
}
Central.Log.Debugf("Removing structure %s", adaType.Name())
removeStructure(adaType, fieldMap, queryField, isPartOfQuery, parentType.Name() != "" && fieldMap.lastStructure.Name() == parentType.Name())
default:
if debug {
Central.Log.Debugf("Field %s in map=%v Level=%d < %d %T", adaType.Name(), isPartOfQuery, fieldMap.lastStructure.Level(),
adaType.Level(), adaType)
}
fieldMap.evaluateTopLevelStructure(adaType.Level())
if queryField != nil && len(queryField.fieldRange) > 0 {
Central.Log.Debugf("Field range for %s -> %s(%d) %T",
adaType.Name(), queryField.fieldRange[0].FormatBuffer(),
len(queryField.fieldRange), adaType)
index := 0
t := adaType.(*AdaType)
if adaType.HasFlagSet(FlagOptionPE) {
t.peRange = *queryField.fieldRange[index]
index++
Central.Log.Debugf("PE range type %#v", t.peRange)
if adaType.HasFlagSet(FlagOptionMUGhost) {
pt := parentType.(*StructureType)
pt.peRange = t.peRange
}
}
if len(queryField.fieldRange) > index && adaType.HasFlagSet(FlagOptionMUGhost) {
t.muRange = *queryField.fieldRange[index]
pt := parentType.(*StructureType)
pt.muRange = t.muRange
Central.Log.Debugf("MU range type %#v", t.muRange)
}
}
Central.Log.Debugf("Field range %s peRange=%s muRange=%s",
adaType.Name(), adaType.PeriodicRange().FormatBuffer(), adaType.MultipleRange().FormatBuffer())
// Skip MU field type if parent is not available
if parentType.Type() == FieldTypeMultiplefield && fieldMap.lastStructure.Name() != parentType.Name() {
Central.Log.Debugf("Skip MU field %s", adaType.Name())
return nil
}
// Needed to check if not group is selected in query
// But not if root node
remove := fieldMap.lastStructure.Name() == "" || fieldMap.lastStructure.HasFlagSet(FlagOptionToBeRemoved)
if debug {
Central.Log.Debugf("Parent node %s has %v", fieldMap.lastStructure.Name(), remove)
}
if !isPartOfQuery && remove {
if debug {
Central.Log.Debugf("Skip copy to active field, because field %s is not part of map map=%v remove=%v",
adaType.Name(), isPartOfQuery, remove)
}
var p IAdaType
p = fieldMap.lastStructure
for {
if p.GetParent() == nil || p.GetParent().Name() == "" {
break
}
p = p.GetParent()
}
if p.Name() != "" {
p.(*StructureType).addPart()
}
} else {
if debug {
Central.Log.Debugf("Current parent %d %s -> %d %s part of query=%v remove=%v", fieldMap.lastStructure.Level(), fieldMap.lastStructure.Name(),
adaType.Level(), adaType.Name(), isPartOfQuery, remove)
}
// Dependent on type create copy of field
switch adaType.Type() {
case FieldTypeSuperDesc:
newType := &AdaSuperType{}
oldType := adaType.(*AdaSuperType)
*newType = *oldType
newType.SetParent(fieldMap.lastStructure)
newType.peRange = fieldMap.lastStructure.peRange
newType.muRange = fieldMap.lastStructure.muRange
fieldMap.lastStructure.SubTypes = append(fieldMap.lastStructure.SubTypes, newType)
newType.RemoveFlag(FlagOptionToBeRemoved)
if _, ok := fieldMap.definition.activeFields[adaType.Name()]; !ok {
Central.Log.Debugf("Add active field %s", adaType.Name())
fieldMap.definition.activeFields[adaType.Name()] = adaType
} else {
Central.Log.Debugf("Skip to add active field %s", adaType.Name())
}
case FieldTypeHyperDesc, FieldTypePhonetic, FieldTypeCollation, FieldTypeReferential, FieldTypeRedefinition:
default:
newType := &AdaType{}
oldType := adaType.(*AdaType)
*newType = *oldType
if queryField != nil && queryField.reference {
newType.fieldType = FieldTypeString
newType.length = 0
newType.AddFlag(FlagOptionReference)
}
newType.SetParent(fieldMap.lastStructure)
newType.peRange = fieldMap.lastStructure.peRange
newType.muRange = fieldMap.lastStructure.muRange
if queryField != nil {
newType.partialRange = queryField.partialRange
for i, r := range queryField.fieldRange {
if i == 0 && newType.HasFlagSet(FlagOptionPE) {
newType.peRange = *r
} else {
newType.muRange = *r
}
Central.Log.Debugf("Field range peRange=%#v", r)
if r.IsSingleIndex() {
newType.AddFlag(FlagOptionSingleIndex)
}
}
Central.Log.Debugf("FB range for field=%s peRange=%s muRange=%s", newType.name, newType.peRange.FormatBuffer(), newType.muRange.FormatBuffer())
}
fieldMap.lastStructure.SubTypes = append(fieldMap.lastStructure.SubTypes, newType)
if debug {
Central.Log.Debugf("Add type to %s value=%p count=%d", fieldMap.lastStructure.Name(), fieldMap.lastStructure, fieldMap.lastStructure.NrFields())
Central.Log.Debugf("Add type entry in structure %s", newType.Name())
}
newType.RemoveFlag(FlagOptionToBeRemoved)
if _, ok := fieldMap.definition.activeFields[adaType.Name()]; !ok {
if adaType.PartialRange() != nil {
Central.Log.Debugf("Add active field %s[%d,%d]", adaType.Name(), adaType.PartialRange().from, adaType.PartialRange().to)
}
fieldMap.definition.activeFields[adaType.Name()] = adaType
} else {
Central.Log.Debugf("Skip to add active field %s", adaType.Name())
}
if fieldMap.lastStructure.HasFlagSet(FlagOptionPart) {
newType.AddFlag(FlagOptionPart)
}
}
}
}
return nil
}
// ResetRestrictToFields reset restriction to all field of tree
func (def *Definition) ResetRestrictToFields() {
if Central.IsDebugLevel() {
Central.Log.Debugf("Reset active tree to complete")
}
x := &StructureType{fieldMap: make(map[string]IAdaType)}
def.activeFieldTree = x
def.activeFields = make(map[string]IAdaType)
t := TraverserMethods{EnterFunction: traverseCacheCopy}
_ = def.fileFieldTree.Traverse(t, 0, def)
}
// ShouldRestrictToFields this method restrict the query to a given comma-separated list
// of fields. If the fields is set to '*', then all fields are read.
// A field definition may contain index information. The index information need to be set
// in square brackets. For example AA[1] will provide the first entry of a multiple field
// or all entries in the first occurence of the period group.
// BB[1,2] will provide the first entry of the period group and the second entry of the
// multiple field.
func (def *Definition) ShouldRestrictToFields(fields string) (err error) {
def.ResetRestrictToFields()
if fields == "*" {
return
}
var field []string
if fields != "" {
re := <-reChanParser2
// regexp.MustCompile(regularFieldParser2)
mt := re.FindAllStringSubmatch(fields, -1)
for _, f := range mt {
field = append(field, f[1])
}
}
if Central.IsDebugLevel() {
Central.Log.Debugf("Split field %s into slice to %#v", fields, field)
}
return def.ShouldRestrictToFieldSlice(field)
}
// RemoveSpecialDescriptors Remove special descriptors from query
func (def *Definition) RemoveSpecialDescriptors() (err error) {
newTypes := make([]IAdaType, 0)
for _, s := range def.activeFieldTree.SubTypes {
if !s.IsSpecialDescriptor() || (s.IsSpecialDescriptor() && !s.IsOption(FieldOptionPE)) {
newTypes = append(newTypes, s)
}
}
def.activeFieldTree.SubTypes = newTypes
def.DumpTypes(true, true, "After remove descriptor")
return nil
}
// newFieldMap create a new `fieldMap` instance used to restrict
// query field set.
func (def *Definition) newFieldMap(field []string) (*fieldMap, error) {
// BUG(tkn) Check if fields are valid!!!!
fieldMap := &fieldMap{definition: def}
fieldMap.set = make(map[string]*fieldQuery)
fieldMap.strCount = make(map[string]*StructureType)
fieldMap.stackStructure = NewStack()
fieldMap.parentStructure = NewStructure()
fieldMap.parentStructure.AddFlag(FlagOptionToBeRemoved)
fieldMap.lastStructure = fieldMap.parentStructure
// prepare debug check in loop
debug := Central.IsDebugLevel()
if len(field) != 0 {
for _, f := range field {
if debug {
Central.Log.Debugf("Add to new Field %s to hash field", f)
}
fl := strings.Trim(f, " ")
if fl != "" {
// regular expression parse field
re := <-reChanParser1
mt := re.FindStringSubmatch(fl)
fl = mt[1]
s := mt[3]
if fl != "" {
rf := false
switch {
case strings.ToLower(f) == "#isn" || strings.ToLower(f) == "#isnquantity" || strings.ToLower(f) == "#key":
case f[0] == '#':
// def.IsPeriodGroup(fl)
//fl = f[1:]
var adaType IAdaType
var ok bool
if adaType, ok = def.fileFields[fl[1:]]; !ok {
return nil, NewGenericError(0)
}
lenType := NewType(FieldTypeFieldLength, fl)
switch adaType.Type() {
case FieldTypePeriodGroup, FieldTypeMultiplefield:
lenType.AddFlag(FlagOptionLengthPE)
}
fieldMap.parentStructure.SubTypes = append(fieldMap.parentStructure.SubTypes, lenType)
case f[0] == '@':
fl = f[1:]
rf = true
fallthrough
default:
// if _, ok := def.fileFields[fl]; !ok {
// fmt.Println(fl, "unknown field")
// return nil, NewGenericError(0)
// }
fq := newFieldQuery(fl, rf, s, mt[4], mt[6], mt[7])
fieldMap.set[fl] = fq
}
}
}
}
}
if debug {
Central.Log.Debugf("Initialized field hash map of select fields")
}
return fieldMap, nil
}
// RestrictFieldSlice Restrict the tree to contain only the given nodes
func (def *Definition) RestrictFieldSlice(field []string) (err error) {
err = def.ShouldRestrictToFieldSlice(field)
if err != nil {
return
}
def.fileFieldTree = def.activeFieldTree
def.fileFields = def.activeFields
return nil
}
// ShouldRestrictToFieldSlice this method restrict the query to a given string slice
// of fields. If one field slice entry is set to '*', then all fields are read.
// A field definition may contain index information. The index information need to be set
// in square brackets. For example AA[1] will provide the first entry of a multiple field
// or all entries in the first occurrence of the period group.
// BB[1,2] will provide the first entry of the period group and the second entry of the
// multiple field.
func (def *Definition) ShouldRestrictToFieldSlice(field []string) (err error) {
debug := Central.IsDebugLevel()
if debug {
Central.Log.Debugf("Should restrict fields to %#v", field)
def.DumpTypes(true, false, "Start status before restrict")
}
def.Values = nil
def.activeFields = make(map[string]IAdaType)
fieldMap, ferr := def.newFieldMap(field)
if ferr != nil {
err = ferr
return
}
// Traverse through field tree to reduce tree to fields which
// are part of the query
t := TraverserMethods{EnterFunction: searchFieldToSetRemoveFlagTrav}
err = def.TraverseTypes(t, true, fieldMap)
if err != nil {
return
}
if len(fieldMap.set) > 0 {
if debug {
Central.Log.Debugf("Field map not empty, unknown fields found ... %v", fieldMap.set)
}
for f := range fieldMap.set {
err = NewGenericError(50, f)
if debug {
Central.Log.Debugf("Error restict fieldMap ... %v", err)
def.DumpTypes(true, false, "error restrict 50")
}
return
}
}
if debug {
Central.Log.Debugf("Remove/Cleanup empty structures ...%#v", fieldMap.strCount)
}
for _, strType := range fieldMap.strCount {
removeFromTree(strType)
}
def.activeFieldTree = fieldMap.parentStructure
if debug {
Central.Log.Debugf("Final restricted type tree .........")
def.DumpTypes(true, false, "Not active restricted")
def.DumpTypes(true, true, "final active restricted")
}
return
}
// removeFromTree Search for field to be removed and set remove flag
func removeFromTree(value *StructureType) {
if !value.HasFlagSet(FlagOptionToBeRemoved) {
Central.Log.Debugf("Field %s already removed", value.Name())
return
}
debug := Central.IsDebugLevel()
if debug {
Central.Log.Debugf("Remove empty nodes from value: %s len=%d", value.Name(), value.NrFields())
}
if value.NrFields() == 0 {
if debug {
Central.Log.Debugf("No sub fields, remove value %s value=%p count=%d", value.Name(), value, value.NrFields())
Central.Log.Debugf("Remove value: %s fields=%d", value.Name(), value.NrFields())
}
if value.parentType != nil {
parent := value.parentType.(*StructureType)
parent.RemoveField(&value.CommonType)
value.SetParent(nil)
if parent.NrFields() == 0 {
Central.Log.Debugf("Remove parent: %s cause %d", parent.Name(), parent.NrFields())
removeFromTree(parent)
}
}
} else {
if debug {
Central.Log.Debugf("Value %s value=%p count=%d contains >0 entries:", value.Name(), value, value.NrFields())
for _, t := range value.SubTypes {
Central.Log.Debugf("Contains %s", t.Name())
}
}
}
}
// SetValueData dependent to the struct interface field the corresponding
// reflection struct the value will be set with the struct value.
func SetValueData(s reflect.Value, v IAdaValue) error {
if Central.IsDebugLevel() {
Central.Log.Debugf("%s = %s", v.Type().Name(), s.Type().Name())
}
switch s.Interface().(type) {
case *int, *int8, *int16, *int32, *int64:
vi, err := v.Int64()
if err != nil {
return err
}
s.Elem().SetInt(vi)
case *uint, *uint8, *uint16, *uint32, *uint64:
vui, err := v.UInt64()
if err != nil {
return err
}
s.Elem().SetUint(vui)
case int, int8, int16, int32, int64:
vi, err := v.Int64()
if err != nil {
return err
}
s.SetInt(vi)
case uint, uint8, uint16, uint32, uint64:
vui, err := v.UInt64()
if err != nil {
return err
}
s.SetUint(vui)
case string:
s.SetString(v.String())
case *string:
s.Elem().SetString(v.String())
default:
if Central.IsDebugLevel() {
Central.Log.Errorf("Unknown conversion %s/%s", s.Type().String(), v.Type().Name())
}
return NewGenericError(80, s.Type(), v.Type().Name())
}
return nil
}
// newFieldQuery new field query instance is generated using the given parameter
func newFieldQuery(fl string, rf bool,
s, fRange, pRangeFrom, pRangeTo string) *fieldQuery {
fq := &fieldQuery{name: fl, reference: rf}
if s != "" {
fq.fieldRange = []*AdaRange{NewRangeParser(s)}
if fRange != "" {
fq.fieldRange = append(fq.fieldRange, NewRangeParser(fRange))
}
}
ps := 0
pt := 0
if pRangeFrom != "" {
var err error
ps, err = strconv.Atoi(pRangeFrom)
if err != nil {
return nil
}
pt, err = strconv.Atoi(pRangeTo)
if err != nil {
return nil
}
fq.partialRange = NewRange(ps, pt)
Central.Log.Debugf("Partial field range %d:%d\n", fq.partialRange.from, fq.partialRange.to)
}
if Central.IsDebugLevel() {
r := ""
if fq.fieldRange != nil && len(fq.fieldRange) > 0 {
if fq.fieldRange[0] == nil {
return nil
}
r = fq.fieldRange[0].FormatBuffer()
}
Central.Log.Debugf("Init field range for field %s -> %s reference=%v", fq.name, r, rf)
}
return fq
}