forked from futurehomeno/fimpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
331 lines (280 loc) · 10.1 KB
/
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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package fimpgo
import "time"
import (
"encoding/json"
"errors"
"github.com/buger/jsonparser"
"github.com/satori/go.uuid"
)
const (
TimeFormat = "2006-01-02T15:04:05.999+01:00"
VTypeString = "string"
VTypeInt = "int"
VTypeFloat = "float"
VTypeBool = "bool"
VTypeStrMap = "str_map"
VTypeIntMap = "int_map"
VTypeFloatMap = "float_map"
VTypeBoolMap = "bool_map"
VTypeStrArray = "str_array"
VTypeIntArray = "int_array"
VTypeFloatArray = "float_array"
VTypeBoolArray = "bool_array"
VTypeObject = "object"
VTypeBinary = "bin"
VTypeNull = "null"
)
type Props map[string]string
type Tags []string
type FimpMessage struct {
Type string `json:"type"`
Service string `json:"serv"`
ValueType string `json:"val_t"`
Value interface{} `json:"val"`
ValueObj []byte `json:"-"`
Tags Tags `json:"tags"`
Properties Props `json:"props"`
Version string `json:"ver"`
CorrelationID string `json:"corid"`
ResponseToTopic string `json:"resp_to,omitempty"`
Source string `json:"src,omitempty"`
CreationTime string `json:"ctime"`
UID string `json:"uid"`
}
func (msg *FimpMessage) SetValue(value interface{}, valType string) {
msg.Value = value
msg.ValueType = valType
}
func (msg *FimpMessage) GetIntValue() (int64, error) {
val, ok := msg.Value.(int64)
if ok {
return val, nil
}
return 0, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetStringValue() (string, error) {
val, ok := msg.Value.(string)
if ok {
return val, nil
}
return "", errors.New("Wrong value type")
}
func (msg *FimpMessage) GetBoolValue() (bool, error) {
val, ok := msg.Value.(bool)
if ok {
return val, nil
}
return false, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetFloatValue() (float64, error) {
val, ok := msg.Value.(float64)
if ok {
return val, nil
}
return 0, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetStrArrayValue() ([]string, error) {
val, ok := msg.Value.([]string)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetIntArrayValue() ([]int64, error) {
val, ok := msg.Value.([]int64)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetFloatArrayValue() ([]float64, error) {
val, ok := msg.Value.([]float64)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetBoolArrayValue() ([]bool, error) {
val, ok := msg.Value.([]bool)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetStrMapValue() (map[string]string, error) {
val, ok := msg.Value.(map[string]string)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetIntMapValue() (map[string]int64, error) {
val, ok := msg.Value.(map[string]int64)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetFloatMapValue() (map[string]float64, error) {
val, ok := msg.Value.(map[string]float64)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetBoolMapValue() (map[string]bool, error) {
val, ok := msg.Value.(map[string]bool)
if ok {
return val, nil
}
return nil, errors.New("Wrong value type")
}
func (msg *FimpMessage) GetRawObjectValue() []byte {
return msg.ValueObj
}
func (msg *FimpMessage) GetObjectValue(objectBindVar interface{}) error {
return json.Unmarshal(msg.ValueObj, objectBindVar)
}
func (msg *FimpMessage) SerializeToJson() ([]byte, error) {
jsonBA, err := json.Marshal(msg)
return jsonBA, err
}
func NewMessage(type_ string, service string, valueType string, value interface{}, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
msg := FimpMessage{Type: type_,
Service: service,
ValueType: valueType,
Value: value,
Tags: tags,
Properties: props,
Version: "1",
CreationTime:time.Now().Format(TimeFormat),
UID:uuid.NewV4().String(),
}
if requestMessage != nil {
msg.CorrelationID = requestMessage.UID
}
return &msg
}
func NewNullMessage(type_ string, service string, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeNull, nil, props, tags, requestMessage)
}
func NewStringMessage(type_ string, service string, value string, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeString, value, props, tags, requestMessage)
}
func NewIntMessage(type_ string, service string, value int64, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeInt, value, props, tags, requestMessage)
}
func NewFloatMessage(type_ string, service string, value float64, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeFloat, value, props, tags, requestMessage)
}
func NewBoolMessage(type_ string, service string, value bool, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeBool, value, props, tags, requestMessage)
}
func NewStrArrayMessage(type_ string, service string, value []string, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeStrArray, value, props, tags, requestMessage)
}
func NewIntArrayMessage(type_ string, service string, value []int64, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeIntArray, value, props, tags, requestMessage)
}
func NewFloatArrayMessage(type_ string, service string, value []float64, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeFloatArray, value, props, tags, requestMessage)
}
func NewBoolArrayMessage(type_ string, service string, value []bool, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeBoolArray, value, props, tags, requestMessage)
}
func NewStrMapMessage(type_ string, service string, value map[string]string, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeStrMap, value, props, tags, requestMessage)
}
func NewIntMapMessage(type_ string, service string, value map[string]int64, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeIntMap, value, props, tags, requestMessage)
}
func NewFloatMapMessage(type_ string, service string, value map[string]float64, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeFloatMap, value, props, tags, requestMessage)
}
func NewBoolMapMessage(type_ string, service string, value map[string]bool, props Props, tags Tags, requestMessage *FimpMessage) *FimpMessage {
return NewMessage(type_, service, VTypeFloatMap, value, props, tags, requestMessage)
}
func NewMessageFromBytes(msg []byte) (*FimpMessage, error) {
fimpmsg := FimpMessage{}
var err error
fimpmsg.Type, err = jsonparser.GetString(msg, "type")
fimpmsg.Service, err = jsonparser.GetString(msg, "serv")
fimpmsg.ValueType, err = jsonparser.GetString(msg, "val_t")
fimpmsg.UID, _ = jsonparser.GetString(msg, "uid")
fimpmsg.CorrelationID, _ = jsonparser.GetString(msg, "corid")
fimpmsg.CreationTime, _ = jsonparser.GetString(msg, "ctime")
switch fimpmsg.ValueType {
case VTypeString:
fimpmsg.Value, err = jsonparser.GetString(msg, "val")
case VTypeBool:
fimpmsg.Value, err = jsonparser.GetBoolean(msg, "val")
case VTypeInt:
fimpmsg.Value, err = jsonparser.GetInt(msg, "val")
case VTypeFloat:
fimpmsg.Value, err = jsonparser.GetFloat(msg, "val")
case VTypeBoolArray:
val := []bool{}
jsonparser.ArrayEach(msg, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
item, _ := jsonparser.ParseBoolean(value)
val = append(val, item)
}, "val")
fimpmsg.Value = val
case VTypeStrArray:
val := []string{}
jsonparser.ArrayEach(msg, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
item, _ := jsonparser.ParseString(value)
val = append(val, item)
}, "val")
fimpmsg.Value = val
case VTypeIntArray:
val := []int64{}
jsonparser.ArrayEach(msg, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
item, _ := jsonparser.ParseInt(value)
val = append(val, item)
}, "val")
fimpmsg.Value = val
case VTypeFloatArray:
val := []float64{}
jsonparser.ArrayEach(msg, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
item, _ := jsonparser.ParseFloat(value)
val = append(val, item)
}, "val")
fimpmsg.Value = val
case VTypeStrMap:
val := make(map[string]string)
jsonparser.ObjectEach(msg, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
val[string(key)],err = jsonparser.ParseString(value)
return nil
}, "val")
fimpmsg.Value = val
case VTypeIntMap:
val := make(map[string]int64)
jsonparser.ObjectEach(msg, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
val[string(key)], err = jsonparser.ParseInt(value)
return nil
}, "val")
fimpmsg.Value = val
case VTypeFloatMap:
val := make(map[string]float64)
jsonparser.ObjectEach(msg, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
val[string(key)], err = jsonparser.ParseFloat(value)
return nil
}, "val")
fimpmsg.Value = val
case VTypeBoolMap:
val := make(map[string]bool)
jsonparser.ObjectEach(msg, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
val[string(key)], err = jsonparser.ParseBoolean(value)
return nil
}, "val")
fimpmsg.Value = val
case VTypeObject:
fimpmsg.ValueObj, _, _, err = jsonparser.Get(msg, "val")
}
fimpmsg.Properties = make(Props)
jsonparser.ObjectEach(msg, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
fimpmsg.Properties[string(key)],err = jsonparser.ParseString(value)
return nil
}, "props")
return &fimpmsg, err
}