-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathexpression.go
More file actions
163 lines (126 loc) · 4.48 KB
/
expression.go
File metadata and controls
163 lines (126 loc) · 4.48 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package path
import (
"github.com/hashicorp/terraform-plugin-framework/attr"
)
// Expression represents an attribute path with expression steps, which can
// represent zero, one, or more actual Paths.
type Expression struct {
// steps is the transversals included with the expression. In general,
// operations against the path should protect against modification of the
// original.
steps ExpressionSteps
}
// AtAnyListIndex returns a copied expression with a new list index step at the
// end. The returned path is safe to modify without affecting the original.
func (e Expression) AtAnyListIndex() Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepElementKeyIntAny{})
return copiedPath
}
// AtAnyMapKey returns a copied expression with a new map key step at the end.
// The returned path is safe to modify without affecting the original.
func (e Expression) AtAnyMapKey() Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepElementKeyStringAny{})
return copiedPath
}
// AtAnySetValue returns a copied expression with a new set value step at the
// end. The returned path is safe to modify without affecting the original.
func (e Expression) AtAnySetValue() Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepElementKeyValueAny{})
return copiedPath
}
// AtListIndex returns a copied expression with a new list index step at the
// end. The returned path is safe to modify without affecting the original.
func (e Expression) AtListIndex(index int) Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepElementKeyIntExact(index))
return copiedPath
}
// AtMapKey returns a copied expression with a new map key step at the end.
// The returned path is safe to modify without affecting the original.
func (e Expression) AtMapKey(key string) Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepElementKeyStringExact(key))
return copiedPath
}
// AtName returns a copied expression with a new attribute or block name step
// at the end. The returned path is safe to modify without affecting the
// original.
func (e Expression) AtName(name string) Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepAttributeNameExact(name))
return copiedPath
}
// AtParent returns a copied expression with a new parent step at the end.
// The returned path is safe to modify without affecting the original.
func (e Expression) AtParent() Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepParent{})
return copiedPath
}
// AtSetValue returns a copied expression with a new set value step at the end.
// The returned path is safe to modify without affecting the original.
func (e Expression) AtSetValue(value attr.Value) Expression {
copiedPath := e.Copy()
copiedPath.steps.Append(ExpressionStepElementKeyValueExact{Value: value})
return copiedPath
}
// Copy returns a duplicate of the expression that is safe to modify without
// affecting the original.
func (e Expression) Copy() Expression {
return Expression{
steps: e.Steps(),
}
}
// Equal returns true if the given expression is exactly equivalent.
func (e Expression) Equal(o Expression) bool {
if e.steps == nil && o.steps == nil {
return true
}
if e.steps == nil {
return false
}
if !e.steps.Equal(o.steps) {
return false
}
return true
}
// Matches returns true if the given Path is valid for the Expression.
func (e Expression) Matches(path Path) bool {
return e.steps.Matches(path.Steps())
}
// Steps returns a copy of the underlying expression steps. Returns an empty
// collection of steps if expression is nil.
func (e Expression) Steps() ExpressionSteps {
if len(e.steps) == 0 {
return ExpressionSteps{}
}
return e.steps.Copy()
}
// String returns the human-readable representation of the path.
// It is intended for logging and error messages and is not protected by
// compatibility guarantees.
func (e Expression) String() string {
return e.steps.String()
}
// MatchParent creates an attribute path expression starting with
// ExpressionStepParent. This allows creating a relative expression in
// nested schemas.
func MatchParent() Expression {
return Expression{
steps: ExpressionSteps{
ExpressionStepParent{},
},
}
}
// MatchRoot creates an attribute path expression starting with
// ExpressionStepAttributeNameExact.
func MatchRoot(rootAttributeName string) Expression {
return Expression{
steps: ExpressionSteps{
ExpressionStepAttributeNameExact(rootAttributeName),
},
}
}