-
Notifications
You must be signed in to change notification settings - Fork 630
Make SubscribableSpec implement apis.Convertible #3472
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,18 @@ func TestSubscribableConversionBadType(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestSubscribableSpecConversionBadType(t *testing.T) { | ||
| good, bad := &SubscribableSpec{}, &SubscribableSpec{} | ||
|
|
||
| if err := good.ConvertTo(context.Background(), bad); err == nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. that's the convention used in everywhere in knative/eventing btw. |
||
| t.Errorf("ConvertTo() = %#v, wanted error", bad) | ||
| } | ||
|
|
||
| if err := good.ConvertFrom(context.Background(), bad); err == nil { | ||
| t.Errorf("ConvertFrom() = %#v, wanted error", good) | ||
| } | ||
| } | ||
|
|
||
| func TestSubscribableStatusConversionBadType(t *testing.T) { | ||
| good, bad := &SubscribableStatus{}, &SubscribableStatus{} | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,13 +39,18 @@ func (source *SubscribableType) ConvertTo(ctx context.Context, obj apis.Converti | |
| } | ||
| } | ||
|
|
||
| // ConvertTo helps implement apis.Convertible | ||
| func (source *SubscribableTypeSpec) ConvertTo(ctx context.Context, sink *duckv1beta1.SubscribableSpec) error { | ||
| if source.Subscribable != nil { | ||
| sink.Subscribers = make([]duckv1beta1.SubscriberSpec, len(source.Subscribable.Subscribers)) | ||
| for i, s := range source.Subscribable.Subscribers { | ||
| s.ConvertTo(ctx, &sink.Subscribers[i]) | ||
| // ConvertTo implements apis.Convertible | ||
| func (source *SubscribableTypeSpec) ConvertTo(ctx context.Context, obj apis.Convertible) error { | ||
| switch sink := obj.(type) { | ||
| case *duckv1beta1.SubscribableSpec: | ||
| if source.Subscribable != nil { | ||
| sink.Subscribers = make([]duckv1beta1.SubscriberSpec, len(source.Subscribable.Subscribers)) | ||
| for i, s := range source.Subscribable.Subscribers { | ||
| s.ConvertTo(ctx, &sink.Subscribers[i]) | ||
| } | ||
| } | ||
| default: | ||
| return fmt.Errorf("unknown version, got: %T", sink) | ||
| } | ||
| return nil | ||
| } | ||
|
|
@@ -98,23 +103,29 @@ func (sink *SubscribableType) ConvertFrom(ctx context.Context, obj apis.Converti | |
| case *duckv1beta1.Subscribable: | ||
| sink.ObjectMeta = source.ObjectMeta | ||
| sink.Status.ConvertFrom(ctx, &source.Status) | ||
| sink.Spec.ConvertFrom(ctx, source.Spec) | ||
| sink.Spec.ConvertFrom(ctx, &source.Spec) | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("unknown version, got: %T", source) | ||
| } | ||
| } | ||
|
|
||
| // ConvertFrom helps implement apis.Convertible | ||
| func (sink *SubscribableTypeSpec) ConvertFrom(ctx context.Context, source duckv1beta1.SubscribableSpec) { | ||
| if len(source.Subscribers) > 0 { | ||
| sink.Subscribable = &Subscribable{ | ||
| Subscribers: make([]SubscriberSpec, len(source.Subscribers)), | ||
| } | ||
| for i, s := range source.Subscribers { | ||
| sink.Subscribable.Subscribers[i].ConvertFrom(ctx, s) | ||
| // ConvertFrom implements apis.Convertible | ||
| func (sink *SubscribableTypeSpec) ConvertFrom(ctx context.Context, obj apis.Convertible) error { | ||
| switch source := obj.(type) { | ||
| case *duckv1beta1.SubscribableSpec: | ||
| if len(source.Subscribers) > 0 { | ||
| sink.Subscribable = &Subscribable{ | ||
| Subscribers: make([]SubscriberSpec, len(source.Subscribers)), | ||
| } | ||
| for i, s := range source.Subscribers { | ||
| sink.Subscribable.Subscribers[i].ConvertFrom(ctx, s) | ||
| } | ||
| } | ||
| default: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you need the conversion to v1 too here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going step by step. In this and 2 more PRs I just make things impelemtn the interface.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok ok |
||
| return fmt.Errorf("unknown version, got: %T", source) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // ConvertFrom helps implement apis.Convertible | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is calling any
Convertiblemethod considered wrong? can't we convert downwards?v1obj.convertTo(v1beta1obj)? and if so, how come thev1alpha1have aconvertFrom? isn't that used for downgrade conversion?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 methods on apis.Convertible:
The code for conversion is kept on the lower version types. It is how it is in all over Knative and I like this practice.
To convert down, you would need to do
v1beta1obj.ConvertFrom(v1obj).if your storage version is v1alpha1, you need to convert v1 object to v1alpha1. That's why that method is needed.