dotfiles/goagent/pkg/gophx/message.go

37 lines
573 B
Go
Raw Normal View History

2024-02-17 15:20:08 +00:00
package gophx
import (
"encoding/json"
)
type message struct {
joinRef int64
ref int64
topic string
event string
payload any
}
func (m message) serializeJSON() ([]byte, error) {
data, err := json.Marshal([]any{m.joinRef, m.ref, m.topic, m.event, m.payload})
if err != nil {
return nil, err
}
return data, nil
}
func deserializeJSON(data []byte, msg *message) error {
m := message{}
slice := []any{&m.joinRef, &m.ref, &m.topic, &m.event, &m.payload}
err := json.Unmarshal(data, &slice)
if err != nil {
return err
}
*msg = m
return nil
}