-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_response.go
More file actions
81 lines (66 loc) · 1.88 KB
/
types_response.go
File metadata and controls
81 lines (66 loc) · 1.88 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
package m3da
import "fmt"
func init() {
// Register the response decoder
registerCustomDecoder(OpCodeResponse, decodeResponse)
}
// M3daResponse represents an M3DA response
type M3daResponse struct {
TicketID uint32 `json:"ticket_id"`
Status int64 `json:"status"`
Message string `json:"message,omitempty"`
}
// GetOpCode returns the operation code for M3DA responses
func (r *M3daResponse) GetOpCode() byte {
return OpCodeResponse
}
// EncodeTo encodes the response using the provided encoder
func (r *M3daResponse) EncodeTo(encoder *BysantEncoder) error {
// Write the response opcode
encoder.buf.WriteByte(OpCodeResponse)
// Encode ticket ID
if err := encoder.encodeUnsignedIntegerInContext(r.TicketID, ContextUintsAndStrs); err != nil {
return err
}
// Encode status
if err := encoder.encodeIntegerInContext(r.Status, ContextGlobal); err != nil {
return err
}
// Encode message
return encoder.encodeString(r.Message)
}
// DecodeResponse decodes an M3DA response from the decoder
func decodeResponse(decoder *BysantDecoder) (M3daEncodable, error) {
// Decode ticket ID (in UINT AND STRINGS context)
ticketObj, err := decoder.decodeObjectInContext(ContextUintsAndStrs)
if err != nil {
return nil, err
}
ticketID, ok := ticketObj.(uint32)
if !ok {
return nil, fmt.Errorf("invalid ticket ID type: got %T", ticketObj)
}
// Decode status (in GLOBAL context)
statusObj, err := decoder.decodeObjectInContext(ContextGlobal)
if err != nil {
return nil, err
}
status, ok := statusObj.(int64)
if !ok {
return nil, fmt.Errorf("invalid status type: got %T", statusObj)
}
// Decode message (string in GLOBAL context)
messageObj, err := decoder.decodeObjectInContext(ContextGlobal)
if err != nil {
return nil, err
}
message, ok := messageObj.(string)
if !ok {
message = ""
}
return &M3daResponse{
TicketID: ticketID,
Status: status,
Message: message,
}, nil
}