@@ -25,43 +25,11 @@ import (
25
25
"sigs.k8s.io/structured-merge-diff/value"
26
26
)
27
27
28
- // TypedValue is a value with an associated type.
29
- type TypedValue interface {
30
- // AsValue removes the type from the TypedValue and only keeps the value.
31
- AsValue () * value.Value
32
- // Validate returns an error with a list of every spec violation.
33
- Validate () error
34
- // ToFieldSet creates a set containing every leaf field and item mentioned, or
35
- // validation errors, if any were encountered.
36
- ToFieldSet () (* fieldpath.Set , error )
37
- // Merge returns the result of merging tv and pso ("partially specified
38
- // object") together. Of note:
39
- // * No fields can be removed by this operation.
40
- // * If both tv and pso specify a given leaf field, the result will keep pso's
41
- // value.
42
- // * Container typed elements will have their items ordered:
43
- // * like tv, if pso doesn't change anything in the container
44
- // * like pso, if pso does change something in the container.
45
- // tv and pso must both be of the same type (their Schema and TypeRef must
46
- // match), or an error will be returned. Validation errors will be returned if
47
- // the objects don't conform to the schema.
48
- Merge (pso TypedValue ) (TypedValue , error )
49
- // Compare compares the two objects. See the comments on the `Comparison`
50
- // struct for details on the return value.
51
- //
52
- // tv and rhs must both be of the same type (their Schema and TypeRef must
53
- // match), or an error will be returned. Validation errors will be returned if
54
- // the objects don't conform to the schema.
55
- Compare (rhs TypedValue ) (c * Comparison , err error )
56
- // RemoveItems removes each provided list or map item from the value.
57
- RemoveItems (items * fieldpath.Set ) TypedValue
58
- }
59
-
60
28
// AsTyped accepts a value and a type and returns a TypedValue. 'v' must have
61
29
// type 'typeName' in the schema. An error is returned if the v doesn't conform
62
30
// to the schema.
63
- func AsTyped (v value.Value , s * schema.Schema , typeRef schema.TypeRef ) (TypedValue , error ) {
64
- tv := typedValue {
31
+ func AsTyped (v value.Value , s * schema.Schema , typeRef schema.TypeRef ) (* TypedValue , error ) {
32
+ tv := & TypedValue {
65
33
value : v ,
66
34
typeRef : typeRef ,
67
35
schema : s ,
@@ -76,36 +44,38 @@ func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef) (TypedValu
76
44
// conforms to the schema, for cases where that has already been checked or
77
45
// where you're going to call a method that validates as a side-effect (like
78
46
// ToFieldSet).
79
- func AsTypedUnvalidated (v value.Value , s * schema.Schema , typeRef schema.TypeRef ) TypedValue {
80
- tv := typedValue {
47
+ func AsTypedUnvalidated (v value.Value , s * schema.Schema , typeRef schema.TypeRef ) * TypedValue {
48
+ tv := & TypedValue {
81
49
value : v ,
82
50
typeRef : typeRef ,
83
51
schema : s ,
84
52
}
85
53
return tv
86
54
}
87
55
88
- // typedValue is a value of some specific type.
89
- type typedValue struct {
56
+ // TypedValue is a value of some specific type.
57
+ type TypedValue struct {
90
58
value value.Value
91
59
typeRef schema.TypeRef
92
60
schema * schema.Schema
93
61
}
94
62
95
- var _ TypedValue = typedValue {}
96
-
97
- func (tv typedValue ) AsValue () * value.Value {
63
+ // AsValue removes the type from the TypedValue and only keeps the value.
64
+ func (tv TypedValue ) AsValue () * value.Value {
98
65
return & tv .value
99
66
}
100
67
101
- func (tv typedValue ) Validate () error {
68
+ // Validate returns an error with a list of every spec violation.
69
+ func (tv TypedValue ) Validate () error {
102
70
if errs := tv .walker ().validate (); len (errs ) != 0 {
103
71
return errs
104
72
}
105
73
return nil
106
74
}
107
75
108
- func (tv typedValue ) ToFieldSet () (* fieldpath.Set , error ) {
76
+ // ToFieldSet creates a set containing every leaf field and item mentioned, or
77
+ // validation errors, if any were encountered.
78
+ func (tv TypedValue ) ToFieldSet () (* fieldpath.Set , error ) {
109
79
s := fieldpath .NewSet ()
110
80
w := tv .walker ()
111
81
w .leafFieldCallback = func (p fieldpath.Path ) { s .Insert (p ) }
@@ -116,27 +86,34 @@ func (tv typedValue) ToFieldSet() (*fieldpath.Set, error) {
116
86
return s , nil
117
87
}
118
88
119
- func (tv typedValue ) Merge (pso TypedValue ) (TypedValue , error ) {
120
- tpso , ok := pso .(typedValue )
121
- if ! ok {
122
- return nil , errorFormatter {}.
123
- errorf ("can't merge typedValue with %T" , pso )
124
- }
125
- return merge (tv , tpso , ruleKeepRHS , nil )
89
+ // Merge returns the result of merging tv and pso ("partially specified
90
+ // object") together. Of note:
91
+ // * No fields can be removed by this operation.
92
+ // * If both tv and pso specify a given leaf field, the result will keep pso's
93
+ // value.
94
+ // * Container typed elements will have their items ordered:
95
+ // * like tv, if pso doesn't change anything in the container
96
+ // * like pso, if pso does change something in the container.
97
+ // tv and pso must both be of the same type (their Schema and TypeRef must
98
+ // match), or an error will be returned. Validation errors will be returned if
99
+ // the objects don't conform to the schema.
100
+ func (tv TypedValue ) Merge (pso * TypedValue ) (* TypedValue , error ) {
101
+ return merge (& tv , pso , ruleKeepRHS , nil )
126
102
}
127
103
128
- func (tv typedValue ) Compare (rhs TypedValue ) (c * Comparison , err error ) {
129
- trhs , ok := rhs .(typedValue )
130
- if ! ok {
131
- return nil , errorFormatter {}.
132
- errorf ("can't compare typedValue with %T" , rhs )
133
- }
104
+ // Compare compares the two objects. See the comments on the `Comparison`
105
+ // struct for details on the return value.
106
+ //
107
+ // tv and rhs must both be of the same type (their Schema and TypeRef must
108
+ // match), or an error will be returned. Validation errors will be returned if
109
+ // the objects don't conform to the schema.
110
+ func (tv TypedValue ) Compare (rhs * TypedValue ) (c * Comparison , err error ) {
134
111
c = & Comparison {
135
112
Removed : fieldpath .NewSet (),
136
113
Modified : fieldpath .NewSet (),
137
114
Added : fieldpath .NewSet (),
138
115
}
139
- c .Merged , err = merge (tv , trhs , func (w * mergingWalker ) {
116
+ c .Merged , err = merge (& tv , rhs , func (w * mergingWalker ) {
140
117
if w .lhs == nil {
141
118
c .Added .Insert (w .path )
142
119
} else if w .rhs == nil {
@@ -163,14 +140,13 @@ func (tv typedValue) Compare(rhs TypedValue) (c *Comparison, err error) {
163
140
}
164
141
165
142
// RemoveItems removes each provided list or map item from the value.
166
- func (tv typedValue ) RemoveItems (items * fieldpath.Set ) TypedValue {
167
- copied := tv
168
- copied .value , _ = value .FromUnstructured (tv .value .ToUnstructured (true ))
169
- removeItemsWithSchema (& copied .value , items , copied .schema , copied .typeRef )
170
- return copied
143
+ func (tv TypedValue ) RemoveItems (items * fieldpath.Set ) * TypedValue {
144
+ tv .value , _ = value .FromUnstructured (tv .value .ToUnstructured (true ))
145
+ removeItemsWithSchema (& tv .value , items , tv .schema , tv .typeRef )
146
+ return & tv
171
147
}
172
148
173
- func merge (lhs , rhs typedValue , rule , postRule mergeRule ) (TypedValue , error ) {
149
+ func merge (lhs , rhs * TypedValue , rule , postRule mergeRule ) (* TypedValue , error ) {
174
150
if lhs .schema != rhs .schema {
175
151
return nil , errorFormatter {}.
176
152
errorf ("expected objects with types from the same schema" )
@@ -193,7 +169,7 @@ func merge(lhs, rhs typedValue, rule, postRule mergeRule) (TypedValue, error) {
193
169
return nil , errs
194
170
}
195
171
196
- out := typedValue {
172
+ out := & TypedValue {
197
173
schema : lhs .schema ,
198
174
typeRef : lhs .typeRef ,
199
175
}
@@ -212,7 +188,7 @@ func merge(lhs, rhs typedValue, rule, postRule mergeRule) (TypedValue, error) {
212
188
type Comparison struct {
213
189
// Merged is the result of merging the two objects, as explained in the
214
190
// comments on TypedValue.Merge().
215
- Merged TypedValue
191
+ Merged * TypedValue
216
192
217
193
// Removed contains any fields removed by rhs (the right-hand-side
218
194
// object in the comparison).
0 commit comments