Skip to content

Commit

Permalink
Make SubscriberSpec implement apis.Convertible (#3473)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliok authored Jul 6, 2020
1 parent 190f477 commit 4912b7e
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 47 deletions.
2 changes: 2 additions & 0 deletions pkg/apis/duck/v1/subscribable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ var (
_ apis.Convertible = (*Subscribable)(nil)
_ apis.Convertible = (*SubscribableSpec)(nil)
_ apis.Convertible = (*SubscribableStatus)(nil)

_ apis.Convertible = (*SubscriberSpec)(nil)
)

// GetFullType implements duck.Implementable
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/duck/v1/subscribable_types_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ func (source *SubscribableStatus) ConvertTo(ctx context.Context, sink apis.Conve
func (sink *SubscribableStatus) ConvertFrom(ctx context.Context, source apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", source)
}

// ConvertTo implements apis.Convertible
func (source *SubscriberSpec) ConvertTo(ctx context.Context, sink apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", sink)
}

// ConvertFrom implements apis.Convertible
func (sink *SubscriberSpec) ConvertFrom(ctx context.Context, source apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", source)
}
12 changes: 12 additions & 0 deletions pkg/apis/duck/v1/subscribable_types_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ func TestSubscribableStatusConversionBadType(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestSubscriberSpecConversionBadType(t *testing.T) {
good, bad := &SubscriberSpec{}, &SubscriberSpec{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}
2 changes: 2 additions & 0 deletions pkg/apis/duck/v1alpha1/subscribable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ var (

_ apis.Convertible = (*SubscribableTypeSpec)(nil)
_ apis.Convertible = (*SubscribableTypeStatus)(nil)

_ apis.Convertible = (*SubscriberSpec)(nil)
)

// GetSubscribableTypeStatus method Returns the Default SubscribableStatus in this case it's SubscribableStatus
Expand Down
68 changes: 40 additions & 28 deletions pkg/apis/duck/v1alpha1/subscribable_types_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,30 @@ func (source *SubscribableTypeSpec) ConvertTo(ctx context.Context, obj apis.Conv
return nil
}

func (source *SubscriberSpec) ConvertTo(ctx context.Context, sink *duckv1beta1.SubscriberSpec) {
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
sink.ReplyURI = source.ReplyURI

if source.Delivery != nil {
sink.Delivery = source.Delivery
} else {
// If however, there's a Deprecated DeadLetterSinkURI, convert that up
// to DeliverySpec.
sink.Delivery = &duckv1beta1.DeliverySpec{
DeadLetterSink: &duckv1.Destination{
URI: source.DeadLetterSinkURI,
},
// ConvertTo implements apis.Convertible
func (source *SubscriberSpec) ConvertTo(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *duckv1beta1.SubscriberSpec:
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
sink.ReplyURI = source.ReplyURI

if source.Delivery != nil {
sink.Delivery = source.Delivery
} else {
// If however, there's a Deprecated DeadLetterSinkURI, convert that up
// to DeliverySpec.
sink.Delivery = &duckv1beta1.DeliverySpec{
DeadLetterSink: &duckv1.Destination{
URI: source.DeadLetterSinkURI,
},
}
}
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
return nil
}

// ConvertTo implements apis.Convertible
Expand Down Expand Up @@ -119,7 +126,7 @@ func (sink *SubscribableTypeSpec) ConvertFrom(ctx context.Context, obj apis.Conv
Subscribers: make([]SubscriberSpec, len(source.Subscribers)),
}
for i, s := range source.Subscribers {
sink.Subscribable.Subscribers[i].ConvertFrom(ctx, s)
sink.Subscribable.Subscribers[i].ConvertFrom(ctx, &s)
}
}
default:
Expand All @@ -128,19 +135,24 @@ func (sink *SubscribableTypeSpec) ConvertFrom(ctx context.Context, obj apis.Conv
return nil
}

// ConvertFrom helps implement apis.Convertible
func (sink *SubscriberSpec) ConvertFrom(ctx context.Context, source duckv1beta1.SubscriberSpec) {
var deadLetterSinkURI *apis.URL
if source.Delivery != nil && source.Delivery.DeadLetterSink != nil {
deadLetterSinkURI = source.Delivery.DeadLetterSink.URI
// ConvertFrom implements apis.Convertible
func (sink *SubscriberSpec) ConvertFrom(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *duckv1beta1.SubscriberSpec:
var deadLetterSinkURI *apis.URL
if source.Delivery != nil && source.Delivery.DeadLetterSink != nil {
deadLetterSinkURI = source.Delivery.DeadLetterSink.URI
}
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
sink.ReplyURI = source.ReplyURI
sink.Delivery = source.Delivery
sink.DeadLetterSinkURI = deadLetterSinkURI
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
sink.ReplyURI = source.ReplyURI
sink.Delivery = source.Delivery
sink.DeadLetterSinkURI = deadLetterSinkURI

return nil
}

// ConvertFrom implements apis.Convertible
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/duck/v1alpha1/subscribable_types_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,15 @@ func TestSubscribableTypeStatusConversionBadType(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestSubscriberSpecStatusConversionBadType(t *testing.T) {
good, bad := &SubscriberSpec{}, &SubscriberSpec{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}
2 changes: 2 additions & 0 deletions pkg/apis/duck/v1beta1/subscribable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ var (
_ apis.Convertible = (*Subscribable)(nil)
_ apis.Convertible = (*SubscribableSpec)(nil)
_ apis.Convertible = (*SubscribableStatus)(nil)

_ apis.Convertible = (*SubscriberSpec)(nil)
)

// GetFullType implements duck.Implementable
Expand Down
48 changes: 29 additions & 19 deletions pkg/apis/duck/v1beta1/subscribable_types_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ func (source *SubscribableSpec) ConvertTo(ctx context.Context, obj apis.Converti
return nil
}

// ConvertTo helps implement apis.Convertible
func (source *SubscriberSpec) ConvertTo(ctx context.Context, sink *eventingduckv1.SubscriberSpec) error {
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
if source.Delivery != nil {
sink.Delivery = &eventingduckv1.DeliverySpec{}
if err := source.Delivery.ConvertTo(ctx, sink.Delivery); err != nil {
return err
// ConvertTo implements apis.Convertible
func (source *SubscriberSpec) ConvertTo(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *eventingduckv1.SubscriberSpec:
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
if source.Delivery != nil {
sink.Delivery = &eventingduckv1.DeliverySpec{}
if err := source.Delivery.ConvertTo(ctx, sink.Delivery); err != nil {
return err
}
}
sink.ReplyURI = source.ReplyURI
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
sink.ReplyURI = source.ReplyURI
return nil
}

Expand Down Expand Up @@ -109,7 +114,7 @@ func (sink *SubscribableSpec) ConvertFrom(ctx context.Context, obj apis.Converti
if len(source.Subscribers) > 0 {
sink.Subscribers = make([]SubscriberSpec, len(source.Subscribers))
for i, s := range source.Subscribers {
if err := sink.Subscribers[i].ConvertFrom(ctx, s); err != nil {
if err := sink.Subscribers[i].ConvertFrom(ctx, &s); err != nil {
return err
}
}
Expand All @@ -121,14 +126,19 @@ func (sink *SubscribableSpec) ConvertFrom(ctx context.Context, obj apis.Converti
}

// ConvertFrom helps implement apis.Convertible
func (sink *SubscriberSpec) ConvertFrom(ctx context.Context, source eventingduckv1.SubscriberSpec) error {
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
sink.ReplyURI = source.ReplyURI
if source.Delivery != nil {
sink.Delivery = &DeliverySpec{}
return sink.Delivery.ConvertFrom(ctx, source.Delivery)
func (sink *SubscriberSpec) ConvertFrom(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *eventingduckv1.SubscriberSpec:
sink.UID = source.UID
sink.Generation = source.Generation
sink.SubscriberURI = source.SubscriberURI
sink.ReplyURI = source.ReplyURI
if source.Delivery != nil {
sink.Delivery = &DeliverySpec{}
return sink.Delivery.ConvertFrom(ctx, source.Delivery)
}
default:
return fmt.Errorf("unknown version, got: %T", source)
}
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/duck/v1beta1/subscribable_types_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,15 @@ func TestSubscribableStatusConversionBadType(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestSubscriberSpecConversionBadType(t *testing.T) {
good, bad := &SubscriberSpec{}, &SubscriberSpec{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

0 comments on commit 4912b7e

Please sign in to comment.