-
Notifications
You must be signed in to change notification settings - Fork 7
/
msg.go
188 lines (157 loc) 路 4.05 KB
/
msg.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package runtime
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Msg methods don't return errors because they can be used not only at startup.
// If runtime functions need to validate message at startup, they must do it by themselves.
type Msg interface {
fmt.Stringer
Type() MsgType
Bool() bool
Int() int64
Float() float64
Str() string
List() []Msg
Map() map[string]Msg
}
type MsgType uint8
const (
UnknownMsgType MsgType = 0
BoolMsgType MsgType = 1
IntMsgType MsgType = 2
FloatMsgType MsgType = 3
StrMsgType MsgType = 4
ListMsgType MsgType = 5
MapMsgType MsgType = 6
)
// Empty
type emptyMsg struct{}
func (emptyMsg) String() string { return "<empty>" }
func (emptyMsg) Type() MsgType { return UnknownMsgType }
func (emptyMsg) Bool() bool { return false }
func (emptyMsg) Int() int64 { return 0 }
func (emptyMsg) Float() float64 { return 0 }
func (emptyMsg) Str() string { return "" }
func (emptyMsg) List() []Msg { return nil }
func (emptyMsg) Map() map[string]Msg { return nil }
// Int
type IntMsg struct {
emptyMsg
v int64
}
func (msg IntMsg) Type() MsgType { return IntMsgType }
func (msg IntMsg) Int() int64 { return msg.v }
func (msg IntMsg) String() string { return strconv.Itoa(int(msg.v)) }
func (msg IntMsg) MarshalJSON() ([]byte, error) { return []byte(msg.String()), nil }
func NewIntMsg(n int64) IntMsg {
return IntMsg{
emptyMsg: emptyMsg{},
v: n,
}
}
// Float
type FloatMsg struct {
emptyMsg
v float64
}
func (msg FloatMsg) Type() MsgType { return FloatMsgType }
func (msg FloatMsg) Float() float64 { return msg.v }
func (msg FloatMsg) String() string { return fmt.Sprint(msg.v) }
func (msg FloatMsg) MarshalJSON() ([]byte, error) { return []byte(msg.String()), nil }
func NewFloatMsg(n float64) FloatMsg {
return FloatMsg{
emptyMsg: emptyMsg{},
v: n,
}
}
// Str
type StrMsg struct {
emptyMsg
v string
}
func (msg StrMsg) Type() MsgType { return StrMsgType }
func (msg StrMsg) Str() string { return msg.v }
func (msg StrMsg) String() string { return msg.v }
func (msg StrMsg) MarshalJSON() ([]byte, error) {
// Escape special characters in the string before marshaling
jsonString, err := json.Marshal(msg.String())
if err != nil {
return nil, err
}
return jsonString, nil
}
func NewStrMsg(s string) StrMsg {
return StrMsg{
emptyMsg: emptyMsg{},
v: s,
}
}
// Bool
type BoolMsg struct {
emptyMsg
v bool
}
func (msg BoolMsg) Type() MsgType { return BoolMsgType }
func (msg BoolMsg) Bool() bool { return msg.v }
func (msg BoolMsg) String() string { return strconv.FormatBool(msg.v) }
func (msg BoolMsg) MarshalJSON() ([]byte, error) { return []byte(msg.String()), nil }
func NewBoolMsg(b bool) BoolMsg {
return BoolMsg{
emptyMsg: emptyMsg{},
v: b,
}
}
// List
type ListMsg struct {
emptyMsg
v []Msg
}
func (msg ListMsg) Type() MsgType { return ListMsgType }
func (msg ListMsg) List() []Msg { return msg.v }
func (msg ListMsg) String() string {
bb, err := msg.MarshalJSON()
if err != nil {
panic(err)
}
return string(bb)
}
func (msg ListMsg) MarshalJSON() ([]byte, error) { return json.Marshal(msg.v) }
func NewListMsg(v ...Msg) ListMsg {
return ListMsg{
emptyMsg: emptyMsg{},
v: v,
}
}
// Map
type MapMsg struct {
emptyMsg
v map[string]Msg
}
func (msg MapMsg) Type() MsgType { return MapMsgType }
func (msg MapMsg) Map() map[string]Msg { return msg.v }
func (msg MapMsg) MarshalJSON() ([]byte, error) {
jsonData, err := json.Marshal(msg.v)
if err != nil {
panic(err)
}
jsonString := string(jsonData)
jsonString = strings.ReplaceAll(jsonString, ":", ": ")
jsonString = strings.ReplaceAll(jsonString, ",", ", ")
return []byte(jsonString), nil
}
func (msg MapMsg) String() string {
b, err := msg.MarshalJSON()
if err != nil {
panic(err)
}
return string(b)
}
func NewMapMsg(m map[string]Msg) MapMsg {
return MapMsg{
emptyMsg: emptyMsg{},
v: m,
}
}