-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoding.go
82 lines (77 loc) · 1.99 KB
/
decoding.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
package errors
import (
"github.com/valyala/fastjson"
)
func UnmarshalJSON(data []byte) ([]Error, error) {
if len(data) == 0 {
return nil, nil
}
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return nil, err
}
v := val.Get("errors")
if v == nil {
return nil, wrap(nil, "invalid errors array")
}
items := make([]Error, 0)
switch v.Type() {
case fastjson.TypeArray:
for _, v := range v.GetArray() {
status := v.GetInt("status")
localErr := errorFromStatus(status)
if err := localErr.UnmarshalJSON([]byte(v.String())); err == nil {
items = append(items, localErr)
}
}
return items, err
case fastjson.TypeObject:
status := v.GetInt("status")
localErr := errorFromStatus(status)
if err := localErr.UnmarshalJSON([]byte(v.String())); err == nil {
items = append(items, localErr)
}
case fastjson.TypeString:
it := new(Err)
it.m = string(data)
items = append(items, it)
}
return items, nil
}
func (e *Err) UnmarshalJSON(data []byte) error {
if m := fastjson.GetString(data, "message"); len(m) > 0 {
e.m = m
}
return nil
}
func (n *notFound) UnmarshalJSON(data []byte) error {
return n.Err.UnmarshalJSON(data)
}
func (m *methodNotAllowed) UnmarshalJSON(data []byte) error {
return m.Err.UnmarshalJSON(data)
}
func (n *notValid) UnmarshalJSON(data []byte) error {
return n.Err.UnmarshalJSON(data)
}
func (f *forbidden) UnmarshalJSON(data []byte) error {
return f.Err.UnmarshalJSON(data)
}
func (n *notImplemented) UnmarshalJSON(data []byte) error {
return n.Err.UnmarshalJSON(data)
}
func (b *badRequest) UnmarshalJSON(data []byte) error {
return b.Err.UnmarshalJSON(data)
}
func (u *unauthorized) UnmarshalJSON(data []byte) error {
return u.Err.UnmarshalJSON(data)
}
func (n *notSupported) UnmarshalJSON(data []byte) error {
return n.Err.UnmarshalJSON(data)
}
func (t *timeout) UnmarshalJSON(data []byte) error {
return t.Err.UnmarshalJSON(data)
}
func (b *badGateway) UnmarshalJSON(data []byte) error {
return b.Err.UnmarshalJSON(data)
}