-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.go
295 lines (252 loc) · 8.95 KB
/
model.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
/*
Copyright IBM Corporation 2021.
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 parser
import (
"strconv"
"strings"
)
// Entry ... Output model
type Entry struct {
isKeyless bool
fields map[string][]interface{}
}
// ParsedValidatedAnnotation ...
type ParsedValidatedAnnotation struct {
Entries []Entry
label string
}
// NewEntry ... Constructor Entry
func NewEntry() Entry {
var entry Entry
entry.fields = make(map[string][]interface{})
return entry
}
// NewKeylessEntry ... Constructor For KeylessEntry
func NewKeylessEntry(keylessValue string) Entry {
var entry Entry
entry.fields = make(map[string][]interface{})
entry.isKeyless = true
entry.Put("KEYLESS", keylessValue)
return entry
}
// GetAsBool support for boolean values for example : proxy-buffering : True or false
func (entry Entry) GetAsBool(key string) (boolvalue bool, exists bool) {
values, keyExists := entry.GetAsBools(key)
return values[0], keyExists
}
// GetAsBools support for multiple boolean values
func (entry Entry) GetAsBools(key string) (boolvalues []bool, exists bool) {
values, keyExists := entry.fields[key]
boolValues := []bool{}
for _, interfaceValue := range values {
boolValues = append(boolValues, interfaceValue.(bool))
}
return boolValues, keyExists
}
// GetEntriesWithFieldValueInt gets the entries which containing integer values
func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithFieldValueInt(fieldName string, fieldValue int) (selectedEntries []Entry, exists bool) {
var entriesSelected []Entry
var fieldFound bool
for _, entry := range parsedValidatedAnnotation.Entries {
values, keyExists := entry.fields[fieldName]
if keyExists {
for _, interfaceValue := range values {
var intFieldValue = interfaceValue.(int)
if fieldValue == intFieldValue {
entriesSelected = append(entriesSelected, entry)
fieldFound = true
break
}
}
}
}
return entriesSelected, fieldFound
}
//GetEntriesWithFieldValueUnitString support only string values having units for example : timeout , rate
func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithFieldValueUnitString(fieldName string, fieldValue string) (selectedEntries []Entry, exists bool) {
var entriesSelected []Entry
var fieldFound bool
for _, entry := range parsedValidatedAnnotation.Entries {
values, keyExists := entry.GetAsValueUnitString(fieldName)
if keyExists {
if strings.TrimSpace(fieldValue) == values {
entriesSelected = append(entriesSelected, entry)
fieldFound = true
}
}
}
return entriesSelected, fieldFound
}
// GetEntriesWithFieldValueString support only string values for example : serviceName
func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithFieldValueString(fieldName string, fieldValue string) (selectedEntries []Entry, exists bool) {
var entriesSelected []Entry
var fieldFound bool
for _, entry := range parsedValidatedAnnotation.Entries {
values, keyExists := entry.fields[fieldName]
if keyExists {
for _, interfaceValue := range values {
var strFieldValue = (interfaceValue.(string))
if strings.TrimSpace(fieldValue) == strFieldValue {
entriesSelected = append(entriesSelected, entry)
fieldFound = true
break
}
}
}
}
return entriesSelected, fieldFound
}
// GetEntriesWithSingleField gets entries with single string field for example : serviceName
func (parsedValidatedAnnotation ParsedValidatedAnnotation) GetEntriesWithSingleField(fieldName string) (selectedEntries []Entry, exists bool) {
var entriesSelected []Entry
var entryFound bool
for _, entry := range parsedValidatedAnnotation.Entries {
_, keyExists := entry.fields[fieldName]
if keyExists && len(entry.fields) == 1 {
entriesSelected = append(entriesSelected, entry)
entryFound = true
break
}
}
return entriesSelected, entryFound
}
// Keyless checks if the entry is keyless
func (entry Entry) Keyless() (isKeyless bool) {
return entry.isKeyless
}
// GetKeylessValue gets the keyless value of a given entry
func (entry Entry) GetKeylessValue() (values []interface{}, exists bool) {
value, keyExists := entry.fields["KEYLESS"]
return value, keyExists
}
// GetKeylessValueAsString Support for getting string values for a given fieldType for example : "cafe.example.com"
func (entry Entry) GetKeylessValueAsString(fieldType string) (strvalue string, exists bool) {
parseMethodDef := fieldTypeToParserMap[fieldType]
if parseMethodDef == nil {
return "", false
}
values, valueExists := entry.GetKeylessValue()
if valueExists {
var valueStr string
for _, interfaceValue := range values {
valueStr = interfaceValue.(string)
break
}
value, err := parseMethodDef(valueStr)
if err == nil {
return value.(string), true
}
}
return "", false
}
// GetKeylessValueAsValueUnitString Support for getting string unit values for a given fieldType for example : 10ms , 10r/s
func (entry Entry) GetKeylessValueAsValueUnitString(fieldType string) (strvalue string, exists bool) {
parseMethodDef := fieldTypeToParserMap[fieldType]
if parseMethodDef == nil {
return "", false
}
values, valueExists := entry.GetKeylessValue()
if valueExists {
var valueStr string
for _, interfaceValue := range values {
valueStr = interfaceValue.(string)
break
}
value, err := parseMethodDef(valueStr)
if err == nil {
strArray := value.([2]string)
return strArray[0] + strArray[1], true
}
}
return "", false
}
// GetKeylessValueAsBool Support for getting boolean value for keyless value
func (entry Entry) GetKeylessValueAsBool(fieldType string) (strvalue bool, exists bool) {
values, valueExists := entry.GetKeylessValue()
if valueExists {
var valueStr string
for _, interfaceValue := range values {
valueStr = interfaceValue.(string)
break
}
boolValue, err := strconv.ParseBool(valueStr)
if err == nil {
return boolValue, true
}
}
return false, false
}
// Exists check if the key exists for a given entry
func (entry Entry) Exists(key string) (isPresent bool) {
_, keyExists := entry.fields[key]
return keyExists
}
// Put ...
func (entry Entry) Put(key string, value interface{}) {
entry.fields[key] = append(entry.fields[key], value)
}
// Get ...
func (entry Entry) Get(key string) (values []interface{}, exists bool) {
values, keyExists := entry.fields[key]
return values, keyExists
}
// GetAsString Get the values of single string value for given fieldName for a given entry
func (entry Entry) GetAsString(key string) (strvalue string, exists bool) {
values, keyExists := entry.GetAsStrings(key)
return values[0], keyExists
}
// GetAsStrings Get the values of mutiple string vlaues for a given filedname for a given entry
func (entry Entry) GetAsStrings(key string) (strvalues []string, exists bool) {
values, keyExists := entry.fields[key]
var stringValues []string
for _, interfaceValue := range values {
stringValues = append(stringValues, interfaceValue.(string))
}
return stringValues, keyExists
}
// GetAsInt Get the values of single Integer value for given fieldName for a given entry
func (entry Entry) GetAsInt(key string) (intvalue int, exists bool) {
values, keyExists := entry.GetAsInts(key)
return values[0], keyExists
}
// GetAsInts Get the values of multiple Integer value for given fieldName for a given entry
func (entry Entry) GetAsInts(key string) (intvalues []int, exists bool) {
values, keyExists := entry.fields[key]
var intValues []int
for _, interfaceValue := range values {
intValues = append(intValues, interfaceValue.(int))
}
return intValues, keyExists
}
// GetAsValueUnitString Get the values of single string value having unit for given fieldName for a given entry ex : timeout,rate
func (entry Entry) GetAsValueUnitString(key string) (valueUnit string, exists bool) {
values, exists := entry.GetAsValueUnitStringArray(key)
if exists {
return values[0] + values[1], exists
}
return "", false
}
// GetAsValueUnitStringArray Get the first array values of the multiple string having units of key
func (entry Entry) GetAsValueUnitStringArray(key string) (strvalues [2]string, exists bool) {
values, keyExists := entry.GetAsValueUnitStringArrays(key)
return values[0], keyExists
}
// GetAsValueUnitStringArrays Get all the values of the strings values for the given field name
func (entry Entry) GetAsValueUnitStringArrays(key string) (strvalues [][2]string, exists bool) {
values, keyExists := entry.fields[key]
var strArrvalues [][2]string
for _, interfaceValue := range values {
strArrvalues = append(strArrvalues, interfaceValue.([2]string))
}
return strArrvalues, keyExists
}