forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
68 lines (54 loc) · 1.12 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
package workers
import (
"reflect"
"github.com/bitly/go-simplejson"
)
type data struct {
*simplejson.Json
}
type Msg struct {
*data
original string
}
type Args struct {
*data
}
func (m *Msg) Jid() string {
return m.Get("jid").MustString()
}
func (m *Msg) Args() *Args {
if args, ok := m.CheckGet("args"); ok {
return &Args{&data{args}}
} else {
d, _ := newData("[]")
return &Args{d}
}
}
func (m *Msg) OriginalJson() string {
return m.original
}
func (d *data) ToJson() string {
json, err := d.Encode()
if err != nil {
Logger.Println("ERR: Couldn't generate json from", d, ":", err)
}
return string(json)
}
func (d *data) Equals(other interface{}) bool {
otherJson := reflect.ValueOf(other).MethodByName("ToJson").Call([]reflect.Value{})
return d.ToJson() == otherJson[0].String()
}
func NewMsg(content string) (*Msg, error) {
if d, err := newData(content); err != nil {
return nil, err
} else {
return &Msg{d, content}, nil
}
}
func newData(content string) (*data, error) {
if json, err := simplejson.NewJson([]byte(content)); err != nil {
return nil, err
} else {
return &data{json}, nil
}
}