forked from weikaishio/ali_mns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.go
85 lines (67 loc) · 1.66 KB
/
decoder.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
package ali_mns
import (
"bytes"
"encoding/xml"
"io"
)
type MNSDecoder interface {
Decode(reader io.Reader, v interface{}) (err error)
DecodeError(bodyBytes []byte, resource string) (decodedError error, err error)
Test() bool
}
type aliMNSDecoder struct {
}
type batchOpDecoder struct {
v interface{}
}
func NewAliMNSDecoder() MNSDecoder {
return &aliMNSDecoder{}
}
func (p *aliMNSDecoder) Test() bool {
return false
}
func (p *batchOpDecoder) Test() bool {
return true
}
func (p *aliMNSDecoder) Decode(reader io.Reader, v interface{}) (err error) {
decoder := xml.NewDecoder(reader)
err = decoder.Decode(&v)
return
}
func (p *aliMNSDecoder) DecodeError(bodyBytes []byte, resource string) (decodedError error, err error) {
bodyReader := bytes.NewReader(bodyBytes)
errResp := ErrorResponse{}
decoder := xml.NewDecoder(bodyReader)
err = decoder.Decode(&errResp)
if err == nil {
decodedError = ParseError(errResp, resource)
}
return
}
func NewBatchOpDecoder(v interface{}) MNSDecoder {
return &batchOpDecoder{v: v}
}
func (p *batchOpDecoder) Decode(reader io.Reader, v interface{}) (err error) {
decoder := xml.NewDecoder(reader)
err = decoder.Decode(&v)
if err == io.EOF {
err = nil
}
return
}
func (p *batchOpDecoder) DecodeError(bodyBytes []byte, resource string) (decodedError error, err error) {
bodyReader := bytes.NewReader(bodyBytes)
decoder := xml.NewDecoder(bodyReader)
err = decoder.Decode(&p.v)
if err != nil {
bodyReader.Seek(0, 0)
errResp := ErrorResponse{}
err = decoder.Decode(&errResp)
if err == nil {
decodedError = ParseError(errResp, resource)
}
} else {
decodedError = ERR_MNS_BATCH_OP_FAIL.New()
}
return
}