forked from SoftwareAG/adabas-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_interface.go
217 lines (203 loc) · 5.85 KB
/
dynamic_interface.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
/*
* 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 (
"bytes"
"reflect"
"strings"
)
// DynamicInterface dynamic interface
type DynamicInterface struct {
DataType reflect.Type
FieldNames map[string][]string
}
// generateFieldNames examine all 'adabas'-tags in the given structure and build up
// field names map pointing to corresponding path with names of structures
func generateFieldNames(ri reflect.Type, f map[string][]string, fields []string) {
if Central.IsDebugLevel() {
Central.Log.Debugf("Generate field names for %s %s", ri.Name(), ri.Kind())
}
if ri.Kind() != reflect.Struct {
return
}
for fi := 0; fi < ri.NumField(); fi++ {
ct := ri.Field(fi)
fieldName := ct.Name
adabasFieldName := fieldName
tag := ct.Tag.Get("adabas")
if Central.IsDebugLevel() {
Central.Log.Debugf("fieldName=%s/%s -> tag=%s", adabasFieldName, fieldName, tag)
}
if tag != "" {
s := strings.Split(tag, ":")
if len(s) > 2 {
adabasFieldName = s[2]
} else {
if s[0] != "" {
adabasFieldName = s[0]
if strings.ToLower(adabasFieldName) == "#isn" {
adabasFieldName = "#isn"
}
}
}
if len(s) > 1 {
switch s[1] {
case "key":
f["#key"] = []string{adabasFieldName}
case "isn":
f["#isn"] = []string{adabasFieldName}
// No sub value and not relevant Adabas field, skip rest
continue
case "ignore":
continue
case "":
// this is if the inmap repository-less map is used
default:
Central.Log.Errorf("Unknown control tag >%s<", s[1])
continue
}
}
}
// copy of subfields
subFields := make([]string, len(fields))
copy(subFields, fields)
subFields = append(subFields, fieldName)
f[adabasFieldName] = subFields
if Central.IsDebugLevel() {
Central.Log.Debugf("Set field names to %s -> %v", adabasFieldName, subFields)
Central.Log.Debugf("Type struct field = %v", ct.Type.Kind())
}
// Handle special case for pointer and slices
switch ct.Type.Kind() {
case reflect.Ptr:
if Central.IsDebugLevel() {
Central.Log.Debugf("Pointer found %v %v", ct.Type.Name(), ct.Type.Elem().Name())
}
generateFieldNames(ct.Type.Elem(), f, subFields)
case reflect.Slice:
if Central.IsDebugLevel() {
Central.Log.Debugf("Slice found %v %v", ct.Type.Name(), ct.Type.Elem().Name())
}
sliceT := ct.Type.Elem()
if sliceT.Kind() == reflect.Ptr {
sliceT = sliceT.Elem()
}
generateFieldNames(sliceT, f, subFields)
}
}
}
// CreateDynamicInterface constructor create dynamic interface
func CreateDynamicInterface(i interface{}) *DynamicInterface {
ri := reflect.TypeOf(i)
if ri.Kind() == reflect.Ptr {
ri = ri.Elem()
}
dynamic := &DynamicInterface{DataType: ri, FieldNames: make(map[string][]string)}
if Central.IsDebugLevel() {
Central.Log.Debugf("Dynamic interface %s", ri.Name())
Central.Log.Debugf("Dynamic interface %v nrFields=%d", ri, ri.NumField())
}
generateFieldNames(ri, dynamic.FieldNames, make([]string, 0))
return dynamic
}
// CreateQueryFields create query field list of dynamic interface given
func (dynamic *DynamicInterface) CreateQueryFields() string {
var buffer bytes.Buffer
for fieldName := range dynamic.FieldNames {
if buffer.Len() > 0 {
buffer.WriteRune(',')
}
buffer.WriteString(fieldName)
}
if Central.IsDebugLevel() {
Central.Log.Debugf("Create query fields: %s", buffer.String())
}
return buffer.String()
}
// ExamineIsnField set the interface Isn-tagged field with value for ISN
func (dynamic *DynamicInterface) ExamineIsnField(value reflect.Value, isn Isn) error {
if Central.IsDebugLevel() {
Central.Log.Debugf("Examine ISN field: %d", isn)
}
v := value
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if f, ok := dynamic.FieldNames["#isn"]; ok {
isnField := v.FieldByName(f[0])
if !isnField.IsValid() || isnField.Kind() != reflect.Uint64 {
return NewGenericError(113)
}
if Central.IsDebugLevel() {
Central.Log.Debugf("Found isn %d", isn)
}
isnField.SetUint(uint64(isn))
} else {
if Central.IsDebugLevel() {
Central.Log.Debugf("No ISN field found")
}
}
return nil
}
// ExtractIsnField extract out of interface Isn-tagged field with value for ISN
func (dynamic *DynamicInterface) ExtractIsnField(value reflect.Value) Isn {
v := value
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if k, ok := dynamic.FieldNames["#isn"]; ok {
if Central.IsDebugLevel() {
Central.Log.Debugf("ISNfield: %v", k)
}
keyField := v.FieldByName(k[0])
return Isn(keyField.Uint())
}
return 0
}
// PutIsnField put ISN field back into structure
func (dynamic *DynamicInterface) PutIsnField(value reflect.Value, isn Isn) {
v := value
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if Central.IsDebugLevel() {
Central.Log.Debugf("Check FieldNames %v", dynamic.FieldNames)
}
if k, ok := dynamic.FieldNames["#isn"]; ok {
if Central.IsDebugLevel() {
Central.Log.Debugf("Set ISN field: %s", k)
}
for _, kisn := range k {
iv := v.FieldByName(kisn)
if iv.Kind() == reflect.Ptr {
iv = iv.Elem()
}
if iv.CanAddr() {
if Central.IsDebugLevel() {
Central.Log.Debugf("Set ISN for %s to %d", kisn, isn)
}
iv.SetUint(uint64(isn))
} else {
if Central.IsDebugLevel() {
Central.Log.Debugf("Cannot address ISN: %s", kisn)
}
}
}
}
}