...
1
2
3
4 package types
5
6 import (
7 "fmt"
8
9 "sigs.k8s.io/kustomize/kyaml/resid"
10 )
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 type FieldSpec struct {
30 resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
31 Path string `json:"path,omitempty" yaml:"path,omitempty"`
32 CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"`
33
34
35 }
36
37 func (fs FieldSpec) String() string {
38 return fmt.Sprintf(
39 "%s:%v:%s", fs.Gvk.String(), fs.CreateIfNotPresent, fs.Path)
40 }
41
42
43 func (fs FieldSpec) effectivelyEquals(other FieldSpec) bool {
44 return fs.IsSelected(&other.Gvk) && fs.Path == other.Path
45 }
46
47 type FsSlice []FieldSpec
48
49 func (s FsSlice) Len() int { return len(s) }
50 func (s FsSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
51 func (s FsSlice) Less(i, j int) bool {
52 return s[i].Gvk.IsLessThan(s[j].Gvk)
53 }
54
55
56 func (s FsSlice) DeepCopy() FsSlice {
57 ret := make(FsSlice, len(s))
58 copy(ret, s)
59 return ret
60 }
61
62
63
64
65
66 func (s FsSlice) MergeAll(incoming FsSlice) (result FsSlice, err error) {
67 result = s
68 for _, x := range incoming {
69 result, err = result.MergeOne(x)
70 if err != nil {
71 return nil, err
72 }
73 }
74 return result, nil
75 }
76
77
78
79
80
81 func (s FsSlice) MergeOne(x FieldSpec) (FsSlice, error) {
82 i := s.index(x)
83 if i > -1 {
84
85 if s[i].CreateIfNotPresent != x.CreateIfNotPresent {
86 return nil, fmt.Errorf("conflicting fieldspecs")
87 }
88 return s, nil
89 }
90 return append(s, x), nil
91 }
92
93 func (s FsSlice) index(fs FieldSpec) int {
94 for i, x := range s {
95 if x.effectivelyEquals(fs) {
96 return i
97 }
98 }
99 return -1
100 }
101
View as plain text