Skip to content

disable unions, since they're still experimental #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/fixture/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ type TestCase struct {
// Managed, if not nil, is the ManagedFields as expected
// after all operations are run.
Managed fieldpath.ManagedFields
// Set to true if the test case needs the union behavior enabled.
RequiresUnions bool
}

// Test runs the test-case using the given parser and a dummy converter.
Expand All @@ -283,6 +285,9 @@ func (tc TestCase) BenchWithConverter(parser typed.ParseableType, converter merg
Updater: &merge.Updater{Converter: converter},
Parser: parser,
}
if tc.RequiresUnions {
state.Updater.EnableUnionFeature()
}
// We currently don't have any test that converts, we can take
// care of that later.
for i, ops := range tc.Ops {
Expand All @@ -300,6 +305,17 @@ func (tc TestCase) TestWithConverter(parser typed.ParseableType, converter merge
Updater: &merge.Updater{Converter: converter},
Parser: parser,
}
if tc.RequiresUnions {
state.Updater.EnableUnionFeature()
} else {
// Also test it with unions on.
tc2 := tc
tc2.RequiresUnions = true
err := tc2.TestWithConverter(parser, converter)
if err != nil {
return fmt.Errorf("fails if unions are on: %v", err)
}
}
// We currently don't have any test that converts, we can take
// care of that later.
for i, ops := range tc.Ops {
Expand Down
7 changes: 7 additions & 0 deletions merge/union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var unionFieldsParser = func() typed.ParseableType {
func TestUnion(t *testing.T) {
tests := map[string]TestCase{
"union_apply_owns_discriminator": {
RequiresUnions: true,
Ops: []Operation{
Apply{
Manager: "default",
Expand All @@ -91,6 +92,7 @@ func TestUnion(t *testing.T) {
},
},
"union_apply_without_discriminator_conflict": {
RequiresUnions: true,
Ops: []Operation{
Update{
Manager: "controller",
Expand Down Expand Up @@ -125,6 +127,7 @@ func TestUnion(t *testing.T) {
},
},
"union_apply_with_null_value": {
RequiresUnions: true,
Ops: []Operation{
Apply{
Manager: "default",
Expand All @@ -138,6 +141,7 @@ func TestUnion(t *testing.T) {
},
},
"union_apply_multiple_unions": {
RequiresUnions: true,
Ops: []Operation{
Apply{
Manager: "default",
Expand Down Expand Up @@ -176,6 +180,7 @@ func TestUnion(t *testing.T) {
func TestUnionErrors(t *testing.T) {
tests := map[string]TestCase{
"union_apply_two": {
RequiresUnions: true,
Ops: []Operation{
Apply{
Manager: "default",
Expand All @@ -188,6 +193,7 @@ func TestUnionErrors(t *testing.T) {
},
},
"union_apply_two_and_discriminator": {
RequiresUnions: true,
Ops: []Operation{
Apply{
Manager: "default",
Expand All @@ -201,6 +207,7 @@ func TestUnionErrors(t *testing.T) {
},
},
"union_apply_wrong_discriminator": {
RequiresUnions: true,
Ops: []Operation{
Apply{
Manager: "default",
Expand Down
26 changes: 20 additions & 6 deletions merge/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ type Converter interface {
// merge the object on Apply.
type Updater struct {
Converter Converter

enableUnions bool
}

// EnableUnionFeature turns on union handling. It is disabled by default until the
// feature is complete.
func (s *Updater) EnableUnionFeature() {
s.enableUnions = true
}

func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, workflow string, force bool) (fieldpath.ManagedFields, error) {
Expand Down Expand Up @@ -112,9 +120,12 @@ func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpa
// PATCH call), and liveObject must be the original object (empty if
// this is a CREATE call).
func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string) (*typed.TypedValue, fieldpath.ManagedFields, error) {
newObject, err := liveObject.NormalizeUnions(newObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
var err error
if s.enableUnions {
newObject, err = liveObject.NormalizeUnions(newObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
}
}
managers = shallowCopyManagers(managers)
managers, err = s.update(liveObject, newObject, version, managers, manager, true)
Expand Down Expand Up @@ -144,9 +155,12 @@ func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldp
// and return it.
func (s *Updater) Apply(liveObject, configObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (*typed.TypedValue, fieldpath.ManagedFields, error) {
managers = shallowCopyManagers(managers)
configObject, err := configObject.NormalizeUnionsApply(configObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
var err error
if s.enableUnions {
configObject, err = configObject.NormalizeUnionsApply(configObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you forgot one just below (yes, we call it before AND after merging)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#98

}
newObject, err := liveObject.Merge(configObject)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue {
// - If discriminator changed to non-nil, all other fields but the
// discriminated one will be cleared,
// - Otherwise, If only one field is left, update discriminator to that value.
//
// Please note: union behavior isn't finalized yet and this is still experimental.
func (tv TypedValue) NormalizeUnions(new *TypedValue) (*TypedValue, error) {
var errs ValidationErrors
var normalizeFn = func(w *mergingWalker) {
Expand All @@ -177,6 +179,8 @@ func (tv TypedValue) NormalizeUnions(new *TypedValue) (*TypedValue, error) {
// NormalizeUnionsApply specifically normalize unions on apply. It
// validates that the applied union is correct (there should be no
// ambiguity there), and clear the fields according to the sent intent.
//
// Please note: union behavior isn't finalized yet and this is still experimental.
func (tv TypedValue) NormalizeUnionsApply(new *TypedValue) (*TypedValue, error) {
var errs ValidationErrors
var normalizeFn = func(w *mergingWalker) {
Expand Down