-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvelope.go
325 lines (283 loc) · 6.75 KB
/
envelope.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
package flam
import (
"fmt"
"net/http"
"strconv"
"strings"
)
// EnvelopeWriter @todo doc
type EnvelopeWriter interface {
WriteEnvelope(env Envelope) error
}
// EnvelopeError @todo doc
type EnvelopeError interface {
GetStatus() int
SetStatus(val int) EnvelopeError
SetServiceID(val int) EnvelopeError
SetEndpointID(val int) EnvelopeError
SetParamID(param int) EnvelopeError
SetErrorID(id any) EnvelopeError
GetID() string
GetContext(key string) interface{}
SetContext(key string, value interface{}) EnvelopeError
}
// NewEnvelopeError @todo doc
func NewEnvelopeError(status int, e error, ctx ...Bag) EnvelopeError {
context := Bag{"message": e.Error()}
for _, c := range ctx {
context.Merge(c)
}
return (&envelopeError{
status: status,
context: context,
}).compose()
}
type envelopeError struct {
status int
id string
serviceID int
endpointID int
paramID int
errorID string
context Bag
}
var _ EnvelopeError = &envelopeError{}
func (e *envelopeError) GetStatus() int {
return e.status
}
func (e *envelopeError) SetStatus(val int) EnvelopeError {
e.status = val
return e
}
func (e *envelopeError) SetServiceID(val int) EnvelopeError {
e.serviceID = val
return e.compose()
}
func (e *envelopeError) SetEndpointID(val int) EnvelopeError {
e.endpointID = val
return e.compose()
}
func (e *envelopeError) SetParamID(param int) EnvelopeError {
e.paramID = param
return e.compose()
}
func (e *envelopeError) SetErrorID(id any) EnvelopeError {
e.errorID = fmt.Sprintf("%v", id)
return e.compose()
}
func (e *envelopeError) GetID() string {
return e.id
}
func (e *envelopeError) GetContext(key string) interface{} {
return e.context[key]
}
func (e *envelopeError) SetContext(key string, value interface{}) EnvelopeError {
e.context[key] = value
return e
}
func (e *envelopeError) compose() *envelopeError {
cb := strings.Builder{}
if e.serviceID != 0 {
cb.WriteString(fmt.Sprintf("s:%d", e.serviceID))
}
if e.endpointID != 0 {
if cb.Len() != 0 {
cb.WriteString(".")
}
cb.WriteString(fmt.Sprintf("e:%d", e.endpointID))
}
if e.paramID != 0 {
if cb.Len() != 0 {
cb.WriteString(".")
}
cb.WriteString(fmt.Sprintf("p:%d", e.paramID))
}
if e.errorID != "" {
if cb.Len() != 0 {
cb.WriteString(".")
}
if i, ee := strconv.Atoi(e.errorID); ee != nil {
cb.WriteString(e.errorID)
} else {
cb.WriteString(fmt.Sprintf("c:%d", i))
}
}
e.id = cb.String()
return e
}
type envelopeListReport interface{}
// PageListReport @todo doc
type PageListReport struct {
Search string `json:"search,omitempty"`
Start int64 `json:"start"`
Count int64 `json:"count"`
Total int64 `json:"total"`
Prev string `json:"prev,omitempty"`
Next string `json:"next,omitempty"`
}
func newEnvelopePageListReport(search string, start, count, total int64) envelopeListReport {
report := &PageListReport{
Search: search,
Start: start,
Count: count,
Total: total,
Prev: "",
Next: "",
}
if start > 0 {
nStart := int64(0)
if count < start {
nStart = start - count
}
report.Prev = fmt.Sprintf("?search=%s&start=%d&count=%d", search, nStart, count)
}
if start+count < total {
report.Next = fmt.Sprintf("?search=%s&start=%d&count=%d", search, start+count, count)
}
return report
}
// Envelope @todo doc
type Envelope interface {
SetServiceID(val int) Envelope
SetEndpointID(val int) Envelope
AddError(status int, e error, ctx ...Bag) EnvelopeError
SetData(id string, data interface{}) Envelope
SetPageListReport(search string, start, count, total int64) Envelope
}
// NewEnvelope @todo doc
func NewEnvelope(data ...interface{}) Envelope {
env := &envelope{}
if len(data) > 0 {
env.Data = data[0]
}
return env
}
type envelope struct {
Errors []EnvelopeError `json:"errors,omitempty"`
Data interface{} `json:"data,omitempty"`
Filter interface{} `json:"filter,omitempty"`
}
func (env *envelope) SetServiceID(val int) Envelope {
for _, e := range env.Errors {
e.SetServiceID(val)
}
return env
}
func (env *envelope) SetEndpointID(val int) Envelope {
for _, e := range env.Errors {
e.SetEndpointID(val)
}
return env
}
func (env *envelope) AddError(status int, e error, ctx ...Bag) EnvelopeError {
envErr := NewEnvelopeError(status, e, ctx...)
env.Errors = append(env.Errors, envErr)
return envErr
}
func (env *envelope) SetData(id string, data interface{}) Envelope {
if _, ok := env.Data.(*Bag); !ok {
env.Data = &Bag{}
}
env.Data.(*Bag).Merge(Bag{id: data})
return env
}
func (env *envelope) SetPageListReport(search string, start, count, total int64) Envelope {
env.Filter = newEnvelopePageListReport(search, start, count, total)
return env
}
// ProblemEnvelope @todo doc
type ProblemEnvelope interface {
GetStatus() int
GetID() string
GetType() string
GetTitle() string
GetDetail() string
GetInstance() string
Get(key string) interface{}
Set(key string, value interface{}) ProblemEnvelope
}
// NewProblemEnvelope @todo doc
func NewProblemEnvelope(status int, typeURI, title, details, instance string, ctx ...Bag) ProblemEnvelope {
pe := &problemEnvelope{}
for _, c := range ctx {
for k, v := range c {
pe.Set(k, v)
}
}
pe.Set("status", status)
pe.Set("type", typeURI)
pe.Set("title", title)
pe.Set("detail", details)
pe.Set("instance", instance)
return pe
}
// NewProblemEnvelopeFrom @todo doc
func NewProblemEnvelopeFrom(env Envelope) ProblemEnvelope {
pe := problemEnvelope{
"status": http.StatusInternalServerError,
"message": "unknown error",
}
if len(env.(*envelope).Errors) > 0 {
e := env.(*envelope).Errors[0]
for k, v := range e.(*envelopeError).context {
pe[k] = v
}
pe["status"] = e.GetStatus()
pe["id"] = e.GetID()
}
status := pe["status"].(int)
pe["type"] = fmt.Sprintf("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/%d", status)
pe["title"] = http.StatusText(status)
pe["detail"] = pe["message"]
return &pe
}
type problemEnvelope Bag
func (pe *problemEnvelope) GetStatus() int {
v, ok := pe.Get("status").(int)
if !ok {
return 0
}
return v
}
func (pe *problemEnvelope) GetID() string {
v, ok := pe.Get("id").(string)
if !ok {
return ""
}
return v
}
func (pe *problemEnvelope) GetType() string {
v, ok := pe.Get("type").(string)
if !ok {
return ""
}
return v
}
func (pe *problemEnvelope) GetTitle() string {
v, ok := pe.Get("title").(string)
if !ok {
return ""
}
return v
}
func (pe *problemEnvelope) GetDetail() string {
v, ok := pe.Get("detail").(string)
if !ok {
return ""
}
return v
}
func (pe *problemEnvelope) GetInstance() string {
v, ok := pe.Get("instance").(string)
if !ok {
return ""
}
return v
}
func (pe *problemEnvelope) Get(key string) interface{} {
return (*pe)[key]
}
func (pe *problemEnvelope) Set(key string, value interface{}) ProblemEnvelope {
(*pe)[key] = value
return pe
}