-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.go
105 lines (94 loc) · 1.95 KB
/
spec.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
package brief
import (
"fmt"
"strings"
)
// Spec for node Type:Name
type Spec struct {
Type, Name string
NoName bool
}
// NewSpec from spec of Type:Name or just Type
func NewSpec(spec string) *Spec {
pos := strings.IndexRune(spec, ':')
if pos < 0 {
return &Spec{Type: spec, NoName: true}
}
return &Spec{Type: spec[:pos], Name: spec[pos+1:]}
}
func (s *Spec) String() string {
if len(s.Name) > 0 {
return fmt.Sprintf("%s:%s", s.Type, s.Name)
}
return s.Type
}
// Match spec for node
func (s *Spec) Match(node *Node) bool {
if s.Type != node.Type {
return false
}
if s.NoName {
return true
}
if s.Name != node.Name {
return false
}
return true
}
// Find searches for a node matching name in the body of this node
// The name is a node type or a type:name pair
func (node *Node) Find(name string) *Node {
return NewSpec(name).Find(node)
}
// Find looks for a specific node in the body that matches spec
func (s *Spec) Find(node *Node) *Node {
for _, sub := range node.Body {
if s.Match(sub) {
return sub
}
}
for _, sub := range node.Body {
if found := s.Find(sub); found != nil {
return found
}
}
return nil
}
// Child follow a path to a specific node in the body
// path elements are node type or type:name pair
func (node *Node) Child(path ...string) *Node {
at := node
for _, name := range path {
spec := NewSpec(name)
var found bool
for _, next := range at.Body {
if spec.Match(next) {
found = true
at = next
break
}
}
if !found {
return nil
}
}
return at
}
// FindAll nodes that match name
func (node *Node) FindAll(name string) []*Node {
return NewSpec(name).FindAll(node)
}
// FindAll nodes in body that match spec
func (s *Spec) FindAll(node *Node) []*Node {
result := make([]*Node, 0)
for _, sub := range node.Body {
if s.Match(sub) {
result = append(result, sub)
}
}
for _, sub := range node.Body {
found := s.FindAll(sub)
result = append(result, found...)
}
return result
}