From 16b701e5b9f808e7db12c7a7f0c34d1c7338f9c4 Mon Sep 17 00:00:00 2001 From: MaryamTaj Date: Sat, 17 Feb 2024 11:22:23 -0500 Subject: [PATCH 1/2] BREAKING CHANGE:Serializing time to RFC-3339 Signed-off-by: MaryamTaj --- test/integration/http/direct_v1_test.go | 11 +++++------ v2/event/event_marshal.go | 5 +++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/integration/http/direct_v1_test.go b/test/integration/http/direct_v1_test.go index 9b8253ce0..e597c2104 100644 --- a/test/integration/http/direct_v1_test.go +++ b/test/integration/http/direct_v1_test.go @@ -13,7 +13,7 @@ import ( "time" cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/cloudevents/sdk-go/v2/types" + // "github.com/cloudevents/sdk-go/v2/types" ) func TestSenderReceiver_binary_v1(t *testing.T) { @@ -101,7 +101,6 @@ func TestSenderReceiver_binary_v1(t *testing.T) { func TestSenderReceiver_structured_v1(t *testing.T) { now := time.Now() - testCases := DirectTapTestCases{ "Structured v1.0": { now: now, @@ -118,7 +117,7 @@ func TestSenderReceiver_structured_v1(t *testing.T) { Context: cloudevents.EventContextV1{ ID: "ABC-123", Type: "unit.test.client.sent", - Time: &cloudevents.Timestamp{Time: now}, + Time: &cloudevents.Timestamp{Time: now.Truncate(time.Second)}, Source: *cloudevents.ParseURIRef("/unit/test/client"), Subject: strptr("resource"), DataContentType: cloudevents.StringOfApplicationJSON(), @@ -131,7 +130,7 @@ func TestSenderReceiver_structured_v1(t *testing.T) { Header: map[string][]string{ "content-type": {"application/cloudevents+json"}, }, - Body: fmt.Sprintf(`{"data":{"hello":"unittest"},"id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, types.FormatTime(now.UTC())), + Body: fmt.Sprintf(`{"data":{"hello":"unittest"},"id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.Truncate(time.Second).Format(time.RFC3339)), ContentLength: 182, }, }, @@ -164,7 +163,7 @@ func TestSenderReceiver_data_base64_v1(t *testing.T) { Context: cloudevents.EventContextV1{ ID: "ABC-123", Type: "unit.test.client.sent", - Time: &cloudevents.Timestamp{Time: now}, + Time: &cloudevents.Timestamp{Time: now.Truncate(time.Second)}, Source: *cloudevents.ParseURIRef("/unit/test/client"), Subject: strptr("resource"), DataContentType: cloudevents.StringOfTextPlain(), @@ -177,7 +176,7 @@ func TestSenderReceiver_data_base64_v1(t *testing.T) { Header: map[string][]string{ "content-type": {"application/cloudevents+json"}, }, - Body: fmt.Sprintf(`{"data_base64":"aGVsbG86IHVuaXR0ZXN0","id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.UTC().Format(time.RFC3339Nano)), + Body: fmt.Sprintf(`{"data_base64":"aGVsbG86IHVuaXR0ZXN0","id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.Truncate(time.Second).Format(time.RFC3339)), ContentLength: 191, }, }, diff --git a/v2/event/event_marshal.go b/v2/event/event_marshal.go index c5f2dc03c..473b8d66a 100644 --- a/v2/event/event_marshal.go +++ b/v2/event/event_marshal.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "strings" + "time" jsoniter "github.com/json-iterator/go" ) @@ -76,7 +77,7 @@ func WriteJson(in *Event, writer io.Writer) error { if eventContext.Time != nil { stream.WriteMore() stream.WriteObjectField("time") - stream.WriteString(eventContext.Time.String()) + stream.WriteString(eventContext.Time.Format(time.RFC3339)) } case *EventContextV1: // Set a bunch of variables we need later @@ -120,7 +121,7 @@ func WriteJson(in *Event, writer io.Writer) error { if eventContext.Time != nil { stream.WriteMore() stream.WriteObjectField("time") - stream.WriteString(eventContext.Time.String()) + stream.WriteString(eventContext.Time.Format(time.RFC3339)) } default: return fmt.Errorf("missing event context") From 6d246124bc77ace804b1f44e5bb402b466c02e2a Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Wed, 23 Oct 2024 16:39:40 +0000 Subject: [PATCH 2/2] don't truncate to seconds Signed-off-by: Doug Davis --- test/integration/http/direct_v1_test.go | 8 ++++---- v2/event/event_marshal.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/integration/http/direct_v1_test.go b/test/integration/http/direct_v1_test.go index e597c2104..fd096344d 100644 --- a/test/integration/http/direct_v1_test.go +++ b/test/integration/http/direct_v1_test.go @@ -117,7 +117,7 @@ func TestSenderReceiver_structured_v1(t *testing.T) { Context: cloudevents.EventContextV1{ ID: "ABC-123", Type: "unit.test.client.sent", - Time: &cloudevents.Timestamp{Time: now.Truncate(time.Second)}, + Time: &cloudevents.Timestamp{Time: now}, Source: *cloudevents.ParseURIRef("/unit/test/client"), Subject: strptr("resource"), DataContentType: cloudevents.StringOfApplicationJSON(), @@ -130,7 +130,7 @@ func TestSenderReceiver_structured_v1(t *testing.T) { Header: map[string][]string{ "content-type": {"application/cloudevents+json"}, }, - Body: fmt.Sprintf(`{"data":{"hello":"unittest"},"id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.Truncate(time.Second).Format(time.RFC3339)), + Body: fmt.Sprintf(`{"data":{"hello":"unittest"},"id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.Format(time.RFC3339)), ContentLength: 182, }, }, @@ -163,7 +163,7 @@ func TestSenderReceiver_data_base64_v1(t *testing.T) { Context: cloudevents.EventContextV1{ ID: "ABC-123", Type: "unit.test.client.sent", - Time: &cloudevents.Timestamp{Time: now.Truncate(time.Second)}, + Time: &cloudevents.Timestamp{Time: now}, Source: *cloudevents.ParseURIRef("/unit/test/client"), Subject: strptr("resource"), DataContentType: cloudevents.StringOfTextPlain(), @@ -176,7 +176,7 @@ func TestSenderReceiver_data_base64_v1(t *testing.T) { Header: map[string][]string{ "content-type": {"application/cloudevents+json"}, }, - Body: fmt.Sprintf(`{"data_base64":"aGVsbG86IHVuaXR0ZXN0","id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.Truncate(time.Second).Format(time.RFC3339)), + Body: fmt.Sprintf(`{"data_base64":"aGVsbG86IHVuaXR0ZXN0","id":"ABC-123","source":"/unit/test/client","specversion":"1.0","subject":"resource","time":%q,"type":"unit.test.client.sent"}`, now.Format(time.RFC3339)), ContentLength: 191, }, }, diff --git a/v2/event/event_marshal.go b/v2/event/event_marshal.go index 473b8d66a..6efe97239 100644 --- a/v2/event/event_marshal.go +++ b/v2/event/event_marshal.go @@ -77,7 +77,7 @@ func WriteJson(in *Event, writer io.Writer) error { if eventContext.Time != nil { stream.WriteMore() stream.WriteObjectField("time") - stream.WriteString(eventContext.Time.Format(time.RFC3339)) + stream.WriteString(eventContext.Time.Format(time.RFC3339Nano)) } case *EventContextV1: // Set a bunch of variables we need later @@ -121,7 +121,7 @@ func WriteJson(in *Event, writer io.Writer) error { if eventContext.Time != nil { stream.WriteMore() stream.WriteObjectField("time") - stream.WriteString(eventContext.Time.Format(time.RFC3339)) + stream.WriteString(eventContext.Time.Format(time.RFC3339Nano)) } default: return fmt.Errorf("missing event context")