Skip to content

Commit

Permalink
Adding client defaulter for datacontenttype = application/json. (#260)
Browse files Browse the repository at this point in the history
* Adding client defaulter for datacontenttype = application/json.
* updating to go 1.13
* set version once.

Signed-off-by: Scott Nichols <snichols@vmware.com>
  • Loading branch information
n3wscott authored Nov 26, 2019
1 parent 83240f1 commit 2da9d3a
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/golang:1.11.5
- image: circleci/golang:1.13.4

environment:
TEST_RESULTS: /tmp/test-results
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ bazel-*
# Others
## IDE-specific
*.iml

tmp/
9 changes: 5 additions & 4 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ var (

// Client Options

WithEventDefaulter = client.WithEventDefaulter
WithUUIDs = client.WithUUIDs
WithTimeNow = client.WithTimeNow
WithConverterFn = client.WithConverterFn
WithEventDefaulter = client.WithEventDefaulter
WithUUIDs = client.WithUUIDs
WithTimeNow = client.WithTimeNow
WithConverterFn = client.WithConverterFn
WithDataContentType = client.WithDataContentType

// Event Creation

Expand Down
4 changes: 3 additions & 1 deletion cmd/samples/simple/http/sender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ func main() {
}

var version string
switch i % 3 {
switch i % 4 {
case 0:
version = cloudevents.VersionV01
case 1:
version = cloudevents.VersionV02
case 2:
version = cloudevents.VersionV03
case 3:
version = cloudevents.VersionV1
}

event := cloudevents.NewEvent(version)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/nats-io/nats-server/v2 v2.0.0 // indirect
github.com/nats-io/nats.go v1.8.1
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829 // indirect
github.com/stretchr/testify v1.3.0
go.opencensus.io v0.22.0
go.uber.org/atomic v1.4.0 // indirect
Expand All @@ -23,3 +22,5 @@ require (
golang.org/x/sync v0.0.0-20190423024810-112230192c58
pack.ag/amqp v0.11.0
)

go 1.13
7 changes: 4 additions & 3 deletions pkg/cloudevents/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ func New(t transport.Transport, opts ...Option) (Client, error) {
// Transport client. The http transport has had WithBinaryEncoding http
// transport option applied to it. The client will always send Binary
// encoding but will inspect the outbound event context and match the version.
// The WithtimeNow and WithUUIDs client options are also applied to the client,
// all outbound events will have a time and id set if not already present.
// The WithTimeNow, WithUUIDs and WithDataContentType("application/json")
// client options are also applied to the client, all outbound events will have
// a time and id set if not already present.
func NewDefault() (Client, error) {
t, err := http.New(http.WithBinaryEncoding())
if err != nil {
return nil, err
}
c, err := New(t, WithTimeNow(), WithUUIDs())
c, err := New(t, WithTimeNow(), WithUUIDs(), WithDataContentType(cloudevents.ApplicationJSON))
if err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/cloudevents/client/defaulters.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ func DefaultTimeToNowIfNotSet(ctx context.Context, event cloudevents.Event) clou
}
return event
}

// NewDefaultDataContentTypeIfNotSet returns a defaulter that will inspect the
// provided event and set the provided content type if content type is found
// to be empty.
func NewDefaultDataContentTypeIfNotSet(contentType string) EventDefaulter {
return func(ctx context.Context, event cloudevents.Event) cloudevents.Event {
if event.Context != nil {
if event.DataContentType() == "" {
event.SetDataContentType(contentType)
}
}
return event
}
}
182 changes: 93 additions & 89 deletions pkg/cloudevents/client/defaulters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,46 @@ import (
"time"

"github.com/cloudevents/sdk-go/pkg/cloudevents"
"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
"github.com/google/go-cmp/cmp"
)

func TestDefaultIDToUUIDIfNotSet(t *testing.T) {
testCases := map[string]struct {
event cloudevents.Event
}{
"nil context": {
event: cloudevents.Event{},
},
"v0.1 empty": {
event: cloudevents.Event{
Context: &cloudevents.EventContextV01{},
},
},
"v0.2 empty": {
event: cloudevents.Event{
Context: &cloudevents.EventContextV02{},
},
},
"v0.3 empty": {
event: cloudevents.Event{
Context: &cloudevents.EventContextV03{},
},
},
"v0.1 no change": {
event: cloudevents.Event{
Context: cloudevents.EventContextV01{EventID: "abc"}.AsV01(),
},
},
"v0.2 no change": {
event: cloudevents.Event{
Context: cloudevents.EventContextV02{ID: "abc"}.AsV02(),
},
},
"v0.3 no change": {
event: cloudevents.Event{
Context: cloudevents.EventContextV03{ID: "abc"}.AsV03(),
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {

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

if got.Context != nil && got.Context.AsV02().ID == "" {
var versions = []string{"0.1", "0.2", "0.3", "1.0"}

func TestDefaultIDToUUIDIfNotSet_empty(t *testing.T) {
for _, tc := range versions {
t.Run(tc, func(t *testing.T) {
got := DefaultIDToUUIDIfNotSet(context.TODO(), cloudevents.New(tc))

if got.Context != nil && got.ID() == "" {
t.Errorf("failed to generate an id for event")
}
})
}
}

func TestDefaultIDToUUIDIfNotSet_set(t *testing.T) {
for _, tc := range versions {
t.Run(tc, func(t *testing.T) {
event := cloudevents.New(tc)
event.SetID("abc-123")

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

if got.ID() != "abc-123" {
t.Errorf("id was defaulted when already set")
}
})
}
}

func TestDefaultIDToUUIDIfNotSet_nil(t *testing.T) {
got := DefaultIDToUUIDIfNotSet(context.TODO(), cloudevents.Event{})

if got.Context != nil && got.ID() == "" {
t.Errorf("failed to generate time for nil context event")
}
}

func TestDefaultIDToUUIDIfNotSetImmutable(t *testing.T) {
event := cloudevents.Event{
Context: &cloudevents.EventContextV01{},
Expand All @@ -82,56 +68,43 @@ func TestDefaultIDToUUIDIfNotSetImmutable(t *testing.T) {
}
}

func TestDefaultTimeToNowIfNotSet(t *testing.T) {
testCases := map[string]struct {
event cloudevents.Event
}{
"nil context": {
event: cloudevents.Event{},
},
"v0.1 empty": {
event: cloudevents.Event{
Context: &cloudevents.EventContextV01{},
},
},
"v0.2 empty": {
event: cloudevents.Event{
Context: &cloudevents.EventContextV02{},
},
},
"v0.3 empty": {
event: cloudevents.Event{
Context: &cloudevents.EventContextV03{},
},
},
"v0.1 no change": {
event: cloudevents.Event{
Context: cloudevents.EventContextV01{EventTime: &types.Timestamp{Time: time.Now()}}.AsV01(),
},
},
"v0.2 no change": {
event: cloudevents.Event{
Context: cloudevents.EventContextV02{Time: &types.Timestamp{Time: time.Now()}}.AsV02(),
},
},
"v0.3 no change": {
event: cloudevents.Event{
Context: cloudevents.EventContextV03{Time: &types.Timestamp{Time: time.Now()}}.AsV03(),
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {

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

if got.Context != nil && got.Context.AsV02().Time.IsZero() {
func TestDefaultTimeToNowIfNotSet_empty(t *testing.T) {
for _, tc := range versions {
t.Run(tc, func(t *testing.T) {
got := DefaultTimeToNowIfNotSet(context.TODO(), cloudevents.New(tc))

if got.Time().IsZero() {
t.Errorf("failed to generate time for event")
}
})
}
}

func TestDefaultTimeToNowIfNotSet_set(t *testing.T) {
for _, tc := range versions {
t.Run(tc, func(t *testing.T) {
event := cloudevents.New(tc)
now := time.Now()

event.SetTime(now)

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

if !got.Time().Equal(now) {
t.Errorf("time was defaulted when already set")
}
})
}
}

func TestDefaultTimeToNowIfNotSet_nil(t *testing.T) {
got := DefaultTimeToNowIfNotSet(context.TODO(), cloudevents.Event{})

if got.Context != nil && got.Time().IsZero() {
t.Errorf("failed to generate time for nil context event")
}
}

func TestDefaultTimeToNowIfNotSetImmutable(t *testing.T) {
event := cloudevents.Event{
Context: &cloudevents.EventContextV01{},
Expand All @@ -153,3 +126,34 @@ func TestDefaultTimeToNowIfNotSetImmutable(t *testing.T) {
t.Errorf("failed to generate a time for event")
}
}

func TestNewDefaultDataContentTypeIfNotSet_empty(t *testing.T) {
ct := "a/b"
for _, tc := range versions {
t.Run(tc, func(t *testing.T) {
fn := NewDefaultDataContentTypeIfNotSet(ct)
got := fn(context.TODO(), cloudevents.New(tc))

if got.DataContentType() != ct {
t.Errorf("failed to default data content type for event")
}
})
}
}

func TestNewDefaultDataContentTypeIfNotSet_set(t *testing.T) {
ct := "a/b"
for _, tc := range versions {
t.Run(tc, func(t *testing.T) {
event := cloudevents.New(tc)
event.SetDataContentType(ct)

fn := NewDefaultDataContentTypeIfNotSet("b/c")
got := fn(context.TODO(), event)

if got.DataContentType() != ct {
t.Errorf("failed to preserve data content type for event")
}
})
}
}
10 changes: 10 additions & 0 deletions pkg/cloudevents/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ func WithUUIDs() Option {
}
}

// WithDataContentType adds the resulting defaulter from
// NewDefaultDataContentTypeIfNotSet event defaulter to the end of the
// defaulter chain.
func WithDataContentType(contentType string) Option {
return func(c *ceClient) error {
c.eventDefaulterFns = append(c.eventDefaulterFns, NewDefaultDataContentTypeIfNotSet(contentType))
return nil
}
}

// WithTimeNow adds DefaultTimeToNowIfNotSet event defaulter to the end of the
// defaulter chain.
func WithTimeNow() Option {
Expand Down

0 comments on commit 2da9d3a

Please sign in to comment.