/
structure_type.go
397 lines (360 loc) · 11.1 KB
/
structure_type.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
/*
* 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 (
"fmt"
"strings"
)
// StructureTraverser structure traverser interface
type StructureTraverser interface {
Traverse(t TraverserMethods, level int, x interface{}) (err error)
}
// StructureType creates a new structure type
type StructureType struct {
CommonType
occ int
condition FieldCondition
fieldMap map[string]IAdaType
}
// NewStructure Creates a new object of structured list types
func NewStructure() *StructureType {
if Central.IsDebugLevel() {
Central.Log.Debugf("Create structure list")
}
return &StructureType{
CommonType: CommonType{
flags: uint32(1 << FlagOptionToBeRemoved),
},
condition: NewFieldCondition(),
}
}
// NewStructureEmpty Creates a new object of structured list types
func NewStructureEmpty(fType FieldType, name string, occByteShort int16,
level uint8) *StructureType {
if Central.IsDebugLevel() {
Central.Log.Debugf("Create empty structure list %s with type %d ", name, fType)
}
var pr *AdaRange
var mr *AdaRange
switch fType {
case FieldTypePeriodGroup:
pr = NewRange(1, LastEntry)
mr = NewEmptyRange()
case FieldTypeMultiplefield:
pr = NewEmptyRange()
mr = NewRange(1, LastEntry)
default:
pr = NewEmptyRange()
mr = NewEmptyRange()
}
st := &StructureType{
CommonType: CommonType{
fieldType: fType,
name: name,
flags: uint32(1 << FlagOptionToBeRemoved),
shortName: name,
length: 0,
peRange: *pr,
muRange: *mr,
level: level,
},
occ: int(occByteShort),
condition: NewFieldCondition(),
}
st.adaptSubFields()
if Central.IsDebugLevel() {
Central.Log.Debugf("Got structure list Range [%s,%s]", st.peRange.FormatBuffer(), st.muRange.FormatBuffer())
}
return st
}
// NewStructureList Creates a new object of structured list types
func NewStructureList(fType FieldType, name string, occByteShort int16, subFields []IAdaType) *StructureType {
if Central.IsDebugLevel() {
Central.Log.Debugf("Create new structure list %s types=%d type=%s", name, len(subFields), fType.name())
}
st := &StructureType{
CommonType: CommonType{fieldType: fType,
name: name,
shortName: name,
flags: uint32(1 << FlagOptionToBeRemoved),
level: 1,
length: 0,
SubTypes: subFields,
},
occ: int(occByteShort),
condition: NewFieldCondition(),
}
switch fType {
case FieldTypePeriodGroup:
st.peRange = *NewRange(1, LastEntry)
case FieldTypeMultiplefield:
st.muRange = *NewRange(1, LastEntry)
default:
}
st.adaptSubFields()
// Central.Log.Debugf("Got structure list Range %s %s", st.peRange.FormatBuffer(), st.muRange.FormatBuffer())
return st
}
// NewLongNameStructureList Creates a new object of structured list types
func NewLongNameStructureList(fType FieldType, name string, shortName string, occByteShort int16, subFields []IAdaType) *StructureType {
st := NewStructureList(fType, name, occByteShort, subFields)
st.shortName = shortName
return st
}
// NewStructureCondition Creates a new object of structured list types
func NewStructureCondition(fType FieldType, name string, subFields []IAdaType, condition FieldCondition) *StructureType {
if Central.IsDebugLevel() {
Central.Log.Debugf("Create new structure with condition %s types=%d type=%d", name, len(subFields), fType)
}
for _, t := range subFields {
t.SetLevel(2)
}
return &StructureType{
CommonType: CommonType{fieldType: fType,
name: name,
shortName: name,
flags: uint32(1 << FlagOptionToBeRemoved),
level: 1,
length: 0,
SubTypes: subFields,
},
condition: condition,
}
}
func (adaType *StructureType) adaptSubFields() {
if adaType.Type() == FieldTypePeriodGroup {
if Central.IsDebugLevel() {
Central.Log.Debugf("%s: set PE flag", adaType.Name())
}
adaType.AddFlag(FlagOptionPE)
adaType.occ = OccCapacity
}
if adaType.Type() == FieldTypeMultiplefield {
if Central.IsDebugLevel() {
Central.Log.Debugf("%s: set MU flag", adaType.Name())
Central.Log.Debugf("Adapt sub fields %s is PE field, need atomic FB", adaType.shortName)
}
adaType.AddFlag(FlagOptionAtomicFB)
adaType.occ = OccCapacity
}
for _, s := range adaType.SubTypes {
s.SetParent(adaType)
s.SetRange(&adaType.peRange)
if adaType.Type() == FieldTypePeriodGroup {
s.AddFlag(FlagOptionPE)
}
if adaType.HasFlagSet(FlagOptionPE) {
s.AddFlag(FlagOptionPE)
}
if adaType.Type() == FieldTypeMultiplefield {
Central.Log.Debugf("%s: set MU flag", adaType.Name())
adaType.AddFlag(FlagOptionAtomicFB)
s.AddFlag(FlagOptionMUGhost)
if adaType.HasFlagSet(FlagOptionPE) {
s.AddFlag(FlagOptionSecondCall)
}
}
}
}
// String return the name of the field
func (adaType *StructureType) String() string {
y := strings.Repeat(" ", int(adaType.level))
if adaType.fieldType == FieldTypeMultiplefield {
if len(adaType.SubTypes) == 0 {
return fmt.Sprintf("%s%d %s deleted", y, adaType.level, adaType.shortName)
}
options := adaType.SubTypes[0].Option()
if options == "" {
options = ",MU"
} else {
options = "," + strings.Replace(options, " ", ",", -1)
}
return fmt.Sprintf("%s%d, %s, %d, %s %s; %s", y, adaType.level, adaType.shortName, adaType.SubTypes[0].Length(),
adaType.SubTypes[0].Type().FormatCharacter(), options, adaType.name)
}
options := adaType.Option()
if options != "" {
options = "," + strings.Replace(options, " ", ",", -1)
}
return fmt.Sprintf("%s%d, %s %s ; %s", y, adaType.level, adaType.shortName, options,
adaType.name)
}
// Length returns the length of the field
func (adaType *StructureType) Length() uint32 {
return adaType.length
}
// SetLength set the length of the field
func (adaType *StructureType) SetLength(length uint32) {
adaType.length = length
}
// IsStructure return the structure of the field
func (adaType *StructureType) IsStructure() bool {
return true
}
// NrFields number of fields contained in the structure
func (adaType *StructureType) NrFields() int {
return len(adaType.SubTypes)
}
// func (adaType *StructureType) parseBuffer(helper *BufferHelper, option *BufferOption) {
// Central.Log.Debugf("Parse Structure type offset=%d", helper.offset)
// }
// Traverse Traverse through the definition tree calling a callback method for each node
func (adaType *StructureType) Traverse(t TraverserMethods, level int, x interface{}) (err error) {
// Go through sub types
for _, v := range adaType.SubTypes {
err = t.EnterFunction(v, adaType, level, x)
if err != nil {
return
}
if v.IsStructure() {
err = v.(StructureTraverser).Traverse(t, level+1, x)
if err != nil {
return
}
if t.leaveFunction != nil {
err = t.leaveFunction(v, adaType, level, x)
if err != nil {
return
}
}
}
}
return nil
}
// AddField add a new field type into the structure type
func (adaType *StructureType) AddField(fieldType IAdaType) {
if Central.IsDebugLevel() {
Central.Log.Debugf("Add sub field %s on parent %s", fieldType.Name(), adaType.Name())
}
fieldType.SetLevel(adaType.level + 1)
fieldType.SetParent(adaType)
fieldType.SetRange(&adaType.peRange)
if Central.IsDebugLevel() {
Central.Log.Debugf("Parent of %s is %s ", fieldType.Name(), fieldType.GetParent())
}
if adaType.HasFlagSet(FlagOptionPE) {
if Central.IsDebugLevel() {
Central.Log.Debugf("Add sub field PE of parent %s field %s", adaType.Name(), fieldType.Name())
}
fieldType.AddFlag(FlagOptionPE)
}
adaType.SubTypes = append(adaType.SubTypes, fieldType)
if adaType.fieldMap == nil {
adaType.fieldMap = make(map[string]IAdaType)
}
adaType.fieldMap[fieldType.Name()] = fieldType
switch {
case fieldType.Type() == FieldTypeRedefinition:
// TODO handle redefinition in add field of structure type
case fieldType.IsStructure():
st := fieldType.(*StructureType)
st.fieldMap = adaType.fieldMap
}
}
func travereAdaptPartOption(adaType IAdaType, parentType IAdaType, level int, x interface{}) error {
adaType.AddFlag(FlagOptionPart)
return nil
}
func (adaType *StructureType) addPart() {
adaType.AddFlag(FlagOptionPart)
t := TraverserMethods{EnterFunction: travereAdaptPartOption}
err := adaType.Traverse(t, 0, nil)
if Central.IsDebugLevel() {
Central.Log.Debugf("adPart error %v", err)
}
}
// RemoveField remote field of the structure type
func (adaType *StructureType) RemoveField(fieldType *CommonType) {
if Central.IsDebugLevel() {
Central.Log.Debugf("Remove field %s out of %s nrFields=%d", fieldType.Name(), adaType.Name(), adaType.NrFields())
Central.Log.Debugf("Rearrange, left=%d", adaType.NrFields())
}
var newTypes []IAdaType
for _, t := range adaType.SubTypes {
if t.Name() != fieldType.Name() {
newTypes = append(newTypes, t)
}
}
adaType.SubTypes = newTypes
}
// SetRange set Adabas range
func (adaType *StructureType) SetRange(r *AdaRange) {
adaType.peRange = *r
}
// Option return structure option as a string
func (adaType *StructureType) Option() string {
switch adaType.fieldType {
case FieldTypeMultiplefield:
return "MU"
case FieldTypePeriodGroup:
return "PE"
default:
}
return ""
}
// SetFractional set fractional part
func (adaType *StructureType) SetFractional(x uint32) {
}
// Fractional get fractional part
func (adaType *StructureType) Fractional() uint32 {
return 0
}
// // SetCharset set fractional part
// func (adaType *StructureType) SetCharset(x string) {
// }
// SetFormatType set format type
func (adaType *StructureType) SetFormatType(x rune) {
}
// FormatType get format type
func (adaType *StructureType) FormatType() rune {
return adaType.FormatTypeCharacter
}
// SetFormatLength set format length
func (adaType *StructureType) SetFormatLength(x uint32) {
}
// Value return type specific value structure object
func (adaType *StructureType) Value() (adaValue IAdaValue, err error) {
if Central.IsDebugLevel() {
Central.Log.Debugf("Create structure type of %v", adaType.fieldType.name())
}
switch adaType.fieldType {
case FieldTypeStructure, FieldTypeGroup, FieldTypePeriodGroup, FieldTypeMultiplefield:
Central.Log.Debugf("Return Structure value")
adaValue = newStructure(adaType)
return
case FieldTypeRedefinition:
adaValue = newRedefinition(adaType)
return
}
if Central.IsDebugLevel() {
Central.Log.Debugf("Return nil structure", adaType.String())
}
err = NewGenericError(104, adaType.String())
return
}
// ReplaceType replace type in the substructure
func (adaType *StructureType) ReplaceType(orgType, newType IAdaType) error {
for i := 0; i < len(adaType.SubTypes); i++ {
if adaType.SubTypes[i] == orgType {
adaType.SubTypes[i] = newType
return nil
}
}
return NewGenericError(93, orgType.Name(), adaType.Name())
}