Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions events/code_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ type CodeCommitEventTime time.Time
// https://golang.org/pkg/time/#Parse
const codeCommitEventTimeReference = "\"2006-01-2T15:04:05.000-0700\""

func (t *CodeCommitEventTime) MarshalJSON() ([]byte, error) {
if t == nil {
return nil, errors.New("CodeCommitEventTime cannot be nil")
}

gt := time.Time(*t)
return []byte(gt.Format(codeCommitEventTimeReference)), nil
// MarshalJSON has a value receiver so that a CodeCommitRecord marshals the
// same whether it is passed by value or by pointer.
func (t CodeCommitEventTime) MarshalJSON() ([]byte, error) {
return []byte(time.Time(t).Format(codeCommitEventTimeReference)), nil
}

func (t *CodeCommitEventTime) UnmarshalJSON(data []byte) error {
Expand Down
22 changes: 22 additions & 0 deletions events/code_commit_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package events

import (
"encoding/json"
"testing"
"time"

"github.com/aws/aws-lambda-go/events/test"
)
Expand Down Expand Up @@ -174,3 +176,23 @@ func TestCodeCommitEvent(t *testing.T) {
})
}
}

func TestCodeCommitEventTimeMarshalsByValue(t *testing.T) {
want := CodeCommitEventTime(time.Date(2020, 1, 15, 1, 2, 3, 0, time.UTC))
record := CodeCommitRecord{EventTime: want}

// a record passed by value has non-addressable fields, so the marshaler
// must not need a pointer receiver
b, err := json.Marshal(record)
if err != nil {
t.Fatal(err)
}

var got CodeCommitRecord
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("%s: %v", b, err)
}
if !time.Time(got.EventTime).Equal(time.Time(want)) {
t.Errorf("event time = %v, want %v", time.Time(got.EventTime), time.Time(want))
}
}
Loading