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

Ignore EventTime if it's not present #492

Merged
merged 2 commits into from
Jul 25, 2018
Merged

Ignore EventTime if it's not present #492

merged 2 commits into from
Jul 25, 2018

Conversation

mthenw
Copy link
Contributor

@mthenw mthenw commented Jul 25, 2018

What did you implement:

This PR fixes parsing CloudEvent EventTime field. If EventTime is not preset it will not fill it with default value (now EventTime is pointer to time.Time)

It also makes consistent parsing functions (parsing in binary mode should also return error if it's impossible to parse time).

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: Yes
Is it a breaking change?: NO

@mthenw mthenw requested a review from sebito91 July 25, 2018 09:25
@@ -36,6 +38,9 @@ func TestNew_Encoding(t *testing.T) {
}

func TestFromRequest(t *testing.T) {
patch := monkey.Patch(time.Now, func() time.Time { return testTime })
Copy link
Contributor Author

@mthenw mthenw Jul 25, 2018

Choose a reason for hiding this comment

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

I'm not sure convinced to this library but it works so it's kinda OK. And I still think it's better than abusing dependency injection pattern.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've never used it before myself, but ok! Wonder if we can just have a mock function to return that, like in the github.com/stretchr/testify/mock package?

In any case, if this works then I would leave it for now!

@codecov
Copy link

codecov bot commented Jul 25, 2018

Codecov Report

Merging #492 into master will increase coverage by 0.2%.
The diff coverage is 77.77%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #492     +/-   ##
========================================
+ Coverage      71%   71.2%   +0.2%     
========================================
  Files          37      37             
  Lines        2190    2195      +5     
========================================
+ Hits         1555    1563      +8     
+ Misses        572     571      -1     
+ Partials       63      61      -2
Impacted Files Coverage Δ
event/event.go 81.2% <77.77%> (+3.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08b8053...4dd7d7a. Read the comment docs.

Copy link
Contributor

@sebito91 sebito91 left a comment

Choose a reason for hiding this comment

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

A couple of questions, primary of which is the use of the pointer to time.time instead of just using the string repr of the time?

@@ -128,7 +130,9 @@ func (e Event) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("cloudEventsVersion", e.CloudEventsVersion)
enc.AddString("source", e.Source)
enc.AddString("eventID", e.EventID)
enc.AddString("eventTime", e.EventTime.String())
if e.EventTime != nil {
enc.AddString("eventTime", e.EventTime.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just make the Event struct EventTime into a string to begin with, then to time.Now().String() to populate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer time.Time because.. it's a time :) First of all it's good pattern to use types where they are meaningful, secondly it enabled all time operations on this data. Right now it's not used but eventually it will be. Also if someone uses our Event struct (it's based on CloudEvents spec) it's better to provide meaningful types.

@@ -34,7 +34,7 @@ type Event struct {
CloudEventsVersion string `json:"cloudEventsVersion" validate:"required"`
Source string `json:"source" validate:"uri,required"`
EventID string `json:"eventID" validate:"required"`
EventTime time.Time `json:"eventTime,omitempty"`
EventTime *time.Time `json:"eventTime,omitempty"`
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 not sure I understand why we need this to be a pointer and not just the string representation of the time.Now()?

event/event.go Outdated
if val, err := time.Parse(time.RFC3339, headers.Get("CE-EventTime")); err == nil {
event.EventTime = val
if headers.Get("CE-EventTime") != "" {
if val, err := time.Parse(time.RFC3339, headers.Get("CE-EventTime")); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would flip this around, the val is stlll local to the outer if block

if headers.Get("CE-EventTime") != "" {
    val, err := time.Parse(time.RFC3339, headers.Get("CE-EventTime"))
    if err != nil {
        return nil, err
    }
    event.EventTime = &val
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, yeah. One sec

@@ -36,6 +38,9 @@ func TestNew_Encoding(t *testing.T) {
}

func TestFromRequest(t *testing.T) {
patch := monkey.Patch(time.Now, func() time.Time { return testTime })
Copy link
Contributor

Choose a reason for hiding this comment

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

I've never used it before myself, but ok! Wonder if we can just have a mock function to return that, like in the github.com/stretchr/testify/mock package?

In any case, if this works then I would leave it for now!

name: "valid CloudEvent in binary mode with invalid event time",
requestHeaders: http.Header{
"Content-Type": []string{"text/plain"},
"Ce-Eventtype": []string{"myevent"},
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these be CE-<name> like the events/events.go package? I guess it doesn't really matter but for consistency...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how headers are stored internally in http.Header struct.

@mthenw mthenw merged commit c84ddec into master Jul 25, 2018
@mthenw mthenw deleted the fix-time branch July 25, 2018 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants