forked from hashicorp/terraform-plugin-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressions.go
77 lines (60 loc) · 1.61 KB
/
expressions.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
package path
import "strings"
// Expressions is a collection of attribute path expressions.
//
// Refer to the Expression documentation for more details about intended usage.
type Expressions []Expression
// Append adds the given Expressions to the collection without duplication and
// returns the combined result.
func (e *Expressions) Append(expressions ...Expression) Expressions {
if e == nil {
return expressions
}
for _, newExpression := range expressions {
if e.Contains(newExpression) {
continue
}
*e = append(*e, newExpression)
}
return *e
}
// Contains returns true if the collection of expressions includes the given
// expression.
func (e Expressions) Contains(checkExpression Expression) bool {
for _, expression := range e {
if expression.Equal(checkExpression) {
return true
}
}
return false
}
// Matches returns true if one of the expressions in the collection matches the
// given path.
func (e Expressions) Matches(checkPath Path) bool {
for _, expression := range e {
if expression.Matches(checkPath) {
return true
}
}
return false
}
// String returns the human-readable representation of the expression
// collection. It is intended for logging and error messages and is not
// protected by compatibility guarantees.
//
// Empty expressions are skipped.
func (p Expressions) String() string {
var result strings.Builder
result.WriteString("[")
for pathIndex, path := range p {
if path.Equal(Expression{}) {
continue
}
if pathIndex != 0 {
result.WriteString(",")
}
result.WriteString(path.String())
}
result.WriteString("]")
return result.String()
}