-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
185 lines (145 loc) · 2.93 KB
/
util.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
package main
import (
"fmt"
"os"
"strings"
)
func checkHasSequancalWildcard(base string) bool {
if strings.Contains(base, "*.*") {
return true
}
return false
}
func checkArraySelector(base string) bool {
if strings.Contains(base, "[") {
return true
}
return false
}
func checkHasWildcard(base string) bool {
if strings.Contains(base, "*") {
return true
}
return false
}
func isWildCardMatches(base, value string) bool {
// This means change all values and it's causes problems
if base == "*" {
return false
}
if !checkHasWildcard(base) {
return false
}
if checkHasSequancalWildcard(base) {
fmt.Printf("Cannot use consecutive wildcard in absolute config key: %s\n", base)
os.Exit(1)
}
if checkArraySelector(base) {
fmt.Printf("Cannot use array selector with wildcards in absolute config key: %s\n", base)
os.Exit(1)
}
baseArr := strings.Split(base, ".")
valueArr := strings.Split(value, ".")
// If valueArr contains wildcard, it's not ok.
for i := range valueArr {
if valueArr[i] == "*" {
return false
}
}
isWildCard := false
for i := range baseArr {
if baseArr[i] == "*" {
isWildCard = true
}
}
if !isWildCard {
return false
}
baseIndex := 0
valueIndex := 0
for {
if len(baseArr) == baseIndex && len(valueArr) == valueIndex {
return true
}
if len(baseArr) == baseIndex {
return false
}
if len(valueArr) == valueIndex {
return false
}
if valueArr[valueIndex] == baseArr[baseIndex] {
baseIndex++
valueIndex++
continue
}
if len(baseArr) == baseIndex+1 {
return false
}
if valueArr[valueIndex] == baseArr[baseIndex+1] {
baseIndex++
continue
}
if baseArr[baseIndex] == "*" {
valueIndex++
continue
}
return false
}
return true
}
func wildcardCount(values []string) int {
count := 0
for _, value := range values {
if value == "*" {
count++
}
}
return count
}
func isMatchesForArray(base string, value string) bool {
if base == value {
return true
}
baseArr := strings.Split(base, ".")
valueArr := strings.Split(value, ".")
for baseIndex, baseValue := range baseArr {
for valueIndex, valueValue := range valueArr {
if baseIndex == valueIndex && baseValue == valueValue {
continue
}
if baseIndex == valueIndex && isSame(base, value) {
continue
}
if baseIndex != valueIndex {
continue
}
return false
}
}
return true
}
func isSame(base, value string) bool {
var aStack Stack
var bStack Stack
for _, v := range base {
aStack.Push(fmt.Sprintf("%v", v))
}
for _, v := range value {
bStack.Push(fmt.Sprintf("%v", v))
}
for !bStack.IsEmpty() {
aValue, aHasValue := aStack.Pop()
bValue, bHasValue := bStack.Pop()
if aHasValue && bHasValue && aValue == bValue {
continue
}
if aHasValue && bHasValue && aValue != bValue {
bNextValue, bHasNextValue := bStack.Pop()
if bHasNextValue && bNextValue == aValue {
continue
}
}
return false
}
return true
}