-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
74 lines (59 loc) · 1.17 KB
/
reply.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
package autonats
import (
"errors"
"github.com/json-iterator/go"
"sync"
)
var replyPool = &sync.Pool{
New: func() interface{} {
return new(Reply)
},
}
func GetReply() *Reply {
return replyPool.Get().(*Reply)
}
func PutReply(reply *Reply) {
reply.Reset()
replyPool.Put(reply)
}
type Reply struct {
Data []byte `json:"d,omitempty"`
Error []byte `json:"e,omitempty"`
}
func (r *Reply) MarshalBinary() ([]byte, error) {
return jsoniter.Marshal(r)
}
func (r *Reply) UnmarshalBinary(data []byte) error {
return jsoniter.Unmarshal(data, r)
}
func (r *Reply) WriteString(data string) {
r.Data = []byte(data)
}
func (r *Reply) SetData(data []byte) {
r.Data = data
}
func (r *Reply) MarshalAndSetData(data interface{}) error {
var err error
r.Data, err = jsoniter.Marshal(data)
return err
}
func (r *Reply) GetError() error {
if r.Error == nil {
return nil
} else {
return errors.New(string(r.Error))
}
}
func (r *Reply) GetDataAsString() string {
return string(r.Data)
}
func (r *Reply) UnmarshalData(vPtr interface{}) error {
if vPtr == nil {
return nil
}
return jsoniter.Unmarshal(r.Data, vPtr)
}
func (r *Reply) Reset() {
r.Data = nil
r.Error = nil
}