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 a2afd2c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 73 deletions.
102 changes: 67 additions & 35 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 @@ -45,15 +47,15 @@ const (

// EventSource type
type EventSource struct {
Type EventSourceType `json:"type"`
UserID string `json:"userId"`
GroupID string `json:"groupId"`
RoomID string `json:"roomId"`
Type EventSourceType `json:"type,omitempty"`
UserID string `json:"userId,omitempty"`
GroupID string `json:"groupId,omitempty"`
RoomID string `json:"roomId,omitempty"`
}

// Postback type
type Postback struct {
Data string `json:"data"`
Data string `json:"data,omitempty"`
}

// BeaconEventType type
Expand All @@ -66,55 +68,85 @@ const (

// Beacon type
type Beacon struct {
Hwid string `json:"hwid"`
Type BeaconEventType `json:"type"`
Hwid string `json:"hwid,omitempty"`
Type BeaconEventType `json:"type,omitempty"`
}

// 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)
}
}
54 changes: 27 additions & 27 deletions linebot/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type TextMessage struct {
// MarshalJSON method of TextMessage
func (m *TextMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
Text string `json:"text"`
Type MessageType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
}{
Type: MessageTypeText,
Text: m.Text,
Expand All @@ -66,9 +66,9 @@ type ImageMessage struct {
// MarshalJSON method of ImageMessage
func (m *ImageMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
Type MessageType `json:"type,omitempty"`
OriginalContentURL string `json:"originalContentUrl,omitempty"`
PreviewImageURL string `json:"previewImageUrl,omitempty"`
}{
Type: MessageTypeImage,
OriginalContentURL: m.OriginalContentURL,
Expand All @@ -86,9 +86,9 @@ type VideoMessage struct {
// MarshalJSON method of VideoMessage
func (m *VideoMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
OriginalContentURL string `json:"originalContentUrl"`
PreviewImageURL string `json:"previewImageUrl"`
Type MessageType `json:"type,omitempty"`
OriginalContentURL string `json:"originalContentUrl,omitempty"`
PreviewImageURL string `json:"previewImageUrl,omitempty"`
}{
Type: MessageTypeVideo,
OriginalContentURL: m.OriginalContentURL,
Expand All @@ -106,9 +106,9 @@ type AudioMessage struct {
// MarshalJSON method of AudioMessage
func (m *AudioMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
OriginalContentURL string `json:"originalContentUrl"`
Duration int `json:"duration"`
Type MessageType `json:"type,omitempty"`
OriginalContentURL string `json:"originalContentUrl,omitempty"`
Duration int `json:"duration,omitempty"`
}{
Type: MessageTypeAudio,
OriginalContentURL: m.OriginalContentURL,
Expand All @@ -128,11 +128,11 @@ type LocationMessage struct {
// MarshalJSON method of LocationMessage
func (m *LocationMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
Title string `json:"title"`
Address string `json:"address"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Type MessageType `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Address string `json:"address,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
}{
Type: MessageTypeLocation,
Title: m.Title,
Expand All @@ -152,9 +152,9 @@ type StickerMessage struct {
// MarshalJSON method of StickerMessage
func (m *StickerMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
PackageID string `json:"packageId"`
StickerID string `json:"stickerId"`
Type MessageType `json:"type,omitempty"`
PackageID string `json:"packageId,omitempty"`
StickerID string `json:"stickerId,omitempty"`
}{
Type: MessageTypeSticker,
PackageID: m.PackageID,
Expand All @@ -171,9 +171,9 @@ type TemplateMessage struct {
// MarshalJSON method of TemplateMessage
func (m *TemplateMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
AltText string `json:"altText"`
Template Template `json:"template"`
Type MessageType `json:"type,omitempty"`
AltText string `json:"altText,omitempty"`
Template Template `json:"template,omitempty"`
}{
Type: MessageTypeTemplate,
AltText: m.AltText,
Expand All @@ -192,11 +192,11 @@ type ImagemapMessage struct {
// MarshalJSON method of ImagemapMessage
func (m *ImagemapMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type MessageType `json:"type"`
BaseURL string `json:"baseUrl"`
AltText string `json:"altText"`
BaseSize ImagemapBaseSize `json:"baseSize"`
Actions []ImagemapAction `json:"actions"`
Type MessageType `json:"type,omitempty"`
BaseURL string `json:"baseUrl,omitempty"`
AltText string `json:"altText,omitempty"`
BaseSize ImagemapBaseSize `json:"baseSize,omitempty"`
Actions []ImagemapAction `json:"actions,omitempty"`
}{
Type: MessageTypeImagemap,
BaseURL: m.BaseURL,
Expand Down
Loading

0 comments on commit a2afd2c

Please sign in to comment.