forked from SoftwareAG/adabas-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
573 lines (526 loc) · 15 KB
/
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
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
/*
* Copyright © 2018-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 (
"bytes"
"encoding/binary"
"fmt"
"math"
"strings"
)
// OccByte Occurrence identifier indicating that the occurrence is defined as byte
const OccByte = -12
// OccUInt2 Occurrence identifier indicating that the occurrence is defined as uint32
const OccUInt2 = -11
// OccNone Occurrence identifier indicating that the occurrence is not used
const OccNone = -10
// OccSingle Occurrence identifier indicating that the occurrence single
const OccSingle = -9
// OccCapacity Occurrence identifier indicating that the occurrence capactity of 2 or PE fields
const OccCapacity = -8
// NoReferenceField field out of range of given field possibilities
const NoReferenceField = math.MaxInt32
// IAdaType data type interface defined for all types
type IAdaType interface {
Type() FieldType
String() string
Name() string
ShortName() string
SetName(string)
Value() (IAdaValue, error)
Length() uint32
SetLength(uint32)
SetRange(*AdaRange)
PartialRange() *AdaRange
SetPartialRange(*AdaRange)
PeriodicRange() *AdaRange
MultipleRange() *AdaRange
IsStructure() bool
Level() uint8
SetLevel(uint8)
Option() string
SetParent(IAdaType)
GetParent() IAdaType
HasFlagSet(FlagOption) bool
AddFlag(FlagOption)
RemoveFlag(FlagOption)
IsOption(FieldOption) bool
AddOption(FieldOption)
IsSpecialDescriptor() bool
SetEndian(binary.ByteOrder)
Endian() binary.ByteOrder
SetFractional(uint32)
SetCharset(string)
SetFormatType(rune)
FormatType() rune
SetFormatLength(uint32)
Fractional() uint32
Convert() ConvertUnicode
}
// AdaType data type structure for field types, no structures
type AdaType struct {
CommonType
SysField byte
EditMask byte
SubOption byte
FractValue uint32
}
// FieldCondition field condition reference using for parser length management
type FieldCondition struct {
lengthFieldIndex int
refField int
conditionMatrix map[byte][]byte
}
//func NewFieldCondition(index int, ref int, condition map[byte][]byte) FieldCondition {
// NewFieldCondition creates a new field condition
func NewFieldCondition(param ...interface{}) FieldCondition {
lengthFieldIndex := -1
refField := NoReferenceField
var conditionMatrix map[byte][]byte
if len(param) > 0 {
lengthFieldIndex = param[0].(int)
refField = param[1].(int)
conditionMatrix = param[2].(map[byte][]byte)
}
if Central.IsDebugLevel() {
Central.Log.Debugf("New field condition lengthFieldIndex=%d refField=%d", lengthFieldIndex, refField)
}
return FieldCondition{
lengthFieldIndex: lengthFieldIndex,
refField: refField,
conditionMatrix: conditionMatrix,
}
// return FieldCondition{
// lengthFieldIndex: index,
// refField: ref,
// conditionMatrix: condition,
// }
}
// NewType Define new type with length equal 1
func NewType(param ...interface{}) *AdaType {
fType := param[0].(FieldType)
name := param[1].(string)
longName := name
length := uint32(1)
if len(param) > 2 {
for i := 2; i < len(param); i++ {
switch p := param[i].(type) {
case int:
length = uint32(p)
case uint32:
length = p
case string:
longName = p
default:
if Central.IsDebugLevel() {
Central.Log.Debugf("Unknown parameter type %T", param[2])
}
//panic("Error for type parameter")
return nil
}
}
}
flags := FlagOptionToBeRemoved.Bit()
if len(param) > 3 {
flags = flags | FlagOptionLengthNotIncluded.Bit()
}
switch fType {
case FieldTypeUByte, FieldTypeByte:
case FieldTypeUInt2, FieldTypeInt2:
length = 2
case FieldTypeUInt4, FieldTypeInt4:
length = 4
case FieldTypeUInt8, FieldTypeInt8:
length = 8
}
return &AdaType{CommonType: CommonType{
fieldType: fType,
level: 1,
name: longName,
flags: flags,
shortName: name,
peRange: *NewEmptyRange(),
muRange: *NewEmptyRange(),
length: length,
}}
}
// NewLongNameType Define new type with length equal 1
func NewLongNameType(fType FieldType, name string, shortName string) *AdaType {
return NewType(fType, shortName, name)
}
// NewTypeWithLength Definen new type
func NewTypeWithLength(fType FieldType, name string, length uint32) *AdaType {
return NewType(fType, name, length)
}
// NewTypeWithFlag Define new type with flag
func NewTypeWithFlag(fType FieldType, name string, flag FlagOption) *AdaType {
t := NewType(fType, name, 0)
t.AddFlag(flag)
return t
}
// NewLongNameTypeWithLength Define new type
func NewLongNameTypeWithLength(fType FieldType, name string, shortName string, length uint32) *AdaType {
return NewType(fType, shortName, name, length)
}
// String return the name of the field
func (adaType *AdaType) String() string {
var b strings.Builder
b.Grow(60)
b.WriteString(strings.Repeat(" ", int(adaType.level)))
options := adaType.Option()
if options != "" {
options = "," + strings.Replace(options, " ", ",", -1)
}
fmt.Fprintf(&b, "%d, %s, %d, %s %s ; %s", adaType.level, adaType.shortName, adaType.length,
adaType.fieldType.FormatCharacter(), options, adaType.name)
return b.String()
}
// Length return the length of the field
func (adaType *AdaType) Length() uint32 {
return adaType.length
}
// SetLength set the length of the field
func (adaType *AdaType) SetLength(length uint32) {
if adaType.length == length {
return
}
if (adaType.fieldType != FieldTypeFloat && adaType.fieldType != FieldTypeDouble) || length > 0 {
if adaType.HasFlagSet(FlagOptionPE) {
// Period length change, CANNNOT use collected FB entry!!!!
if Central.IsDebugLevel() {
Central.Log.Debugf("Length not default and field %s is PE field, need atomic FB", adaType.shortName)
}
adaType.AddFlag(FlagOptionAtomicFB)
}
adaType.length = length
} else {
if Central.IsDebugLevel() {
Central.Log.Debugf("Skip float or double to %d", adaType.length)
}
}
}
// IsStructure return if it is an structure
func (adaType *AdaType) IsStructure() bool {
return false
}
// SetFractional set fractional part
func (adaType *AdaType) SetFractional(x uint32) {
adaType.FractValue = x
}
// Fractional get fractional part
func (adaType *AdaType) Fractional() uint32 {
return adaType.FractValue
}
// // SetCharset set fractional part
// func (adaType *AdaType) SetCharset(x string) {
// adaType.Charset = x
// }
// SetFormatType set format type
func (adaType *AdaType) SetFormatType(x rune) {
adaType.FormatTypeCharacter = x
}
// FormatType get format type
func (adaType *AdaType) FormatType() rune {
return adaType.FormatTypeCharacter
}
// SetFormatLength set format length
func (adaType *AdaType) SetFormatLength(x uint32) {
adaType.FormatLength = x
}
// Value return type specific value structure object
func (adaType *AdaType) Value() (adaValue IAdaValue, err error) {
Central.Log.Debugf("Create field type of %v", adaType.fieldType)
switch adaType.fieldType {
case FieldTypeByte:
adaValue = newByteValue(adaType)
return
case FieldTypeByteArray:
if adaType.length > 126 {
return nil, NewGenericError(111, adaType.length, "binary", adaType.Name())
}
adaValue = newByteArrayValue(adaType)
return
case FieldTypeFieldLength:
adaValue = newLengthValue(adaType)
return
case FieldTypeLength, FieldTypeUByte, FieldTypeCharacter:
adaValue = newUByteValue(adaType)
return
case FieldTypeString:
if adaType.length > 253 {
return nil, NewGenericError(111, adaType.length, "alpha", adaType.Name())
}
adaValue = newStringValue(adaType)
return
case FieldTypeLAString:
if adaType.length > 65533 {
return nil, NewGenericError(111, adaType.length, "large alpha", adaType.Name())
}
adaValue = newStringValue(adaType)
return
case FieldTypeLBString:
if adaType.length > 2147483543 {
return nil, NewGenericError(111, adaType.length, "large object alpha", adaType.Name())
}
adaValue = newStringValue(adaType)
return
case FieldTypeUnicode:
if adaType.length > 253 {
return nil, NewGenericError(111, adaType.length, "unicode", adaType.Name())
}
adaValue = newUnicodeValue(adaType)
return
case FieldTypeLAUnicode:
if adaType.length > 16381 {
return nil, NewGenericError(111, adaType.length, "large unicode", adaType.Name())
}
adaValue = newUnicodeValue(adaType)
return
case FieldTypeLBUnicode:
if adaType.length > 16381 {
return nil, NewGenericError(111, adaType.length, "large object unicode", adaType.Name())
}
adaValue = newUnicodeValue(adaType)
return
case FieldTypeUInt2:
adaValue = newUInt2Value(adaType)
return
case FieldTypeInt2:
adaValue = newInt2Value(adaType)
return
case FieldTypeUInt4:
adaValue = newUInt4Value(adaType)
return
case FieldTypeInt4:
adaValue = newInt4Value(adaType)
return
case FieldTypeUInt8:
adaValue = newUInt8Value(adaType)
return
case FieldTypeInt8:
adaValue = newInt8Value(adaType)
return
case FieldTypeUnpacked:
if adaType.length > 29 {
return nil, NewGenericError(111, adaType.length, "unpacked", adaType.Name())
}
adaValue = newUnpackedValue(adaType)
case FieldTypePacked:
if adaType.length > 15 {
return nil, NewGenericError(111, adaType.length, "packed", adaType.Name())
}
adaValue = newPackedValue(adaType)
case FieldTypeFloat:
switch adaType.length {
case (4):
adaValue = newFloatValue(adaType)
case (8):
adaValue = newDoubleValue(adaType)
default:
err = NewGenericError(110, adaType.length, adaType.Name())
}
case FieldTypeFiller:
adaValue = newFillerValue(adaType)
case FieldTypePhonetic:
adaValue = newPhoneticValue(adaType)
case FieldTypeSuperDesc:
adaValue = newSuperDescriptorValue(adaType)
// Should not come here for structure types
// case FieldTypeStructure,FieldTypeGroup:
// Central.Log.Debugf("Return Structure value")
// return 0, newStructure(adaType)
case FieldTypeCollation:
adaValue = newCollationValue(adaType)
case FieldTypeReferential:
adaValue = newReferentialValue(adaType)
default:
Central.Log.Debugf("Return error type value evaluation for %v %s", adaType.fieldType, adaType.String())
return nil, NewGenericError(102, adaType.fieldType.name(), adaType.Name())
}
return
}
// Option output all options of a field in an string
func (adaType *AdaType) Option() string {
var buffer bytes.Buffer
for i := 0; i < len(fieldOptions); i++ {
if (adaType.options & (1 << uint(i))) > 0 {
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString(fieldOptions[i])
}
}
switch {
case adaType.Type() == FieldTypeLBString:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("LB")
case adaType.Type() == FieldTypeLAString:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("LA")
default:
}
if adaType.fieldType == FieldTypeMultiplefield {
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("MU")
}
switch adaType.SysField {
case 1:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("SY=TIME")
case 2:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("SY=SESSIONID")
case 3:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("SY=OPUSER")
default:
}
switch adaType.EditMask {
case 1:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(DATE)")
case 2:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(TIME)")
case 3:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(DATETIME)")
case 4:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(TIMESTAMP)")
case 5:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(NATDATE)")
case 6:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(NATTIME)")
case 7:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(UNIXTIME)")
case 8:
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString("DT=E(XTIMESTAMP)")
default:
}
return buffer.String()
}
// RedefinitionType creates a new redefinition type
type RedefinitionType struct {
CommonType
MainType IAdaType
SubTypes []IAdaType
// fieldMap map[string]IAdaType
}
// NewRedefinitionType Creates a new object of redefootopm types
func NewRedefinitionType(mainType IAdaType) *RedefinitionType {
if mainType == nil {
//panic("Main type of redefinition nil")
return nil
}
return &RedefinitionType{MainType: mainType,
CommonType: CommonType{level: mainType.Level(), name: mainType.Name(),
FormatTypeCharacter: mainType.FormatType(), shortName: mainType.ShortName(),
length: mainType.Length(), fieldType: FieldTypeRedefinition}}
}
// AddSubType add redefinition sub types used for the field
func (adaType *RedefinitionType) AddSubType(subType IAdaType) {
subType.SetLevel(adaType.MainType.Level() + 1)
adaType.SubTypes = append(adaType.SubTypes, subType)
}
// Value return type specific value structure object
func (adaType *RedefinitionType) Value() (adaValue IAdaValue, err error) {
return newRedefinition(adaType), nil
}
// SetFormatType set format type
func (adaType *RedefinitionType) SetFormatType(x rune) {
}
// FormatType get format type
func (adaType *RedefinitionType) FormatType() rune {
Central.Log.Debugf("Redefinition format type %c", adaType.MainType.FormatType())
return adaType.MainType.FormatType()
}
// SetFractional set fractional part
func (adaType *RedefinitionType) SetFractional(x uint32) {
}
// Fractional get fractional part
func (adaType *RedefinitionType) Fractional() uint32 {
return 0
}
// // SetCharset set fractional part
// func (adaType *RedefinitionType) SetCharset(x string) {
// }
// SetFormatLength set format length
func (adaType *RedefinitionType) SetFormatLength(x uint32) {
}
// Length return the length of the field
func (adaType *RedefinitionType) Length() uint32 {
return adaType.MainType.Length()
}
// IsStructure return if it is an structure
func (adaType *RedefinitionType) IsStructure() bool {
return true
}
// Option output all options of a field in an string
func (adaType *RedefinitionType) Option() string {
return ""
}
// SetLength set the length of the field
func (adaType *RedefinitionType) SetLength(length uint32) {
adaType.MainType.SetLength(length)
adaType.CommonType.length = length
}
// String return the name of the field
func (adaType *RedefinitionType) String() string {
if adaType.MainType == nil {
return "Main type in redefinition nil"
}
return adaType.MainType.String()
}
// Traverse Traverse through the definition tree calling a callback method for each node
func (adaType *RedefinitionType) Traverse(t TraverserMethods, level int, x interface{}) (err error) {
return nil
}