/
model.go
69 lines (58 loc) · 1.4 KB
/
model.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package messagebroker
import (
"fmt"
"strconv"
)
type MessageTypes int
const (
MessageTypeCreated MessageTypes = iota
MessageTypeUpdated
MessageTypeDeleted
_maxMessageType
)
var MessageTypesName = map[int]string{
0: "create",
1: "update",
2: "delete",
}
var MessageTypesValue = map[string]MessageTypes{
MessageTypesName[0]: MessageTypeCreated,
MessageTypesName[1]: MessageTypeUpdated,
MessageTypesName[2]: MessageTypeDeleted,
}
func (m MessageTypes) String() string {
s, ok := MessageTypesName[int(m)]
if ok {
return s
}
return strconv.Itoa(int(m))
}
// UnmarshalJSON unmarshals b into MessageTypes.
func (m *MessageTypes) UnmarshalJSON(b []byte) error {
// From json.Unmarshaler: By convention, to approximate the behavior of
// Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
// a no-op.
if string(b) == "null" {
return nil
}
if m == nil {
return fmt.Errorf("nil receiver passed to UnmarshalJSON")
}
if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
if ci >= uint64(_maxMessageType) {
return fmt.Errorf("invalid code: %q", ci)
}
*m = MessageTypes(ci)
return nil
}
if mv, ok := MessageTypesValue[string(b)]; ok {
*m = mv
return nil
}
return fmt.Errorf("invalid code: %q", string(b))
}
type Message struct {
Type MessageTypes `json:"type"`
SubType string `json:"subtype"`
Data interface{} `json:"data,omitempty"`
}