Skip to content

Commit

Permalink
Wire up context to event defaults. (cloudevents#172)
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Nichols <nicholss@google.com>
  • Loading branch information
n3wscott authored and markpeek committed Aug 16, 2019
1 parent 70bc5a5 commit 7ef8ff8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/cloudevents/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/cloudevents/client/defaulters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"time"

"github.com/cloudevents/sdk-go/pkg/cloudevents"
Expand All @@ -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()
Expand All @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions pkg/cloudevents/client/defaulters_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -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")
Expand All @@ -64,7 +65,7 @@ func TestDefaultIDToUUIDIfNotSetImmutable(t *testing.T) {
Context: &cloudevents.EventContextV01{},
}

got := DefaultIDToUUIDIfNotSet(event)
got := DefaultIDToUUIDIfNotSet(context.TODO(), event)

want := "0.1"

Expand Down Expand Up @@ -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")
Expand All @@ -136,7 +137,7 @@ func TestDefaultTimeToNowIfNotSetImmutable(t *testing.T) {
Context: &cloudevents.EventContextV01{},
}

got := DefaultTimeToNowIfNotSet(event)
got := DefaultTimeToNowIfNotSet(context.TODO(), event)

want := "0.1"

Expand Down
7 changes: 4 additions & 3 deletions pkg/cloudevents/client/options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"testing"

"github.com/cloudevents/sdk-go/pkg/cloudevents"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/http/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 7ef8ff8

Please sign in to comment.