Skip to content

Commit 302c24c

Browse files
authored
Merge pull request #59 from apelisse/merge-multiple-types
Merge multiple types
2 parents 3e9f406 + db5504c commit 302c24c

File tree

10 files changed

+771
-306
lines changed

10 files changed

+771
-306
lines changed

fieldpath/path.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func (fp Path) String() string {
3535
return strings.Join(strs, "")
3636
}
3737

38+
func (fp Path) Copy() Path {
39+
new := make(Path, len(fp))
40+
copy(new, fp)
41+
return new
42+
}
43+
3844
// MakePath constructs a Path. The parts may be PathElements, ints, strings.
3945
func MakePath(parts ...interface{}) (Path, error) {
4046
var fp Path

internal/fixture/state.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// State of the current test in terms of live object. One can check at
3030
// any time that Live and Managers match the expectations.
3131
type State struct {
32-
Live *typed.TypedValue
32+
Live typed.TypedValue
3333
Parser typed.ParseableType
3434
Managers fieldpath.ManagedFields
3535
Updater *merge.Updater
@@ -91,7 +91,7 @@ func (s *State) checkInit() error {
9191
if err != nil {
9292
return fmt.Errorf("failed to create new empty object: %v", err)
9393
}
94-
s.Live = &obj
94+
s.Live = obj
9595
}
9696
return nil
9797
}
@@ -103,11 +103,11 @@ func (s *State) Update(obj typed.YAMLObject, version fieldpath.APIVersion, manag
103103
return err
104104
}
105105
tv, err := s.Parser.FromYAML(obj)
106-
managers, err := s.Updater.Update(*s.Live, tv, version, s.Managers, manager)
106+
managers, err := s.Updater.Update(s.Live, tv, version, s.Managers, manager)
107107
if err != nil {
108108
return err
109109
}
110-
s.Live = &tv
110+
s.Live = tv
111111
s.Managers = managers
112112

113113
return nil
@@ -123,11 +123,11 @@ func (s *State) Apply(obj typed.YAMLObject, version fieldpath.APIVersion, manage
123123
if err != nil {
124124
return err
125125
}
126-
new, managers, err := s.Updater.Apply(*s.Live, tv, version, s.Managers, manager, force)
126+
new, managers, err := s.Updater.Apply(s.Live, tv, version, s.Managers, manager, force)
127127
if err != nil {
128128
return err
129129
}
130-
s.Live = &new
130+
s.Live = new
131131
s.Managers = managers
132132

133133
return nil
@@ -249,7 +249,7 @@ func (tc TestCase) Test(parser typed.ParseableType) error {
249249
return fmt.Errorf("failed to compare live with config: %v", err)
250250
}
251251
if !comparison.IsSame() {
252-
return fmt.Errorf("expected live and config to be the same: %v", comparison)
252+
return fmt.Errorf("expected live and config to be the same:\n%v", comparison)
253253
}
254254
}
255255

merge/deduced_test.go

Lines changed: 15 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"sigs.k8s.io/structured-merge-diff/typed"
2626
)
2727

