mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-19 14:05:23 +00:00
string formatted timestamp for chats
This commit is contained in:
@@ -40,13 +40,25 @@ type Message struct {
|
||||
SentAt time.Time
|
||||
}
|
||||
|
||||
func ParseTime(v string) time.Time {
|
||||
t, err := time.Parse(time.RFC3339Nano, v)
|
||||
if err == nil {
|
||||
return t
|
||||
}
|
||||
t, err = time.Parse(time.RFC3339, v)
|
||||
if err == nil {
|
||||
return t
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (m *Message) Serialize() *pb.Message {
|
||||
return &pb.Message{
|
||||
Id: m.ID,
|
||||
AuthorId: m.AuthorID,
|
||||
ChatId: m.ChatID,
|
||||
Text: m.Text,
|
||||
SentAt: m.SentAt.UnixNano(),
|
||||
SentAt: m.SentAt.Format(time.RFC3339Nano),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +121,6 @@ func (c *Chat) Serialize() *pb.Chat {
|
||||
return &pb.Chat{
|
||||
Id: c.ID,
|
||||
UserIds: c.UserIDs,
|
||||
CreatedAt: c.CreatedAt.UnixNano(),
|
||||
CreatedAt: c.CreatedAt.Format(time.RFC3339Nano),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func assertChatsMatch(t *testing.T, exp, act *pb.Chat) {
|
||||
|
||||
assert.Equal(t, exp.UserIds, act.UserIds)
|
||||
|
||||
if act.CreatedAt == 0 {
|
||||
if len(act.CreatedAt) == 0 {
|
||||
t.Errorf("CreatedAt not set")
|
||||
return
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
|
||||
assert.Equal(t, exp.AuthorId, act.AuthorId)
|
||||
assert.Equal(t, exp.ChatId, act.ChatId)
|
||||
|
||||
if act.SentAt == 0 {
|
||||
if len(act.SentAt) == 0 {
|
||||
t.Errorf("SentAt not set")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/micro/services/chats/handler"
|
||||
pb "github.com/micro/services/chats/proto"
|
||||
@@ -98,7 +99,7 @@ func TestSendMessage(t *testing.T) {
|
||||
assertMessagesMatch(t, &pb.Message{
|
||||
AuthorId: tc.AuthorID,
|
||||
ChatId: tc.ChatID,
|
||||
SentAt: h.Time().UnixNano(),
|
||||
SentAt: h.Time().Format(time.RFC3339Nano),
|
||||
Text: tc.Text,
|
||||
Id: tc.ID,
|
||||
}, rsp.Message)
|
||||
|
||||
@@ -106,9 +106,9 @@ func TestListMessages(t *testing.T) {
|
||||
// sortMessages by the time they were sent
|
||||
func sortMessages(msgs []*pb.Message) {
|
||||
sort.Slice(msgs, func(i, j int) bool {
|
||||
if msgs[i].SentAt == 0 || msgs[j].SentAt == 0 {
|
||||
if len(msgs[i].SentAt) == 0 || len(msgs[j].SentAt) == 0 {
|
||||
return true
|
||||
}
|
||||
return msgs[i].SentAt < msgs[j].SentAt
|
||||
return handler.ParseTime(msgs[i].SentAt).Before(handler.ParseTime(msgs[j].SentAt))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user