forked from hashicorp/terraform-plugin-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_steps.go
98 lines (74 loc) · 2.08 KB
/
path_steps.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
package path
import "strings"
// PathSteps represents an ordered collection of attribute path transversals.
type PathSteps []PathStep
// Append adds the given PathSteps to the end of the previous PathSteps and
// returns the combined result.
func (s *PathSteps) Append(steps ...PathStep) PathSteps {
if s == nil {
return steps
}
*s = append(*s, steps...)
return *s
}
// Copy returns a duplicate of the steps that is safe to modify without
// affecting the original. Returns nil if the original steps is nil.
func (s PathSteps) Copy() PathSteps {
if s == nil {
return nil
}
copiedPathSteps := make(PathSteps, len(s))
copy(copiedPathSteps, s)
return copiedPathSteps
}
// Equal returns true if the given PathSteps are equivalent.
func (s PathSteps) Equal(o PathSteps) bool {
if len(s) != len(o) {
return false
}
for stepIndex, step := range s {
if !step.Equal(o[stepIndex]) {
return false
}
}
return true
}
// LastStep returns the final PathStep and the remaining PathSteps.
func (s PathSteps) LastStep() (PathStep, PathSteps) {
if len(s) == 0 {
return nil, PathSteps{}
}
if len(s) == 1 {
return s[0], PathSteps{}
}
return s[len(s)-1], s[:len(s)-1]
}
// NextStep returns the first PathStep and the remaining PathSteps.
func (s PathSteps) NextStep() (PathStep, PathSteps) {
if len(s) == 0 {
return nil, s
}
return s[0], s[1:]
}
// String returns the human-readable representation of the PathSteps.
// It is intended for logging and error messages and is not protected by
// compatibility guarantees.
func (s PathSteps) String() string {
var result strings.Builder
for stepIndex, step := range s {
if _, ok := step.(PathStepAttributeName); ok && stepIndex != 0 {
result.WriteString(".")
}
result.WriteString(step.String())
}
return result.String()
}
// ExpressionSteps returns the ordered collection of expression steps which
// exactly matches the PathSteps.
func (s PathSteps) ExpressionSteps() ExpressionSteps {
result := make(ExpressionSteps, len(s))
for stepIndex, pathStep := range s {
result[stepIndex] = pathStep.ExpressionStep()
}
return result
}