Skip to content

Commit

Permalink
Add Timestamp line#14
Browse files Browse the repository at this point in the history
  • Loading branch information
k2wanko committed Oct 2, 2016
1 parent fdf8ef7 commit 935cc4b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 39 deletions.
88 changes: 60 additions & 28 deletions linebot/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package linebot

import (
"encoding/json"
"fmt"
"strconv"
"time"
)

Expand Down Expand Up @@ -72,49 +74,79 @@ type Beacon struct {

// Event type
type Event struct {
ReplyToken string
Type EventType
Timestamp time.Time
Source *EventSource
Message Message
Postback *Postback
Beacon *Beacon
ReplyToken string `json:"replyToken,omitempty"`
Type EventType `json:"type,omitempty"`
Timestamp *Timestamp `json:"timestamp,omitempty"`
Source *EventSource `json:"source,omitempty"`
Message Message `json:"message,omitempty"`
Postback *Postback `json:"postback,omitempty"`
Beacon *Beacon `json:"beacon,omitempty"`
}

const (
millisecPerSec = int64(time.Second / time.Millisecond)
nanosecPerMillisec = int64(time.Millisecond / time.Nanosecond)
)

// Timestamp type
type Timestamp struct {
time.Time
}

// MarshalJSON method of Timestamp
func (t *Timestamp) MarshalJSON() (body []byte, err error) {
body = []byte(t.String())
return
}

// UnmarshalJSON method of Timestamp
func (t *Timestamp) UnmarshalJSON(body []byte) (err error) {
i, err := strconv.ParseInt(string(body), 10, 64)
if err != nil {
return
}
t.Time = time.Unix(i/millisecPerSec, (i%millisecPerSec)*nanosecPerMillisec).UTC()
return
}

// String method of Timestamp
func (t *Timestamp) String() string {
if t == nil {
return ""
}
return fmt.Sprintf("%d", t.Time.UnixNano()/int64(time.Millisecond))
}

// UnmarshalJSON method of Event
func (e *Event) UnmarshalJSON(body []byte) (err error) {
rawEvent := struct {
ReplyToken string `json:"replyToken"`
Type EventType `json:"type"`
Timestamp int64 `json:"timestamp"`
Source EventSource `json:"source"`
ReplyToken string `json:"replyToken,omitempty"`
Type EventType `json:"type,omitempty"`
Timestamp *Timestamp `json:"timestamp,omitempty"`
Source EventSource `json:"source,omitempty"`
Message struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Text string `json:"text"`
Duration int `json:"duration"`
Title string `json:"title"`
Address string `json:"address"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
PackageID string `json:"packageId"`
StickerID string `json:"stickerId"`
} `json:"message"`
Postback `json:"postback"`
Beacon `json:"beacon"`
ID string `json:"id,omitempty"`
Type MessageType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Duration int `json:"duration,omitempty"`
Title string `json:"title,omitempty"`
Address string `json:"address,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
PackageID string `json:"packageId,omitempty"`
StickerID string `json:"stickerId,omitempty"`

Message
} `json:"message,omitempty"`
Postback *Postback `json:"postback,omitempty"`
Beacon *Beacon `json:"beacon,omitempty"`
}{}
if err = json.Unmarshal(body, &rawEvent); err != nil {
return
}

e.ReplyToken = rawEvent.ReplyToken
e.Type = rawEvent.Type
e.Timestamp = time.Unix(rawEvent.Timestamp/millisecPerSec, (rawEvent.Timestamp%millisecPerSec)*nanosecPerMillisec).UTC()
e.Timestamp = rawEvent.Timestamp
e.Source = &rawEvent.Source

switch rawEvent.Type {
Expand Down Expand Up @@ -154,9 +186,9 @@ func (e *Event) UnmarshalJSON(body []byte) (err error) {
}
}
case EventTypePostback:
e.Postback = &rawEvent.Postback
e.Postback = rawEvent.Postback
case EventTypeBeacon:
e.Beacon = &rawEvent.Beacon
e.Beacon = rawEvent.Beacon
}
return
}
41 changes: 41 additions & 0 deletions linebot/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package linebot

import (
"encoding/json"
"reflect"
"testing"
)

func TestEventSerialize(t *testing.T) {
e := &Event{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypePostback,
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
},
Postback: &Postback{
Data: "action=buyItem&itemId=123123&color=red",
},
}

t.Logf("Event %v", e)

b, err := json.MarshalIndent(e, "", " ")
if err != nil {
t.Fatalf("json.Marshal Error: %v", err)
}

t.Logf("JSON: %s", b)

var e2 Event
err = json.Unmarshal(b, &e2)
if err != nil {
t.Fatalf("json.Unmarshal Error: %v", err)
}

if !reflect.DeepEqual(e, &e2) {
t.Errorf("Event \n%v\n want \n%v", e, &e2)
}
}
24 changes: 13 additions & 11 deletions linebot/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ var webhookTestRequestBody = `{
}
`

var ts = Timestamp{time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC)}

var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeMessage,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -183,7 +185,7 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeMessage,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeGroup,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -197,7 +199,7 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeMessage,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -209,7 +211,7 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeMessage,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -225,7 +227,7 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeMessage,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -239,15 +241,15 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeFollow,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
},
},
{
Type: EventTypeUnfollow,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -256,15 +258,15 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeJoin,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeGroup,
GroupID: "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
},
},
{
Type: EventTypeLeave,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeGroup,
GroupID: "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
Expand All @@ -273,7 +275,7 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypePostback,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
Expand All @@ -285,7 +287,7 @@ var webhookTestWantEvents = []*Event{
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeBeacon,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Timestamp: &ts,
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "U012345678901234567890123456789ab",
Expand Down

0 comments on commit 935cc4b

Please sign in to comment.