12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package borealis
- import (
- "encoding/base64"
- "encoding/json"
- "fmt"
- )
- type Event struct {
- Type uint16
- TimestampS uint32
- TimestampMS uint16
- SequentialID uint64
- UniqueID []byte
- Payload interface{}
- }
- func (event Event) MarshalJSON() ([]byte, error) {
- array := []interface{}{
- event.Type,
- event.TimestampS,
- event.TimestampMS,
- event.SequentialID,
- event.UniqueID, // automatically Base64-encoded
- event.Payload,
- }
- return json.Marshal(array)
- }
- func (event *Event) UnmarshalJSON(input []byte) error {
- var err error
- array := []interface{}{}
- if err = json.Unmarshal(input, &array); err != nil {
- return err
- }
- if len(array) != 6 {
- return fmt.Errorf("event must be an array of length 6, got %d", len(array))
- }
- event.Type = uint16(array[0].(float64))
- event.TimestampS = uint32(array[1].(float64))
- event.TimestampMS = uint16(array[2].(float64))
- event.SequentialID = uint64(array[3].(float64))
- event.UniqueID, err = base64.StdEncoding.DecodeString(array[4].(string))
- if err != nil {
- return err
- }
- event.Payload = array[5]
- return nil
- }
|