-
Notifications
You must be signed in to change notification settings - Fork 0
/
referencesUtil.go
75 lines (66 loc) · 2.08 KB
/
referencesUtil.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
package api
import (
"strings"
)
// First returns the first StepName component of the SubmoduleRef.
// The empty string is returned if this SubmoduleRef is itself zero.
func (ref SubmoduleRef) First() StepName {
i := strings.IndexRune(string(ref), '.')
if i > 0 {
return StepName(ref[:i])
}
return StepName(ref)
}
// Child appends the stepname to this ref.
// Think of it as leaving breadcrumbs behind as you zoom in
// ('Child' and 'Decontextualize' often come in pairs.)
func (ref SubmoduleRef) Child(child StepName) SubmoduleRef {
if ref == "" {
return SubmoduleRef(child)
}
return SubmoduleRef(string(ref) + "." + string(child))
}
// Contextualize prepends a set of step references to this ref.
// Think of it as zooming out.
func (ref SubmoduleRef) Contextualize(parent SubmoduleRef) SubmoduleRef {
if ref == "" {
return parent
}
return SubmoduleRef(string(parent) + "." + string(ref))
}
// Decontextualize strips the first stepName from the front of the ref.
// Think of it as zooming in.
func (ref SubmoduleRef) Decontextualize() SubmoduleRef {
i := strings.IndexRune(string(ref), '.')
if i > 0 {
return ref[i+1:]
}
return ""
}
// Contextualize prepends a set of step references to this ref.
func (ref SubmoduleStepRef) Contextualize(parent SubmoduleRef) SubmoduleStepRef {
return SubmoduleStepRef{
ref.SubmoduleRef.Contextualize(parent),
ref.StepName,
}
}
// Decontextualize strips the first stepName from the front of the ref.
// Think of it as zooming in.
func (ref SubmoduleStepRef) Decontextualize() SubmoduleStepRef {
return SubmoduleStepRef{ref.SubmoduleRef.Decontextualize(), ref.StepName}
}
// Contextualize prepends a set of step references to this ref.
func (ref SubmoduleSlotRef) Contextualize(parent SubmoduleRef) SubmoduleSlotRef {
return SubmoduleSlotRef{
ref.SubmoduleRef.Contextualize(parent),
ref.SlotRef,
}
}
// Decontextualize strips the first stepName from the front of the ref.
// Think of it as zooming in.
func (ref SubmoduleSlotRef) Decontextualize() SubmoduleSlotRef {
return SubmoduleSlotRef{
ref.SubmoduleRef.Decontextualize(),
ref.SlotRef,
}
}