From 7ef8ff8ceb8504ee083f4364008e8b2f2c8b1741 Mon Sep 17 00:00:00 2001 From: Scott Nichols <32305648+n3wscott@users.noreply.github.com> Date: Fri, 16 Aug 2019 15:52:55 -0700 Subject: [PATCH] Wire up context to event defaults. (#172) Signed-off-by: Scott Nichols --- pkg/cloudevents/client/client.go | 4 ++-- pkg/cloudevents/client/defaulters.go | 7 ++++--- pkg/cloudevents/client/defaulters_test.go | 9 +++++---- pkg/cloudevents/client/options_test.go | 7 ++++--- test/http/loopback.go | 2 +- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pkg/cloudevents/client/client.go b/pkg/cloudevents/client/client.go index 2a6ac768b..29cbe90ac 100644 --- a/pkg/cloudevents/client/client.go +++ b/pkg/cloudevents/client/client.go @@ -102,7 +102,7 @@ func (c *ceClient) obsSend(ctx context.Context, event cloudevents.Event) (*cloud // Apply the defaulter chain to the incoming event. if len(c.eventDefaulterFns) > 0 { for _, fn := range c.eventDefaulterFns { - event = fn(event) + event = fn(ctx, event) } } @@ -139,7 +139,7 @@ func (c *ceClient) obsReceive(ctx context.Context, event cloudevents.Event, resp // Apply the defaulter chain to the outgoing event. if err == nil && resp != nil && resp.Event != nil && len(c.eventDefaulterFns) > 0 { for _, fn := range c.eventDefaulterFns { - *resp.Event = fn(*resp.Event) + *resp.Event = fn(ctx, *resp.Event) } // Validate the event conforms to the CloudEvents Spec. if err := resp.Event.Validate(); err != nil { diff --git a/pkg/cloudevents/client/defaulters.go b/pkg/cloudevents/client/defaulters.go index 762febdd8..40bd85a9c 100644 --- a/pkg/cloudevents/client/defaulters.go +++ b/pkg/cloudevents/client/defaulters.go @@ -1,6 +1,7 @@ package client import ( + "context" "time" "github.com/cloudevents/sdk-go/pkg/cloudevents" @@ -9,11 +10,11 @@ import ( // EventDefaulter is the function signature for extensions that are able // to perform event defaulting. -type EventDefaulter func(event cloudevents.Event) cloudevents.Event +type EventDefaulter func(ctx context.Context, event cloudevents.Event) cloudevents.Event // DefaultIDToUUIDIfNotSet will inspect the provided event and assign a UUID to // context.ID if it is found to be empty. -func DefaultIDToUUIDIfNotSet(event cloudevents.Event) cloudevents.Event { +func DefaultIDToUUIDIfNotSet(ctx context.Context, event cloudevents.Event) cloudevents.Event { if event.Context != nil { if event.ID() == "" { event.Context = event.Context.Clone() @@ -25,7 +26,7 @@ func DefaultIDToUUIDIfNotSet(event cloudevents.Event) cloudevents.Event { // DefaultTimeToNowIfNotSet will inspect the provided event and assign a new // Timestamp to context.Time if it is found to be nil or zero. -func DefaultTimeToNowIfNotSet(event cloudevents.Event) cloudevents.Event { +func DefaultTimeToNowIfNotSet(ctx context.Context, event cloudevents.Event) cloudevents.Event { if event.Context != nil { if event.Time().IsZero() { event.Context = event.Context.Clone() diff --git a/pkg/cloudevents/client/defaulters_test.go b/pkg/cloudevents/client/defaulters_test.go index f798b107c..ea9f75989 100644 --- a/pkg/cloudevents/client/defaulters_test.go +++ b/pkg/cloudevents/client/defaulters_test.go @@ -1,6 +1,7 @@ package client import ( + "context" "testing" "time" @@ -50,7 +51,7 @@ func TestDefaultIDToUUIDIfNotSet(t *testing.T) { for n, tc := range testCases { t.Run(n, func(t *testing.T) { - got := DefaultIDToUUIDIfNotSet(tc.event) + got := DefaultIDToUUIDIfNotSet(context.TODO(), tc.event) if got.Context != nil && got.Context.AsV02().ID == "" { t.Errorf("failed to generate an id for event") @@ -64,7 +65,7 @@ func TestDefaultIDToUUIDIfNotSetImmutable(t *testing.T) { Context: &cloudevents.EventContextV01{}, } - got := DefaultIDToUUIDIfNotSet(event) + got := DefaultIDToUUIDIfNotSet(context.TODO(), event) want := "0.1" @@ -122,7 +123,7 @@ func TestDefaultTimeToNowIfNotSet(t *testing.T) { for n, tc := range testCases { t.Run(n, func(t *testing.T) { - got := DefaultTimeToNowIfNotSet(tc.event) + got := DefaultTimeToNowIfNotSet(context.TODO(), tc.event) if got.Context != nil && got.Context.AsV02().Time.IsZero() { t.Errorf("failed to generate time for event") @@ -136,7 +137,7 @@ func TestDefaultTimeToNowIfNotSetImmutable(t *testing.T) { Context: &cloudevents.EventContextV01{}, } - got := DefaultTimeToNowIfNotSet(event) + got := DefaultTimeToNowIfNotSet(context.TODO(), event) want := "0.1" diff --git a/pkg/cloudevents/client/options_test.go b/pkg/cloudevents/client/options_test.go index 2ca5b06b1..61cb49976 100644 --- a/pkg/cloudevents/client/options_test.go +++ b/pkg/cloudevents/client/options_test.go @@ -1,6 +1,7 @@ package client import ( + "context" "testing" "github.com/cloudevents/sdk-go/pkg/cloudevents" @@ -9,17 +10,17 @@ import ( func TestWithEventDefaulter(t *testing.T) { - v1 := func(event cloudevents.Event) cloudevents.Event { + v1 := func(ctx context.Context, event cloudevents.Event) cloudevents.Event { event.Context = event.Context.AsV01() return event } - v2 := func(event cloudevents.Event) cloudevents.Event { + v2 := func(ctx context.Context, event cloudevents.Event) cloudevents.Event { event.Context = event.Context.AsV02() return event } - v3 := func(event cloudevents.Event) cloudevents.Event { + v3 := func(ctx context.Context, event cloudevents.Event) cloudevents.Event { event.Context = event.Context.AsV03() return event } diff --git a/test/http/loopback.go b/test/http/loopback.go index f3f73da47..391e743fa 100644 --- a/test/http/loopback.go +++ b/test/http/loopback.go @@ -21,7 +21,7 @@ import ( // Client is a set to binary or func AlwaysThen(then time.Time) client.EventDefaulter { - return func(event cloudevents.Event) cloudevents.Event { + return func(ctx context.Context, event cloudevents.Event) cloudevents.Event { if event.Context != nil { switch event.Context.GetSpecVersion() { case "0.1":