28-
func TestDeducedTypesBroken(t *testing.T) {
28+
func TestDeduced(t *testing.T) {
2929
tests := map[string]TestCase{
3030
"leaf_apply_twice": {
3131
Ops: []Operation{
@@ -230,28 +230,7 @@ func TestDeducedTypesBroken(t *testing.T) {
230230
},
231231
},
232232
},
233-
"leaf_apply_remove_empty_set": {
234-
Ops: []Operation{
235-
Apply{
236-
Manager: "default",
237-
APIVersion: "v1",
238-
Object: `
239-
string: "string"
240-
`,
241-
},
242-
Apply{
243-
Manager: "default",
244-
APIVersion: "v1",
245-
Object: "",
246-
},
247-
},
248-
Object: `
249-
string: "string"
250-
`,
251-
Managed: fieldpath.ManagedFields{},
252-
},
253-
254-
"apply_twice": {
233+
"apply_twice_list_is_atomic": {
255234
Ops: []Operation{
256235
Apply{
257236
Manager: "default",
@@ -268,94 +247,27 @@ func TestDeducedTypesBroken(t *testing.T) {
268247
Object: `
269248
list:
270249
- a
271-
- b
272-
- c
273250
- d
274-
`,
275-
},
276-
},
277-
Object: `
278-
list:
279-
- a
280-
- b
281-
- c
282-
- d
283-
`,
284-
Managed: fieldpath.ManagedFields{
285-
"default": &fieldpath.VersionedSet{
286-
Set: _NS(
287-
_P("list", _SV("a")),
288-
_P("list", _SV("b")),
289-
_P("list", _SV("c")),
290-
_P("list", _SV("d")),
291-
),
292-
APIVersion: "v1",
293-
},
294-
},
295-
},
296-
"apply_update_apply_no_overlap": {
297-
Ops: []Operation{
298-
Apply{
299-
Manager: "default",
300-
APIVersion: "v1",
301-
Object: `
302-
list:
303-
- a
304251
- c
305-
`,
306-
},
307-
Update{
308-
Manager: "controller",
309-
APIVersion: "v1",
310-
Object: `
311-
list:
312-
- a
313252
- b
314-
- c
315-
- d
316-
`,
317-
},
318-
Apply{
319-
Manager: "default",
320-
APIVersion: "v1",
321-
Object: `
322-
list:
323-
- a
324-
- aprime
325-
- c
326-
- cprime
327253
`,
328254
},
329255
},
330256
Object: `
331257
list:
332258
- a
333-
- aprime
334-
- b
335-
- c
336-
- cprime
337259
- d
260+
- c
261+
- b
338262
`,
339263
Managed: fieldpath.ManagedFields{
340264
"default": &fieldpath.VersionedSet{
341-
Set: _NS(
342-
_P("list", _SV("a")),
343-
_P("list", _SV("aprime")),
344-
_P("list", _SV("c")),
345-
_P("list", _SV("cprime")),
346-
),
347-
APIVersion: "v1",
348-
},
349-
"controller": &fieldpath.VersionedSet{
350-
Set: _NS(
351-
_P("list", _SV("b")),
352-
_P("list", _SV("d")),
353-
),
265+
Set: _NS(_P("list")),
354266
APIVersion: "v1",
355267
},
356268
},
357269
},
358-
"apply_update_apply_with_overlap": {
270+
"apply_update_apply_list": {
359271
Ops: []Operation{
360272
Apply{
361273
Manager: "default",
@@ -377,7 +289,7 @@ func TestDeducedTypesBroken(t *testing.T) {
377289
- d
378290
`,
379291
},
380-
Apply{
292+
ForceApply{
381293
Manager: "default",
382294
APIVersion: "v1",
383295
Object: `
@@ -393,131 +305,40 @@ func TestDeducedTypesBroken(t *testing.T) {
393305
- a
394306
- b
395307
- c
396-
- d
397308
`,
398309
Managed: fieldpath.ManagedFields{
399310
"default": &fieldpath.VersionedSet{
400-
Set: _NS(
401-
_P("list", _SV("a")),
402-
_P("list", _SV("b")),
403-
_P("list", _SV("c")),
404-
),
405-
APIVersion: "v1",
406-
},
407-
"controller": &fieldpath.VersionedSet{
408-
Set: _NS(
409-
_P("list", _SV("b")),
410-
_P("list", _SV("d")),
411-
),
412-
APIVersion: "v1",
413-
},
414-
},
415-
},
416-
"apply_twice_reorder": {
417-
Ops: []Operation{
418-
Apply{
419-
Manager: "default",
420-
APIVersion: "v1",
421-
Object: `
422-
list:
423-
- a
424-
- b
425-
- c
426-
- d
427-
`,
428-
},
429-
Apply{
430-
Manager: "default",
431-
APIVersion: "v1",
432-
Object: `
433-
list:
434-
- a
435-
- d
436-
- c
437-
- b
438-
`,
439-
},
440-
},
441-
Object: `
442-
list:
443-
- a
444-
- d
445-
- c
446-
- b
447-
`,
448-
Managed: fieldpath.ManagedFields{
449-
"default": &fieldpath.VersionedSet{
450-
Set: _NS(
451-
_P("list", _SV("a")),
452-
_P("list", _SV("b")),
453-
_P("list", _SV("c")),
454-
_P("list", _SV("d")),
455-
),
311+
Set: _NS(_P("list")),
456312
APIVersion: "v1",
457313
},
458314
},
459315
},
460-
"apply_update_apply_reorder": {
316+
"leaf_apply_remove_empty_set": {
461317
Ops: []Operation{
462318
Apply{
463319
Manager: "default",
464320
APIVersion: "v1",
465321
Object: `
466-
list:
467-
- a
468-
- b
469-
- c
470-
- d
471-
`,
472-
},
473-
Update{
474-
Manager: "controller",
475-
APIVersion: "v1",
476-
Object: `
477-
list:
478-
- a
479-
- d
480-
- c
481-
- b
322+
string: "string"
482323
`,
483324
},
484325
Apply{
485326
Manager: "default",
486327
APIVersion: "v1",
487-
Object: `
488-
list:
489-
- a
490-
- b
491-
- c
492-
- d
493-
`,
328+
Object: ``,
494329
},
495330
},
496331
Object: `
497-
list:
498-
- a
499-
- b
500-
- c
501-
- d
332+
string: "string"
502333
`,
503-
Managed: fieldpath.ManagedFields{
504-
"default": &fieldpath.VersionedSet{
505-
Set: _NS(
506-
_P("list", _SV("a")),
507-
_P("list", _SV("b")),
508-
_P("list", _SV("c")),
509-
_P("list", _SV("d")),
510-
),
511-
APIVersion: "v1",
512-
},
513-
},
334+
Managed: fieldpath.ManagedFields{},
514335
},
515336
}
516337

517338
for name, test := range tests {
518339
t.Run(name, func(t *testing.T) {
519-
if test.Test(typed.DeducedParseableType{}) == nil {
520-
t.Fatal("Broken test passed")
340+
if err := test.Test(typed.DeducedParseableType{}); err != nil {
341+
t.Fatal(err)
521342
}
522343
})
523344
}

merge/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,18 @@ func (s *Updater) Update(liveObject, newObject typed.TypedValue, version fieldpa
127127
func (s *Updater) Apply(liveObject, configObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (typed.TypedValue, fieldpath.ManagedFields, error) {
128128
newObject, err := liveObject.Merge(configObject)
129129
if err != nil {
130-
return typed.TypedValue{}, fieldpath.ManagedFields{}, fmt.Errorf("failed to merge config: %v", err)
130+
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to merge config: %v", err)
131131
}
132132
managers, err = s.update(liveObject, newObject, version, managers, manager, force)
133133
if err != nil {
134-
return typed.TypedValue{}, fieldpath.ManagedFields{}, err
134+
return nil, fieldpath.ManagedFields{}, err
135135
}
136136

137137
// TODO: Remove unconflicting removed fields
138138

139139
set, err := configObject.ToFieldSet()
140140
if err != nil {
141-
return typed.TypedValue{}, fieldpath.ManagedFields{}, fmt.Errorf("failed to get field set: %v", err)
141+
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to get field set: %v", err)
142142
}
143143
managers[manager] = &fieldpath.VersionedSet{
144144
Set: set,

0 commit comments

Comments
 (0)