-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
227 lines (212 loc) · 5.69 KB
/
helpers.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
package uniform
import (
"fmt"
"github.com/go-diary/diary"
"go.mongodb.org/mongo-driver/bson"
"strings"
"time"
)
func encode(model interface{}) ([]byte, error) {
// switch between common top value types and marshal using an adapter struct
switch value := model.(type) {
case nil:
return bson.Marshal(struct{ Value interface{} }{Value: nil})
case string:
return bson.Marshal(struct{ Value string }{Value: value})
case []byte:
return bson.Marshal(struct{ Value []byte }{Value: value})
case byte:
return bson.Marshal(struct{ Value byte }{Value: value})
case int:
return bson.Marshal(struct{ Value int }{Value: value})
case uint64:
return bson.Marshal(struct{ Value uint64 }{Value: value})
case int64:
return bson.Marshal(struct{ Value int64 }{Value: value})
case float64:
return bson.Marshal(struct{ Value float64 }{Value: value})
case uint32:
return bson.Marshal(struct{ Value uint32 }{Value: value})
case int32:
return bson.Marshal(struct{ Value int32 }{Value: value})
case float32:
return bson.Marshal(struct{ Value float32 }{Value: value})
case *string:
return bson.Marshal(struct{ Value *string }{Value: value})
case *[]byte:
return bson.Marshal(struct{ Value *[]byte }{Value: value})
case *byte:
return bson.Marshal(struct{ Value *byte }{Value: value})
case *int:
return bson.Marshal(struct{ Value *int }{Value: value})
case *uint64:
return bson.Marshal(struct{ Value *uint64 }{Value: value})
case *int64:
return bson.Marshal(struct{ Value *int64 }{Value: value})
case *float64:
return bson.Marshal(struct{ Value *float64 }{Value: value})
case *uint32:
return bson.Marshal(struct{ Value *uint32 }{Value: value})
case *int32:
return bson.Marshal(struct{ Value *int32 }{Value: value})
case *float32:
return bson.Marshal(struct{ Value *float32 }{Value: value})
}
// otherwise just use normal marshal method
return bson.Marshal(model)
}
func decode(data []byte, model interface{}) error {
// switch between common top value types and unmarshal using an adapter struct
switch value := model.(type) {
case *string:
var adapter struct{ Value string }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *[]byte:
var adapter struct{ Value []byte }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *byte:
var adapter struct{ Value byte }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *int:
var adapter struct{ Value int }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *uint64:
var adapter struct{ Value uint64 }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *int64:
var adapter struct{ Value int64 }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *float64:
var adapter struct{ Value float64 }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *uint32:
var adapter struct{ Value uint32 }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *int32:
var adapter struct{ Value int32 }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
case *float32:
var adapter struct{ Value float32 }
if err := bson.Unmarshal(data, &adapter); err != nil {
return err
}
*value = adapter.Value
return nil
}
// otherwise just use normal marshal method
return bson.Unmarshal(data, model)
}
func requestEncode(page diary.IPage, request Request, timeout time.Duration, startedAt time.Time) ([]byte, error) {
return encode(payload{
Request: request,
PageJson: page.ToJson(),
RequestTimeout: timeout,
RequestStartedAt: startedAt,
})
}
func requestDecode(conn IConn, d diary.IDiary, subject, replyChannel string, data []byte, scope S) {
var temp payload
if err := decode(data, &temp); err != nil {
panic(err)
}
if temp.Request.Error != "" {
panic(temp.Request.Error)
}
temp.conn = conn
temp.ReplyChannel = &replyChannel
temp.Subject = subject
if err := d.LoadX(temp.PageJson, subject, func(p diary.IPage) {
defer func() {
if rec := recover(); rec != nil {
var err error
message := ""
if value, ok := rec.(error); ok {
message = value.Error()
err = value
} else if value, ok := rec.(string); ok {
message = value
} else {
message = fmt.Sprint(rec)
}
p.Error("catch", message, diary.M{
"rec": rec,
"err": err,
})
if temp.CanReply() {
var isTimeoutError = strings.HasPrefix(message, ErrTimeout.Error())
var isCantReplyError = strings.HasPrefix(message, ErrCantReply.Error())
var isGeneralError = !isTimeoutError && !isCantReplyError
if isGeneralError {
if err := temp.Reply(Request{
Error: message,
}); err != nil {
p.Error("reply", err.Error(), diary.M{
"err": err,
})
}
}
}
}
}()
temp.page = p
if scope != nil {
scope(&temp, p)
}
}); err != nil {
panic(err)
}
}
func responseDecode(conn IConn, d diary.IDiary, subject, replyChannel string, data []byte, scope S) error {
var temp payload
if err := decode(data, &temp); err != nil {
return err
}
temp.conn = conn
temp.ReplyChannel = &replyChannel
temp.Subject = subject
if err := d.LoadX(temp.PageJson, subject, func(p diary.IPage) {
temp.page = p
if scope != nil {
scope(&temp, p)
}
}); err != nil {
return err
}
return nil
}