-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_message.go
More file actions
89 lines (74 loc) · 2.19 KB
/
types_message.go
File metadata and controls
89 lines (74 loc) · 2.19 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package m3da
import "fmt"
func init() {
// Register the message decoder
registerCustomDecoder(OpCodeMessage, decodeMessage)
}
// M3daMessage represents an M3DA message
type M3daMessage struct {
Path string `json:"path"`
TicketID *uint32 `json:"ticket_id,omitempty"`
Body map[string]interface{} `json:"body"`
}
// GetOpCode returns the operation code for M3DA messages
func (m *M3daMessage) GetOpCode() byte {
return OpCodeMessage
}
// EncodeTo encodes the message using the provided encoder
func (m *M3daMessage) EncodeTo(encoder *BysantEncoder) error {
// Write the message opcode
encoder.buf.WriteByte(OpCodeMessage)
// Message path uses UINTS_AND_STRS context
if err := encoder.encodeStringInContext(m.Path, ContextUintsAndStrs); err != nil {
return err
}
// Message ticket ID uses UINTS_AND_STRS context (unsigned integers)
if m.TicketID != nil {
if err := encoder.encodeUnsignedIntegerInContext(*m.TicketID, ContextUintsAndStrs); err != nil {
return err
}
} else {
if err := encoder.encodeNull(); err != nil {
return err
}
}
// Message body uses LIST_AND_MAPS context
return encoder.encodeMapInContext(m.Body, ContextListAndMaps)
}
// DecodeMessage decodes an M3DA message from the decoder
func decodeMessage(decoder *BysantDecoder) (M3daEncodable, error) {
// Decode path (string in UINTS_AND_STRS context)
pathObj, err := decoder.decodeObjectInContext(ContextUintsAndStrs)
if err != nil {
return nil, err
}
path, ok := pathObj.(string)
if !ok {
return nil, fmt.Errorf("expected string path, got %T", pathObj)
}
// Decode ticket ID (in UINTS_AND_STRS context, can be null or int64)
ticketObj, err := decoder.decodeObjectInContext(ContextUintsAndStrs)
if err != nil {
return nil, err
}
var ticketID *uint32
if ticketObj != nil {
if tid, ok := ticketObj.(uint32); ok {
ticketID = &tid
}
}
// Decode body (map in LIST_AND_MAPS context)
bodyObj, err := decoder.decodeObjectInContext(ContextListAndMaps)
if err != nil {
return nil, err
}
body, ok := bodyObj.(map[string]interface{})
if !ok {
body = make(map[string]interface{})
}
return &M3daMessage{
Path: path,
TicketID: ticketID,
Body: body,
}, nil
}