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