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