Skip to content

Commit

Permalink
starting to add context for 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Nichols committed Aug 18, 2019
1 parent 7ef8ff8 commit 78a9ced
Show file tree
Hide file tree
Showing 18 changed files with 368 additions and 34 deletions.
1 change: 1 addition & 0 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const (
VersionV01 = cloudevents.CloudEventsVersionV01
VersionV02 = cloudevents.CloudEventsVersionV02
VersionV03 = cloudevents.CloudEventsVersionV03
VersionV04 = cloudevents.CloudEventsVersionV04

// HTTP Transport Encodings

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Azure/go-autorest/autorest/to v0.2.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.1.0 // indirect
github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/gogo/protobuf v1.2.0 // indirect
github.com/google/go-cmp v0.3.0
github.com/google/uuid v1.1.1
github.com/kelseyhightower/envconfig v1.4.0
Expand Down
10 changes: 5 additions & 5 deletions pkg/cloudevents/event_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type EventReader interface {
ID() string
// Time returns event.Context.GetTime().
Time() time.Time
// SchemaURL returns event.Context.GetSchemaURL().
SchemaURL() string
// DataSchema returns event.Context.GetDataSchema().
DataSchema() string
// DataContentType returns event.Context.GetDataContentType().
DataContentType() string
// DataMediaType returns event.Context.GetDataMediaType().
Expand Down Expand Up @@ -58,8 +58,8 @@ type EventWriter interface {
SetID(string)
// SetTime performs event.Context.SetTime.
SetTime(time.Time)
// SetSchemaURL performs event.Context.SetSchemaURL.
SetSchemaURL(string)
// SetDataSchema performs event.Context.SetDataSchema.
SetDataSchema(string)
// SetDataContentType performs event.Context.SetDataContentType.
SetDataContentType(string)
// SetDataContentEncoding performs event.Context.SetDataContentEncoding.
Expand All @@ -68,7 +68,7 @@ type EventWriter interface {
// Extension Attributes

// SetExtension performs event.Context.SetExtension.
SetExtension(string, interface{})
SetExtension(string, interface{}) // TODO: this needs to move to just string.

// SetData encodes the given payload with the current encoding settings.
SetData(interface{}) error
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/event_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (e Event) Time() time.Time {
return time.Time{}
}

// SchemaURL implements EventReader.SchemaURL
func (e Event) SchemaURL() string {
// DataSchema implements EventReader.DataSchema
func (e Event) DataSchema() string {
if e.Context != nil {
return e.Context.GetSchemaURL()
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cloudevents/event_reader_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,23 +412,23 @@ func TestEventRW_SchemaURL(t *testing.T) {
"nilled v01": {
event: func() ce.Event {
e := ce.New("0.1")
e.SetSchemaURL("should nil")
e.SetDataSchema("should nil")
return e
}(),
want: "",
},
"nilled v02": {
event: func() ce.Event {
e := ce.New("0.2")
e.SetSchemaURL("should nil")
e.SetDataSchema("should nil")
return e
}(),
want: "",
},
"nilled v03": {
event: func() ce.Event {
e := ce.New("0.3")
e.SetSchemaURL("should nil")
e.SetDataSchema("should nil")
return e
}(),
want: "",
Expand All @@ -446,8 +446,8 @@ func TestEventRW_SchemaURL(t *testing.T) {
validateReaderWriter(t, tc, got, err)
}()

tc.event.SetSchemaURL(tc.set)
got = tc.event.SchemaURL()
tc.event.SetDataSchema(tc.set)
got = tc.event.DataSchema()
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestSchemaURL(t *testing.T) {
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {

got := tc.event.SchemaURL()
got := tc.event.DataSchema()

if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("unexpected (-want, +got) = %v", diff)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudevents/event_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (e *Event) SetTime(t time.Time) {
}
}

// SetSchemaURL implements EventWriter.SetSchemaURL
func (e *Event) SetSchemaURL(s string) {
// SetDataSchema implements EventWriter.SetDataSchema
func (e *Event) SetDataSchema(s string) {
if err := e.Context.SetSchemaURL(s); err != nil {
panic(err)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/cloudevents/eventcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type EventContextWriter interface {
SetID(string) error
// SetTime sets the time of the context.
SetTime(time time.Time) error
// SetSchemaURL sets the schema url of the context.
// SetDataSchema sets the schema url of the context.
SetSchemaURL(string) error
// SetDataContentType sets the data content type of the context.
SetDataContentType(string) error
Expand All @@ -68,6 +68,8 @@ type EventContextWriter interface {
SetExtension(string, interface{}) error
}

// EventContextConverter are the methods that allow for event version
// conversion.
type EventContextConverter interface {
// AsV01 provides a translation from whatever the "native" encoding of the
// CloudEvent was to the equivalent in v0.1 field names, moving fields to or
Expand All @@ -83,6 +85,11 @@ type EventContextConverter interface {
// CloudEvent was to the equivalent in v0.3 field names, moving fields to or
// from extensions as necessary.
AsV03() *EventContextV03

// AsV04 provides a translation from whatever the "native" encoding of the
// CloudEvent was to the equivalent in v0.4 field names, moving fields to or
// from extensions as necessary.
AsV04() *EventContextV04
}

// EventContext is conical interface for a CloudEvents Context.
Expand Down
8 changes: 6 additions & 2 deletions pkg/cloudevents/eventcontext_v01.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ func (ec EventContextV01) AsV02() *EventContextV02 {

// AsV03 implements EventContextConverter.AsV03
func (ec EventContextV01) AsV03() *EventContextV03 {
ecv2 := ec.AsV02()
return ecv2.AsV03()
return ec.AsV02().AsV03()
}

// AsV04 implements EventContextConverter.AsV04
func (ec EventContextV01) AsV04() *EventContextV04 {
return ec.AsV02().AsV03().AsV04()
}

// Validate returns errors based on requirements from the CloudEvents spec.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/eventcontext_v01_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (ec *EventContextV01) SetTime(t time.Time) error {
return nil
}

// SetSchemaURL implements EventContextWriter.SetSchemaURL
// SetDataSchema implements EventContextWriter.SetDataSchema
func (ec *EventContextV01) SetSchemaURL(u string) error {
u = strings.TrimSpace(u)
if u == "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/eventcontext_v02_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (ec *EventContextV02) SetTime(t time.Time) error {
return nil
}

// SetSchemaURL implements EventContextWriter.SetSchemaURL
// SetDataSchema implements EventContextWriter.SetDataSchema
func (ec *EventContextV02) SetSchemaURL(u string) error {
u = strings.TrimSpace(u)
if u == "" {
Expand Down
29 changes: 26 additions & 3 deletions pkg/cloudevents/eventcontext_v03.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
)

// WIP: AS OF FEB 19, 2019

const (
// CloudEventsVersionV03 represents the version 0.3 of the CloudEvents spec.
CloudEventsVersionV03 = "0.3"
Expand All @@ -32,7 +30,7 @@ type EventContextV03 struct {
ID string `json:"id"`
// Time - A Timestamp when the event happened.
Time *types.Timestamp `json:"time,omitempty"`
// SchemaURL - A link to the schema that the `data` attribute adheres to.
// DataSchema - A link to the schema that the `data` attribute adheres to.
SchemaURL *types.URLRef `json:"schemaurl,omitempty"`
// GetDataMediaType - A MIME (RFC2046) string describing the media type of `data`.
// TODO: Should an empty string assume `application/json`, `application/octet-stream`, or auto-detect the content?
Expand Down Expand Up @@ -138,6 +136,31 @@ func (ec EventContextV03) AsV03() *EventContextV03 {
return &ec
}

// AsV04 implements EventContextConverter.AsV04
func (ec EventContextV03) AsV04() *EventContextV04 {
ret := EventContextV04{
SpecVersion: CloudEventsVersionV02,
ID: ec.ID,
Time: ec.Time,
Type: ec.Type,
DataSchema: ec.SchemaURL,
DataContentType: ec.DataContentType,
DataContentEncoding: ec.DataContentEncoding,
Source: ec.Source,
Subject: ec.Subject,
Extensions: make(map[string]string),
}
if ec.Extensions != nil {
for k, v := range ec.Extensions {
ret.Extensions[k] = fmt.Sprintf("%v", v) // TODO: This is wrong. Follow up with what should be done.
}
}
if len(ret.Extensions) == 0 {
ret.Extensions = nil
}
return &ret
}

// Validate returns errors based on requirements from the CloudEvents spec.
// For more details, see https://github.com/cloudevents/spec/blob/master/spec.md
// As of Feb 26, 2019, commit 17c32ea26baf7714ad027d9917d03d2fff79fc7e
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/eventcontext_v03_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (ec *EventContextV03) SetTime(t time.Time) error {
return nil
}

// SetSchemaURL implements EventContextWriter.SetSchemaURL
// SetDataSchema implements EventContextWriter.SetDataSchema
func (ec *EventContextV03) SetSchemaURL(u string) error {
u = strings.TrimSpace(u)
if u == "" {
Expand Down
Loading

0 comments on commit 78a9ced

Please sign in to comment.