Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace time.Time.String() with time.Time.Format() #1017

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions test/integration/http/direct_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
},
},
Expand Down
5 changes: 3 additions & 2 deletions v2/event/event_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"strings"
"time"

jsoniter "github.com/json-iterator/go"
)
Expand Down Expand Up @@ -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))
Comment on lines -79 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran an example w/ Go 1.22 (same w/ Go 1.21) here: https://go.dev/play/p/NcMTqPu75fe

And I get:

2009-11-10 23:00:00 +0000 UTC m=+0.000000001
2009-11-10T23:00:00Z

Which could be considered a breaking change. WDYT?

cc/ @duglin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the breaking change. I'm more bothered that:
1 - it wasn't spec compliant before and no one noticed
2 - not a single testcase needed to be changed

I agree with splitting the upgrade stuff into a separate PR. But can we also get at least one testcase added that checks the format of the time attribute and that it adheres to the CE spec (RFC3339) ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll get to work on it! Thank you so much for the feedback!

Copy link
Author

@MaryamTaj MaryamTaj Feb 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up the PR. Currently working on test cases:)

}
case *EventContextV1:
// Set a bunch of variables we need later
Expand Down Expand Up @@ -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")
Expand Down