...
1
16
17 package fieldpath
18
19 import (
20 "fmt"
21 "strings"
22
23 "sigs.k8s.io/structured-merge-diff/v4/value"
24 )
25
26
27
28 type Path []PathElement
29
30 func (fp Path) String() string {
31 strs := make([]string, len(fp))
32 for i := range fp {
33 strs[i] = fp[i].String()
34 }
35 return strings.Join(strs, "")
36 }
37
38
39 func (fp Path) Equals(fp2 Path) bool {
40 if len(fp) != len(fp2) {
41 return false
42 }
43 for i := range fp {
44 if !fp[i].Equals(fp2[i]) {
45 return false
46 }
47 }
48 return true
49 }
50
51
52 func (fp Path) Compare(rhs Path) int {
53 i := 0
54 for {
55 if i >= len(fp) && i >= len(rhs) {
56
57 return 0
58 }
59 if i >= len(fp) {
60
61 return -1
62 }
63 if i >= len(rhs) {
64
65 return 1
66 }
67 if c := fp[i].Compare(rhs[i]); c != 0 {
68 return c
69 }
70
71 i++
72 }
73 }
74
75 func (fp Path) Copy() Path {
76 new := make(Path, len(fp))
77 copy(new, fp)
78 return new
79 }
80
81
82 func MakePath(parts ...interface{}) (Path, error) {
83 var fp Path
84 for _, p := range parts {
85 switch t := p.(type) {
86 case PathElement:
87 fp = append(fp, t)
88 case int:
89
90
91 fp = append(fp, PathElement{Index: &t})
92 case string:
93 fp = append(fp, PathElement{FieldName: &t})
94 case *value.FieldList:
95 if len(*t) == 0 {
96 return nil, fmt.Errorf("associative list key type path elements must have at least one key (got zero)")
97 }
98 fp = append(fp, PathElement{Key: t})
99 case value.Value:
100
101
102 fp = append(fp, PathElement{Value: &t})
103 default:
104 return nil, fmt.Errorf("unable to make %#v into a path element", p)
105 }
106 }
107 return fp, nil
108 }
109
110
111
112 func MakePathOrDie(parts ...interface{}) Path {
113 fp, err := MakePath(parts...)
114 if err != nil {
115 panic(err)
116 }
117 return fp
118 }
119
View as plain text