Skip to content

Commit 059502f

Browse files
authored
Merge pull request #85 from apelisse/versioned-set-api
Make VersionedSet into an interface
2 parents 0cb3320 + 3f447ff commit 059502f

15 files changed

+543
-434
lines changed

fieldpath/managers.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,42 @@ package fieldpath
1616
// APIVersion describes the version of an object or of a fieldset.
1717
type APIVersion string
1818

19+
type VersionedSet interface {
20+
Set() *Set
21+
APIVersion() APIVersion
22+
Applied() bool
23+
}
24+
1925
// VersionedSet associates a version to a set.
20-
type VersionedSet struct {
21-
*Set
22-
APIVersion APIVersion
23-
Applied bool
26+
type versionedSet struct {
27+
set *Set
28+
apiVersion APIVersion
29+
applied bool
30+
}
31+
32+
func NewVersionedSet(set *Set, apiVersion APIVersion, applied bool) VersionedSet {
33+
return versionedSet{
34+
set: set,
35+
apiVersion: apiVersion,
36+
applied: applied,
37+
}
38+
}
39+
40+
func (v versionedSet) Set() *Set {
41+
return v.set
42+
}
43+
44+
func (v versionedSet) APIVersion() APIVersion {
45+
return v.apiVersion
46+
}
47+
48+
func (v versionedSet) Applied() bool {
49+
return v.applied
2450
}
2551

2652
// ManagedFields is a map from manager to VersionedSet (what they own in
2753
// what version).
28-
type ManagedFields map[string]*VersionedSet
54+
type ManagedFields map[string]VersionedSet
2955

3056
// Difference returns a symmetric difference between two Managers. If a
3157
// given user's entry has version X in lhs and version Y in rhs, then
@@ -37,7 +63,7 @@ func (lhs ManagedFields) Difference(rhs ManagedFields) ManagedFields {
3763
for manager, left := range lhs {
3864
right, ok := rhs[manager]
3965
if !ok {
40-
if !left.Empty() {
66+
if !left.Set().Empty() {
4167
diff[manager] = left
4268
}
4369
continue
@@ -46,17 +72,14 @@ func (lhs ManagedFields) Difference(rhs ManagedFields) ManagedFields {
4672
// If we have sets in both but their version
4773
// differs, we don't even diff and keep the
4874
// entire thing.
49-
if left.APIVersion != right.APIVersion {
75+
if left.APIVersion() != right.APIVersion() {
5076
diff[manager] = right
5177
continue
5278
}
5379

54-
newSet := left.Difference(right.Set).Union(right.Difference(left.Set))
80+
newSet := left.Set().Difference(right.Set()).Union(right.Set().Difference(left.Set()))
5581
if !newSet.Empty() {
56-
diff[manager] = &VersionedSet{
57-
Set: newSet,
58-
APIVersion: right.APIVersion,
59-
}
82+
diff[manager] = NewVersionedSet(newSet, right.APIVersion(), false)
6083
}
6184
}
6285

@@ -65,7 +88,7 @@ func (lhs ManagedFields) Difference(rhs ManagedFields) ManagedFields {
6588
// Already done
6689
continue
6790
}
68-
if !set.Empty() {
91+
if !set.Set().Empty() {
6992
diff[manager] = set
7093
}
7194
}

fieldpath/managers_test.go

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -44,98 +44,112 @@ func TestManagersDifference(t *testing.T) {
4444
{
4545
name: "Empty RHS",
4646
lhs: fieldpath.ManagedFields{
47-
"default": &fieldpath.VersionedSet{
48-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
49-
APIVersion: "v1",
50-
},
47+
"default": fieldpath.NewVersionedSet(
48+
_NS(_P("numeric"), _P("string"), _P("bool")),
49+
"v1",
50+
false,
51+
),
5152
},
5253
out: fieldpath.ManagedFields{
53-
"default": &fieldpath.VersionedSet{
54-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
55-
APIVersion: "v1",
56-
},
54+
"default": fieldpath.NewVersionedSet(
55+
_NS(_P("numeric"), _P("string"), _P("bool")),
56+
"v1",
57+
false,
58+
),
5759
},
5860
},
5961
{
6062
name: "Empty LHS",
6163
rhs: fieldpath.ManagedFields{
62-
"default": &fieldpath.VersionedSet{
63-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
64-
APIVersion: "v1",
65-
},
64+
"default": fieldpath.NewVersionedSet(
65+
_NS(_P("numeric"), _P("string"), _P("bool")),
66+
"v1",
67+
false,
68+
),
6669
},
6770
out: fieldpath.ManagedFields{
68-
"default": &fieldpath.VersionedSet{
69-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
70-
APIVersion: "v1",
71-
},
71+
"default": fieldpath.NewVersionedSet(
72+
_NS(_P("numeric"), _P("string"), _P("bool")),
73+
"v1",
74+
false,
75+
),
7276
},
7377
},
7478
{
7579
name: "Different managers",
7680
lhs: fieldpath.ManagedFields{
77-
"one": &fieldpath.VersionedSet{
78-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
79-
APIVersion: "v1",
80-
},
81+
"one": fieldpath.NewVersionedSet(
82+
_NS(_P("numeric"), _P("string"), _P("bool")),
83+
"v1",
84+
false,
85+
),
8186
},
8287
rhs: fieldpath.ManagedFields{
83-
"two": &fieldpath.VersionedSet{
84-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
85-
APIVersion: "v1",
86-
},
88+
"two": fieldpath.NewVersionedSet(
89+
_NS(_P("numeric"), _P("string"), _P("bool")),
90+
"v1",
91+
false,
92+
),
8793
},
8894
out: fieldpath.ManagedFields{
89-
"one": &fieldpath.VersionedSet{
90-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
91-
APIVersion: "v1",
92-
},
93-
"two": &fieldpath.VersionedSet{
94-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
95-
APIVersion: "v1",
96-
},
95+
"one": fieldpath.NewVersionedSet(
96+
_NS(_P("numeric"), _P("string"), _P("bool")),
97+
"v1",
98+
false,
99+
),
100+
"two": fieldpath.NewVersionedSet(
101+
_NS(_P("numeric"), _P("string"), _P("bool")),
102+
"v1",
103+
false,
104+
),
97105
},
98106
},
99107
{
100108
name: "Same manager, different version",
101109
lhs: fieldpath.ManagedFields{
102-
"one": &fieldpath.VersionedSet{
103-
Set: _NS(_P("numeric"), _P("string"), _P("integer")),
104-
APIVersion: "v1",
105-
},
110+
"one": fieldpath.NewVersionedSet(
111+
_NS(_P("numeric"), _P("string"), _P("integer")),
112+
"v1",
113+
false,
114+
),
106115
},
107116
rhs: fieldpath.ManagedFields{
108-
"one": &fieldpath.VersionedSet{
109-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
110-
APIVersion: "v2",
111-
},
117+
"one": fieldpath.NewVersionedSet(
118+
_NS(_P("numeric"), _P("string"), _P("bool")),
119+
"v2",
120+
false,
121+
),
112122
},
113123
out: fieldpath.ManagedFields{
114-
"one": &fieldpath.VersionedSet{
115-
Set: _NS(_P("numeric"), _P("string"), _P("bool")),
116-
APIVersion: "v2",
117-
},
124+
"one": fieldpath.NewVersionedSet(
125+
_NS(_P("numeric"), _P("string"), _P("bool")),
126+
"v2",
127+
false,
128+
),
118129
},
119130
},
120131
{
121132
name: "Set difference",
122133
lhs: fieldpath.ManagedFields{
123-
"one": &fieldpath.VersionedSet{
124-
Set: _NS(_P("numeric"), _P("string")),
125-
APIVersion: "v1",
126-
},
134+
"one": fieldpath.NewVersionedSet(
135+
_NS(_P("numeric"), _P("string")),
136+
"v1",
137+
false,
138+
),
127139
},
128140
rhs: fieldpath.ManagedFields{
129-
"one": &fieldpath.VersionedSet{
130-
Set: _NS(_P("string"), _P("bool")),
131-
APIVersion: "v1",
132-
},
141+
"one": fieldpath.NewVersionedSet(
142+
_NS(_P("string"), _P("bool")),
143+
"v1",
144+
false,
145+
),
133146
},
134147
out: fieldpath.ManagedFields{
135-
"one": &fieldpath.VersionedSet{
136-
Set: _NS(_P("numeric"), _P("bool")),
137-
APIVersion: "v1",
138-
},
148+
"one": fieldpath.NewVersionedSet(
149+
_NS(_P("numeric"), _P("bool")),
150+
"v1",
151+
false,
152+
),
139153
},
140154
},
141155
}

internal/fixture/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (tc TestCase) TestWithConverter(parser typed.ParseableType, converter merge
303303

304304
// Fail if any empty sets are present in the managers
305305
for manager, set := range state.Managers {
306-
if set.Empty() {
306+
if set.Set().Empty() {
307307
return fmt.Errorf("expected Managers to have no empty sets, but found one managed by %v", manager)
308308
}
309309
}

merge/conflict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func ConflictsFromManagers(sets fieldpath.ManagedFields) Conflicts {
7979
conflicts := []Conflict{}
8080

8181
for manager, set := range sets {
82-
set.Iterate(func(p fieldpath.Path) {
82+
set.Set().Iterate(func(p fieldpath.Path) {
8383
conflicts = append(conflicts, Conflict{
8484
Manager: manager,
8585
Path: p,

merge/conflict_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ var (
3535

3636
func TestNewFromSets(t *testing.T) {
3737
got := merge.ConflictsFromManagers(fieldpath.ManagedFields{
38-
"Bob": &fieldpath.VersionedSet{
39-
Set: _NS(
38+
"Bob": fieldpath.NewVersionedSet(
39+
_NS(
4040
_P("key"),
4141
_P("list", _KBF("key", _SV("a"), "id", _IV(2)), "id"),
4242
),
43-
APIVersion: "v1",
44-
},
45-
"Alice": &fieldpath.VersionedSet{
46-
Set: _NS(
43+
"v1",
44+
false,
45+
),
46+
"Alice": fieldpath.NewVersionedSet(
47+
_NS(
4748
_P("value"),
4849
_P("list", _KBF("key", _SV("a"), "id", _IV(2)), "key"),
4950
),
50-
APIVersion: "v1",
51-
},
51+
"v1",
52+
false,
53+
),
5254
})
5355
wanted := `conflicts with "Alice":
5456
- .value

0 commit comments

Comments
 (0)