forked from einride/aip-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatches.go
36 lines (35 loc) · 1021 Bytes
/
matches.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
package resourcename
// Match reports whether the specified resource name matches the specified resource name pattern.
func Match(pattern, name string) bool {
var nameScanner, patternScanner Scanner
nameScanner.Init(name)
patternScanner.Init(pattern)
for patternScanner.Scan() {
if !nameScanner.Scan() {
return false
}
nameSegment := nameScanner.Segment()
if nameSegment.IsVariable() {
return false
}
patternSegment := patternScanner.Segment()
if patternSegment.IsWildcard() {
return false // edge case - wildcard not allowed in patterns
}
if patternSegment.IsVariable() {
if nameSegment == "" {
return false
}
} else if nameSegment != patternSegment {
return false
}
}
switch {
case
nameScanner.Scan(), // name has more segments than pattern, no match
patternScanner.Segment() == "", // edge case - empty pattern never matches
patternScanner.Full(): // edge case - full resource name not allowed in patterns
return false
}
return true
}