Skip to content

Commit 655edf8

Browse files
committed
disable unions, since they're still experimental
1 parent e2d5a6e commit 655edf8

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

internal/fixture/state.go

+16
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ type TestCase struct {
261261
// Managed, if not nil, is the ManagedFields as expected
262262
// after all operations are run.
263263
Managed fieldpath.ManagedFields
264+
// Set to true if the test case needs the union behavior enabled.
265+
RequiresUnions bool
264266
}
265267

266268
// Test runs the test-case using the given parser and a dummy converter.
@@ -283,6 +285,9 @@ func (tc TestCase) BenchWithConverter(parser typed.ParseableType, converter merg
283285
Updater: &merge.Updater{Converter: converter},
284286
Parser: parser,
285287
}
288+
if tc.RequiresUnions {
289+
state.Updater.EnableUnionFeature()
290+
}
286291
// We currently don't have any test that converts, we can take
287292
// care of that later.
288293
for i, ops := range tc.Ops {
@@ -300,6 +305,17 @@ func (tc TestCase) TestWithConverter(parser typed.ParseableType, converter merge
300305
Updater: &merge.Updater{Converter: converter},
301306
Parser: parser,
302307
}
308+
if tc.RequiresUnions {
309+
state.Updater.EnableUnionFeature()
310+
} else {
311+
// Also test it with unions on.
312+
tc2 := tc
313+
tc2.RequiresUnions = true
314+
err := tc2.TestWithConverter(parser, converter)
315+
if err != nil {
316+
return fmt.Errorf("fails if unions are on: %v", err)
317+
}
318+
}
303319
// We currently don't have any test that converts, we can take
304320
// care of that later.
305321
for i, ops := range tc.Ops {

merge/union_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var unionFieldsParser = func() typed.ParseableType {
6767
func TestUnion(t *testing.T) {
6868
tests := map[string]TestCase{
6969
"union_apply_owns_discriminator": {
70+
RequiresUnions: true,
7071
Ops: []Operation{
7172
Apply{
7273
Manager: "default",
@@ -91,6 +92,7 @@ func TestUnion(t *testing.T) {
9192
},
9293
},
9394
"union_apply_without_discriminator_conflict": {
95+
RequiresUnions: true,
9496
Ops: []Operation{
9597
Update{
9698
Manager: "controller",
@@ -125,6 +127,7 @@ func TestUnion(t *testing.T) {
125127
},
126128
},
127129
"union_apply_with_null_value": {
130+
RequiresUnions: true,
128131
Ops: []Operation{
129132
Apply{
130133
Manager: "default",
@@ -138,6 +141,7 @@ func TestUnion(t *testing.T) {
138141
},
139142
},
140143
"union_apply_multiple_unions": {
144+
RequiresUnions: true,
141145
Ops: []Operation{
142146
Apply{
143147
Manager: "default",
@@ -176,6 +180,7 @@ func TestUnion(t *testing.T) {
176180
func TestUnionErrors(t *testing.T) {
177181
tests := map[string]TestCase{
178182
"union_apply_two": {
183+
RequiresUnions: true,
179184
Ops: []Operation{
180185
Apply{
181186
Manager: "default",
@@ -188,6 +193,7 @@ func TestUnionErrors(t *testing.T) {
188193
},
189194
},
190195
"union_apply_two_and_discriminator": {
196+
RequiresUnions: true,
191197
Ops: []Operation{
192198
Apply{
193199
Manager: "default",
@@ -201,6 +207,7 @@ func TestUnionErrors(t *testing.T) {
201207
},
202208
},
203209
"union_apply_wrong_discriminator": {
210+
RequiresUnions: true,
204211
Ops: []Operation{
205212
Apply{
206213
Manager: "default",

merge/update.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ type Converter interface {
3131
// merge the object on Apply.
3232
type Updater struct {
3333
Converter Converter
34+
35+
enableUnions bool
36+
}
37+
38+
// EnableUnionFeature turns on union handling. It is disabled by default until the
39+
// feature is complete.
40+
func (s *Updater) EnableUnionFeature() {
41+
s.enableUnions = true
3442
}
3543

3644
func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, workflow string, force bool) (fieldpath.ManagedFields, error) {
@@ -112,9 +120,12 @@ func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpa
112120
// PATCH call), and liveObject must be the original object (empty if
113121
// this is a CREATE call).
114122
func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string) (*typed.TypedValue, fieldpath.ManagedFields, error) {
115-
newObject, err := liveObject.NormalizeUnions(newObject)
116-
if err != nil {
117-
return nil, fieldpath.ManagedFields{}, err
123+
var err error
124+
if s.enableUnions {
125+
newObject, err = liveObject.NormalizeUnions(newObject)
126+
if err != nil {
127+
return nil, fieldpath.ManagedFields{}, err
128+
}
118129
}
119130
managers = shallowCopyManagers(managers)
120131
managers, err = s.update(liveObject, newObject, version, managers, manager, true)
@@ -144,9 +155,12 @@ func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldp
144155
// and return it.
145156
func (s *Updater) Apply(liveObject, configObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (*typed.TypedValue, fieldpath.ManagedFields, error) {
146157
managers = shallowCopyManagers(managers)
147-
configObject, err := configObject.NormalizeUnionsApply(configObject)
148-
if err != nil {
149-
return nil, fieldpath.ManagedFields{}, err
158+
var err error
159+
if s.enableUnions {
160+
configObject, err = configObject.NormalizeUnionsApply(configObject)
161+
if err != nil {
162+
return nil, fieldpath.ManagedFields{}, err
163+
}
150164
}
151165
newObject, err := liveObject.Merge(configObject)
152166
if err != nil {

typed/typed.go

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue {
153153
// - If discriminator changed to non-nil, all other fields but the
154154
// discriminated one will be cleared,
155155
// - Otherwise, If only one field is left, update discriminator to that value.
156+
//
157+
// Please note: union behavior isn't finalized yet and this is still experimental.
156158
func (tv TypedValue) NormalizeUnions(new *TypedValue) (*TypedValue, error) {
157159
var errs ValidationErrors
158160
var normalizeFn = func(w *mergingWalker) {
@@ -177,6 +179,8 @@ func (tv TypedValue) NormalizeUnions(new *TypedValue) (*TypedValue, error) {
177179
// NormalizeUnionsApply specifically normalize unions on apply. It
178180
// validates that the applied union is correct (there should be no
179181
// ambiguity there), and clear the fields according to the sent intent.
182+
//
183+
// Please note: union behavior isn't finalized yet and this is still experimental.
180184
func (tv TypedValue) NormalizeUnionsApply(new *TypedValue) (*TypedValue, error) {
181185
var errs ValidationErrors
182186
var normalizeFn = func(w *mergingWalker) {

0 commit comments

Comments
 (0)