-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
message.go
55 lines (48 loc) · 1.24 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package data
import (
"github.com/simpleiot/simpleiot/internal/pb"
"google.golang.org/protobuf/proto"
)
// Message describes a notification that is sent to a particular user
type Message struct {
ID string
UserID string
ParentID string
NotificationID string
Email string
Phone string
Subject string
Message string
}
// ToPb converts to protobuf data
func (m *Message) ToPb() ([]byte, error) {
pbMsg := pb.Message{
Id: m.ID,
UserId: m.UserID,
ParentId: m.ParentID,
NotificationId: m.NotificationID,
Email: m.Email,
Phone: m.Phone,
Subject: m.Subject,
Message: m.Message,
}
return proto.Marshal(&pbMsg)
}
// PbDecodeMessage converts a protobuf to a message data structure
func PbDecodeMessage(data []byte) (Message, error) {
pbMsg := &pb.Message{}
err := proto.Unmarshal(data, pbMsg)
if err != nil {
return Message{}, err
}
return Message{
ID: pbMsg.Id,
UserID: pbMsg.UserId,
ParentID: pbMsg.ParentId,
NotificationID: pbMsg.NotificationId,
Email: pbMsg.Email,
Phone: pbMsg.Phone,
Subject: pbMsg.Subject,
Message: pbMsg.Message,
}, nil
}