-
Notifications
You must be signed in to change notification settings - Fork 1
/
relator.go
104 lines (92 loc) · 2.01 KB
/
relator.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
package lookup
// find Stores a .Find()
type find struct {
path string
runners []Runner
}
// How the relator interactions with Scope
type relationType int
const (
// Relates to the Scope.Current position
relationTypeCurrent relationType = iota
// Relates to the Scope.Current parent
relationTypeParent
// Relates to the Scope.Position position
relationTypePosition
)
// Relator allows you to do an Evaluate from a relative location
type Relator struct {
finds []*find
positionName string
relationType relationType
}
func Find(path string, opts ...Runner) *Relator {
r := &Relator{}
return r.Find(path, opts...)
}
func Parent(path string, opts ...Runner) *Relator {
r := &Relator{
relationType: relationTypeParent,
}
return r.Find(path, opts...)
}
func This(paths ...string) *Relator {
r := &Relator{}
for _, path := range paths {
r = r.Find(path)
}
return r
}
func Result(paths ...string) *Relator {
r := &Relator{
relationType: relationTypePosition,
}
for _, path := range paths {
r = r.Find(path)
}
return r
}
func NewRelator() *Relator {
return &Relator{
finds: nil,
positionName: "",
}
}
// Find stores a find request to be used in the relative location. Please note this doesn't alloc a new Relator use
// Copy for that.
func (r *Relator) Find(path string, opts ...Runner) *Relator {
r.finds = append(r.finds, &find{
path: path,
runners: opts,
})
return r
}
// Copy produces a copy of the Relator
func (r *Relator) Copy() *Relator {
fs := make([]*find, len(r.finds), len(r.finds))
copy(fs, r.finds)
return &Relator{
finds: fs,
positionName: r.positionName,
relationType: r.relationType,
}
}
func (r *Relator) Run(scope *Scope) Pathor {
var p Pathor
switch r.relationType {
case relationTypeParent:
if scope.Parent != nil {
p = scope.Parent.Current
break
}
fallthrough
case relationTypeCurrent:
p = scope.Current
case relationTypePosition:
p = scope.Position
}
for _, f := range r.finds {
p = p.Find(f.path, f.runners...)
}
return p
}