Skip to content

Commit 925b736

Browse files
committed
Apply feedback
1 parent a584cfc commit 925b736

File tree

8 files changed

+131
-69
lines changed

8 files changed

+131
-69
lines changed

internal/fixture/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (s *State) UpdateObject(tv *typed.TypedValue, version fieldpath.APIVersion,
100100
return nil
101101
}
102102

103-
// Set the current state with the passed in object
103+
// Update the current state with the passed in object
104104
func (s *State) Update(obj typed.YAMLObject, version fieldpath.APIVersion, manager string) error {
105105
tv, err := s.Parser.FromYAML(FixTabsOrDie(obj))
106106
if err != nil {
@@ -308,7 +308,7 @@ func (f ForceApplyObject) preprocess(parser typed.ParseableType) (Operation, err
308308
return f, nil
309309
}
310310

311-
// Set is a type of operation. It is a controller type of
311+
// Update is a type of operation. It is a controller type of
312312
// update. Errors are passed along.
313313
type Update struct {
314314
Manager string

merge/real_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func BenchmarkOperations(b *testing.B) {
101101
},
102102
},
103103
{
104-
name: "Set",
104+
name: "Update",
105105
ops: []Operation{
106106
Update{
107107
Manager: "controller",

merge/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpa
114114
return managers, compare, nil
115115
}
116116

117-
// Set is the method you should call once you've merged your final
117+
// Update is the method you should call once you've merged your final
118118
// object on CREATE/UPDATE/PATCH verbs. newObject must be the object
119119
// that you intend to persist (after applying the patch if this is for a
120120
// PATCH call), and liveObject must be the original object (empty if

typed/parser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ func (p ParseableType) FromUnstructured(in interface{}) (*TypedValue, error) {
114114

115115
// FromStructured converts a go "interface{}" type, typically an structured object in
116116
// Kubernetes, to a TypedValue. It will return an error if the resulting object fails
117-
// schema validation. The provided "interface{}" may contain structs and types that are
118-
// converted to Values by the jsonMarshaler interface.
117+
// schema validation. The provided "interface{}" value must be a pointer so that the
118+
// value can be modified via reflection. The provided "interface{}" may contain structs
119+
// and types that are converted to Values by the jsonMarshaler interface.
119120
func (p ParseableType) FromStructured(in interface{}) (*TypedValue, error) {
120121
v, err := value.NewValueReflect(in)
121122
if err != nil {

typed/union.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (u *union) NormalizeApply(applied, merged, out value.Map) error {
265265
return fmt.Errorf("applied discriminator (%v) doesn't match applied field (%v)", u.d.Get(applied), *as.One())
266266
}
267267

268-
// Set discriminiator if needed
268+
// Update discriminiator if needed
269269
if u.deduceInvalidDiscriminator {
270270
u.d.Set(out, u.dn.toDiscriminated(*as.One()))
271271
}

value/fields.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Field struct {
2727
Value Value
2828
}
2929

30-
// FieldList is a list of key-value pairs. Each field is expected to
30+
// FieldList is a list of key-value pairs. Each field is expectUpdated to
3131
// have a different name.
3232
type FieldList []Field
3333

value/structreflect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ type fieldCacheEntry struct {
8585
isOmitEmpty bool
8686
// fieldPath is the field indices (see FieldByIndex) to lookup the value of
8787
// a field in a reflect.Value struct. A path of field indices is used
88-
// to support traversing to a field nested in struct fields that have the 'inline'
88+
// to support traversing to a field field in struct fields that have the 'inline'
8989
// json tag.
9090
fieldPath [][]int
9191
}
9292

9393
func (f *fieldCacheEntry) getFieldFromStruct(structVal reflect.Value) reflect.Value {
94-
// field might be nested within 'inline' structs
94+
// field might be field within 'inline' structs
9595
for _, elem := range f.fieldPath {
9696
structVal = structVal.FieldByIndex(elem)
9797
}

value/valuereflect_test.go

Lines changed: 120 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -299,72 +299,133 @@ func TestReflectStructMutate(t *testing.T) {
299299
}
300300
}
301301

302-
// TestReflectMutateNestedStruct ensures a structs nested within various typed can be modified.
302+
// TestReflectMutateNestedStruct ensures a structs field within various typed can be modified.
303303
func TestReflectMutateNestedStruct(t *testing.T) {
304-
type nested struct {
304+
type field struct {
305305
S string `json:"s,omitempty"`
306306
}
307-
type root struct {
308-
Field nested `json:"field"`
309-
List []nested `json:"list"`
310-
Map map[string]nested `json:"map"`
311-
MapOfMaps map[string]map[string]nested `json:"mapOfMaps"`
312-
MapOfLists map[string][]nested `json:"mapOfLists"`
313-
}
314-
rv := MustReflect(&root{
315-
Field: nested{S: "field"},
316-
List: []nested{{S: "listItem"}},
317-
Map: map[string]nested{"mapKey": {S: "mapItem"}},
318-
MapOfMaps: map[string]map[string]nested{"outer": {"inner": {S: "mapOfMapItem"}}},
319-
MapOfLists: map[string][]nested{"outer": {{S: "mapOfListsItem"}}},
320-
})
321-
rootMap := rv.AsMap()
322-
field, _ := rootMap.Get("field")
323-
list, _ := rootMap.Get("list")
324-
listItem := list.AsList().At(0)
325-
m, _ := rootMap.Get("map")
326-
mapItem, _ := m.AsMap().Get("mapKey")
327-
mapOfMaps, _ := rootMap.Get("mapOfMaps")
328-
innerMap, _ := mapOfMaps.AsMap().Get("outer")
329-
mapOfMapsItem, _ := innerMap.AsMap().Get("inner")
330-
mapOfLists, _ := rootMap.Get("mapOfLists")
331-
innerList, _ := mapOfLists.AsMap().Get("outer")
332-
mapOfListsItem := innerList.AsList().At(0)
333-
334-
field.AsMap().Set("s", NewValueInterface("field2"))
335-
listItem.AsMap().Set("s", NewValueInterface("listItem2"))
336-
mapItem.AsMap().Set("s", NewValueInterface("mapItem2"))
337-
mapOfMapsItem.AsMap().Set("s", NewValueInterface("mapOfMapItem2"))
338-
mapOfListsItem.AsMap().Set("s", NewValueInterface("mapOfListsItem2"))
339307

340-
unstructured := rv.Unstructured()
341-
expectedMap := map[string]interface{}{
342-
"field": map[string]interface{}{"s": "field2"},
343-
"list": []interface{}{map[string]interface{}{"s": "listItem2"}},
344-
"map": map[string]interface{}{"mapKey": map[string]interface{}{"s": "mapItem2"}},
345-
"mapOfMaps": map[string]interface{}{"outer": map[string]interface{}{"inner": map[string]interface{}{"s": "mapOfMapItem2"}}},
346-
"mapOfLists": map[string]interface{}{"outer": []interface{}{map[string]interface{}{"s": "mapOfListsItem2"}}},
347-
}
348-
if !reflect.DeepEqual(unstructured, expectedMap) {
349-
t.Errorf("expected %v but got: %v", expectedMap, unstructured)
308+
cases := []struct {
309+
fieldName string
310+
root Value
311+
lookupField func(root Value) Value
312+
expectUpdated interface{}
313+
expectDeleted interface{}
314+
}{
315+
{
316+
fieldName: "field",
317+
root: MustReflect(&struct {
318+
Field field `json:"field,omitempty"`
319+
}{
320+
Field: field{S: "field"},
321+
}),
322+
lookupField: func(rv Value) Value {
323+
field, _ := rv.AsMap().Get("field")
324+
return field
325+
},
326+
expectUpdated: map[string]interface{}{
327+
"field": map[string]interface{}{"s": "updatedValue"},
328+
},
329+
expectDeleted: map[string]interface{}{
330+
"field": map[string]interface{}{},
331+
},
332+
},
333+
{
334+
fieldName: "map",
335+
root: MustReflect(&struct {
336+
Map map[string]field `json:"map,omitempty"`
337+
}{
338+
Map: map[string]field{"mapKey": {S: "mapItem"}},
339+
}),
340+
lookupField: func(rv Value) Value {
341+
m, _ := rv.AsMap().Get("map")
342+
mapItem, _ := m.AsMap().Get("mapKey")
343+
return mapItem
344+
},
345+
expectUpdated: map[string]interface{}{
346+
"map": map[string]interface{}{"mapKey": map[string]interface{}{"s": "updatedValue"}},
347+
},
348+
expectDeleted: map[string]interface{}{
349+
"map": map[string]interface{}{"mapKey": map[string]interface{}{}},
350+
},
351+
},
352+
{
353+
fieldName: "list",
354+
root: MustReflect(&struct {
355+
List []field `json:"list,omitempty"`
356+
}{
357+
List: []field{{S: "listItem"}},
358+
}),
359+
lookupField: func(rv Value) Value {
360+
list, _ := rv.AsMap().Get("list")
361+
return list.AsList().At(0)
362+
},
363+
expectUpdated: map[string]interface{}{
364+
"list": []interface{}{map[string]interface{}{"s": "updatedValue"}},
365+
},
366+
expectDeleted: map[string]interface{}{
367+
"list": []interface{}{map[string]interface{}{}},
368+
},
369+
},
370+
{
371+
fieldName: "mapOfMaps",
372+
root: MustReflect(&struct {
373+
MapOfMaps map[string]map[string]field `json:"mapOfMaps,omitempty"`
374+
}{
375+
MapOfMaps: map[string]map[string]field{"outer": {"inner": {S: "mapOfMapItem"}}},
376+
}),
377+
lookupField: func(rv Value) Value {
378+
mapOfMaps, _ := rv.AsMap().Get("mapOfMaps")
379+
innerMap, _ := mapOfMaps.AsMap().Get("outer")
380+
mapOfMapsItem, _ := innerMap.AsMap().Get("inner")
381+
return mapOfMapsItem
382+
},
383+
expectUpdated: map[string]interface{}{
384+
"mapOfMaps": map[string]interface{}{"outer": map[string]interface{}{"inner": map[string]interface{}{"s": "updatedValue"}}},
385+
},
386+
expectDeleted: map[string]interface{}{
387+
"mapOfMaps": map[string]interface{}{"outer": map[string]interface{}{"inner": map[string]interface{}{}}},
388+
},
389+
},
390+
{
391+
fieldName: "mapOfLists",
392+
root: MustReflect(&struct {
393+
MapOfLists map[string][]field `json:"mapOfLists,omitempty"`
394+
}{
395+
MapOfLists: map[string][]field{"outer": {{S: "mapOfListsItem"}}},
396+
}),
397+
lookupField: func(rv Value) Value {
398+
mapOfLists, _ := rv.AsMap().Get("mapOfLists")
399+
innerList, _ := mapOfLists.AsMap().Get("outer")
400+
mapOfListsItem := innerList.AsList().At(0)
401+
return mapOfListsItem
402+
},
403+
404+
expectUpdated: map[string]interface{}{
405+
"mapOfLists": map[string]interface{}{"outer": []interface{}{map[string]interface{}{"s": "updatedValue"}}},
406+
},
407+
expectDeleted: map[string]interface{}{
408+
"mapOfLists": map[string]interface{}{"outer": []interface{}{map[string]interface{}{}}},
409+
},
410+
},
350411
}
351412

352-
field.AsMap().Delete("s")
353-
listItem.AsMap().Delete("s")
354-
mapItem.AsMap().Delete("s")
355-
mapOfMapsItem.AsMap().Delete("s")
356-
mapOfListsItem.AsMap().Delete("s")
413+
for _, tc := range cases {
414+
t.Run(tc.fieldName, func(t *testing.T) {
415+
root := tc.root
416+
field := tc.lookupField(root)
417+
field.AsMap().Set("s", NewValueInterface("updatedValue"))
418+
unstructured := root.Unstructured()
419+
if !reflect.DeepEqual(unstructured, tc.expectUpdated) {
420+
t.Errorf("expected %v but got: %v", tc.expectUpdated, unstructured)
421+
}
357422

358-
unstructured = rv.Unstructured()
359-
expectedMap = map[string]interface{}{
360-
"field": map[string]interface{}{},
361-
"list": []interface{}{map[string]interface{}{}},
362-
"map": map[string]interface{}{"mapKey": map[string]interface{}{}},
363-
"mapOfMaps": map[string]interface{}{"outer": map[string]interface{}{"inner": map[string]interface{}{}}},
364-
"mapOfLists": map[string]interface{}{"outer": []interface{}{map[string]interface{}{}}},
365-
}
366-
if !reflect.DeepEqual(unstructured, expectedMap) {
367-
t.Errorf("expected %v but got: %v", expectedMap, unstructured)
423+
field.AsMap().Delete("s")
424+
unstructured = root.Unstructured()
425+
if !reflect.DeepEqual(unstructured, tc.expectDeleted) {
426+
t.Errorf("expected %v but got: %v", tc.expectDeleted, unstructured)
427+
}
428+
})
368429
}
369430
}
370431

0 commit comments

Comments
 (0)