diff options
Diffstat (limited to 'encoding.go')
| -rw-r--r-- | encoding.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/encoding.go b/encoding.go new file mode 100644 index 0000000..4ed9da6 --- /dev/null +++ b/encoding.go | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | package axon | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | |||
| 6 | "github.com/vmihailenco/msgpack/v5" | ||
| 7 | ) | ||
| 8 | |||
| 9 | // MarshalEvent encodes an Event to MessagePack. Fields are encoded in the | ||
| 10 | // canonical struct order using the msgpack struct tags. Binary fields (id, | ||
| 11 | // pubkey, sig, content) are encoded as msgpack bin type ([]byte). | ||
| 12 | func MarshalEvent(e *Event) ([]byte, error) { | ||
| 13 | b, err := msgpack.Marshal(e) | ||
| 14 | if err != nil { | ||
| 15 | return nil, fmt.Errorf("axon: marshal event: %w", err) | ||
| 16 | } | ||
| 17 | return b, nil | ||
| 18 | } | ||
| 19 | |||
| 20 | // UnmarshalEvent decodes a MessagePack blob into an Event. | ||
| 21 | func UnmarshalEvent(data []byte) (*Event, error) { | ||
| 22 | var e Event | ||
| 23 | if err := msgpack.Unmarshal(data, &e); err != nil { | ||
| 24 | return nil, fmt.Errorf("axon: unmarshal event: %w", err) | ||
| 25 | } | ||
| 26 | return &e, nil | ||
| 27 | } | ||
| 28 | |||
| 29 | // MarshalMessage encodes a wire message as a msgpack array: [type, payload]. | ||
| 30 | // messageType is a uint16; payload is any msgpack-serializable value. | ||
| 31 | func MarshalMessage(messageType uint16, payload interface{}) ([]byte, error) { | ||
| 32 | b, err := msgpack.Marshal([]interface{}{messageType, payload}) | ||
| 33 | if err != nil { | ||
| 34 | return nil, fmt.Errorf("axon: marshal message: %w", err) | ||
| 35 | } | ||
| 36 | return b, nil | ||
| 37 | } | ||
| 38 | |||
| 39 | // UnmarshalMessageType reads only the first element of a [type, payload] | ||
| 40 | // msgpack array, returning the message type without decoding the payload. | ||
| 41 | func UnmarshalMessageType(data []byte) (uint16, error) { | ||
| 42 | var arr []msgpack.RawMessage | ||
| 43 | if err := msgpack.Unmarshal(data, &arr); err != nil { | ||
| 44 | return 0, fmt.Errorf("axon: unmarshal message: %w", err) | ||
| 45 | } | ||
| 46 | if len(arr) < 1 { | ||
| 47 | return 0, fmt.Errorf("axon: message array is empty") | ||
| 48 | } | ||
| 49 | var t uint16 | ||
| 50 | if err := msgpack.Unmarshal(arr[0], &t); err != nil { | ||
| 51 | return 0, fmt.Errorf("axon: unmarshal message type: %w", err) | ||
| 52 | } | ||
| 53 | return t, nil | ||
| 54 | } | ||
