forked from freeconf/yang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
43 lines (41 loc) · 917 Bytes
/
find.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
package meta
import "strings"
func Find(p Meta, path string) Definition {
if strings.HasPrefix(path, "../") {
if p.Parent() == nil {
return nil
}
return Find(p.Parent(), path[3:])
}
if strings.HasPrefix(path, "/") {
return Find(RootModule(p), path[1:])
}
if seg := strings.IndexRune(path, '/'); seg > 0 {
if child := Find(p, path[:seg]); child != nil {
return Find(child, path[seg+1:])
}
return nil
}
if colon := strings.IndexRune(path, ':'); colon > 0 {
ident := path[colon+1:]
if _, isModule := p.(*Module); isModule {
prefix := path[:colon]
mod, err := RootModule(p).ModuleByPrefix(prefix)
if err != nil {
return nil
}
p = mod
}
return Find(p, ident)
}
if hd, ok := p.(HasDataDefinitions); ok {
return hd.Definition(path)
}
if choice, ok := p.(*Choice); ok {
if c, found := choice.Cases()[path]; found {
return c
}
return nil
}
return nil
}