/
input_message.go
51 lines (40 loc) 路 1.06 KB
/
input_message.go
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
package connections
import (
"bytes"
"errors"
)
type InputMessageType uint8
const (
InputMessageTypeSnakeCommand InputMessageType = iota
InputMessageTypeBroadcast
)
var inputMessageTypeJSONs = map[InputMessageType][]byte{
InputMessageTypeSnakeCommand: []byte(`"snake"`),
InputMessageTypeBroadcast: []byte(`"broadcast"`),
}
var ErrUnknownInputMessageType = errors.New("unknown input message type")
func (t *InputMessageType) UnmarshalJSON(data []byte) error {
for msgType, commandJSON := range inputMessageTypeJSONs {
if bytes.Equal(commandJSON, data) {
*t = msgType
return nil
}
}
return ErrUnknownInputMessageType
}
var inputMessageTypeLabels = map[InputMessageType]string{
InputMessageTypeSnakeCommand: "snake",
InputMessageTypeBroadcast: "broadcast",
}
func (t InputMessageType) String() string {
if label, ok := inputMessageTypeLabels[t]; ok {
return label
}
return "unknown"
}
//go:generate ffjson $GOFILE
// ffjson: noencoder
type InputMessage struct {
Type InputMessageType `json:"type"`
Payload string `json:"payload"`
}