string formatted timestamp for chats

This commit is contained in:
Asim Aslam
2021-05-07 18:01:26 +01:00
parent 542c105037
commit 6a7273d39d
6 changed files with 34 additions and 21 deletions

View File

@@ -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),
}
}

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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))
})
}

View File

@@ -29,8 +29,8 @@ type Chat struct {
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
// list of users in the chat
UserIds []string `protobuf:"bytes,2,rep,name=user_ids,json=userIds,proto3" json:"user_ids,omitempty"`
// unix nanosecond timestamp
CreatedAt int64 `protobuf:"varint,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
// RFC3339Nano timestamp
CreatedAt string `protobuf:"bytes,3,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
}
func (x *Chat) Reset() {
@@ -79,11 +79,11 @@ func (x *Chat) GetUserIds() []string {
return nil
}
func (x *Chat) GetCreatedAt() int64 {
func (x *Chat) GetCreatedAt() string {
if x != nil {
return x.CreatedAt
}
return 0
return ""
}
type Message struct {
@@ -99,8 +99,8 @@ type Message struct {
ChatId string `protobuf:"bytes,3,opt,name=chat_id,json=chatId,proto3" json:"chat_id,omitempty"`
// text within the message
Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"`
// unix nanosecond timestamp
SentAt int64 `protobuf:"varint,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"`
// RFC3339Nano timestamp
SentAt string `protobuf:"bytes,5,opt,name=sent_at,json=sentAt,proto3" json:"sent_at,omitempty"`
}
func (x *Message) Reset() {
@@ -163,11 +163,11 @@ func (x *Message) GetText() string {
return ""
}
func (x *Message) GetSentAt() int64 {
func (x *Message) GetSentAt() string {
if x != nil {
return x.SentAt
}
return 0
return ""
}
// Create a new chat between mulitple users
@@ -526,7 +526,7 @@ var file_proto_chats_proto_rawDesc = []byte{
0x69, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x02,
0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x73, 0x12, 0x1d, 0x0a,
0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28,
0x03, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x7c, 0x0a, 0x07,
0x09, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x7c, 0x0a, 0x07,
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f,
0x72, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68,
@@ -534,7 +534,7 @@ var file_proto_chats_proto_rawDesc = []byte{
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x68, 0x61, 0x74, 0x49, 0x64, 0x12, 0x12, 0x0a,
0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78,
0x74, 0x12, 0x17, 0x0a, 0x07, 0x73, 0x65, 0x6e, 0x74, 0x5f, 0x61, 0x74, 0x18, 0x05, 0x20, 0x01,
0x28, 0x03, 0x52, 0x06, 0x73, 0x65, 0x6e, 0x74, 0x41, 0x74, 0x22, 0x3e, 0x0a, 0x11, 0x43, 0x72,
0x28, 0x09, 0x52, 0x06, 0x73, 0x65, 0x6e, 0x74, 0x41, 0x74, 0x22, 0x3e, 0x0a, 0x11, 0x43, 0x72,
0x65, 0x61, 0x74, 0x65, 0x43, 0x68, 0x61, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12,
0x19, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28,

View File

@@ -20,8 +20,8 @@ message Chat {
string id = 1;
// list of users in the chat
repeated string user_ids = 2;
// unix nanosecond timestamp
int64 created_at = 3;
// RFC3339Nano timestamp
string created_at = 3;
}
message Message {
@@ -33,8 +33,8 @@ message Message {
string chat_id = 3;
// text within the message
string text = 4;
// unix nanosecond timestamp
int64 sent_at = 5;
// RFC3339Nano timestamp
string sent_at = 5;
}
// Create a new chat between mulitple users