-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
73 lines (66 loc) · 1.39 KB
/
group.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
package robots
import (
"net/url"
"strings"
)
type Group struct {
ProductTokens ProductTokens
Rules []*Rule
}
// Allowed returns (true, _) if the group is not for the given user-agent.
// Otherwise, it returns (_, true) if the path is allowed.
func (g Group) Allowed(pt ProductToken, path *url.URL) *bool {
var check bool
for _, gpt := range g.ProductTokens {
if *gpt == Wildcard || gpt.Equal(&pt) {
check = true
break
}
}
if !check {
// Not checking this group, it's not for this user-agent.
return nil
}
for _, r := range g.Rules {
allowed := r.Allowed(path)
if allowed == nil {
// Rule path does not match the given path.
continue
}
return allowed
}
// It matched the group, but no rules applied.
defaultAllow := true
return &defaultAllow
}
func (g Group) Equal(o *Group) bool {
if len(g.ProductTokens) != len(o.ProductTokens) {
return false
}
if len(g.Rules) != len(o.Rules) {
return false
}
for i := range g.ProductTokens {
if !g.ProductTokens[i].Equal(o.ProductTokens[i]) {
return false
}
}
for i := range g.Rules {
if !g.Rules[i].Equal(o.Rules[i]) {
return false
}
}
return true
}
func (g Group) String() string {
var b strings.Builder
for _, pt := range g.ProductTokens {
b.WriteString(pt.String())
b.WriteString("\n")
}
for _, r := range g.Rules {
b.WriteString(r.String())
b.WriteString("\n")
}
return b.String()
}