-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshaling.go
235 lines (188 loc) · 5.67 KB
/
marshaling.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
package httpsling
import (
"encoding/json"
"encoding/xml"
"fmt"
"mime"
"net/url"
"strings"
goquery "github.com/google/go-querystring/query"
)
var DefaultMarshaler Marshaler = &JSONMarshaler{}
var DefaultUnmarshaler Unmarshaler = NewContentTypeUnmarshaler()
// Marshaler marshals values into a []byte
type Marshaler interface {
Marshal(v interface{}) (data []byte, contentType string, err error)
}
// Unmarshaler unmarshals a []byte response body into a value
type Unmarshaler interface {
Unmarshal(data []byte, contentType string, v interface{}) error
}
// MarshalFunc adapts a function to the Marshaler interface
type MarshalFunc func(v interface{}) ([]byte, string, error)
// Apply implements Option
func (f MarshalFunc) Apply(r *Requester) error {
r.Marshaler = f
return nil
}
// Marshal implements the Marshaler interface
func (f MarshalFunc) Marshal(v interface{}) ([]byte, string, error) {
return f(v)
}
// UnmarshalFunc adapts a function to the Unmarshaler interface
type UnmarshalFunc func(data []byte, contentType string, v interface{}) error
// Apply implements Option
func (f UnmarshalFunc) Apply(r *Requester) error {
r.Unmarshaler = f
return nil
}
// Unmarshal implements the Unmarshaler interface
func (f UnmarshalFunc) Unmarshal(data []byte, contentType string, v interface{}) error {
return f(data, contentType, v)
}
// JSONMarshaler implement Marshaler and Unmarshaler
type JSONMarshaler struct {
Indent bool
}
// Unmarshal implements Unmarshaler
func (m *JSONMarshaler) Unmarshal(data []byte, _ string, v interface{}) error {
return json.Unmarshal(data, v)
}
// Marshal implements Marshaler
func (m *JSONMarshaler) Marshal(v interface{}) (data []byte, contentType string, err error) {
if m.Indent {
data, err = json.MarshalIndent(v, "", " ")
} else {
data, err = json.Marshal(v)
}
return data, ContentTypeJSONUTF8, err
}
// Apply implements Option
func (m *JSONMarshaler) Apply(r *Requester) error {
r.Marshaler = m
return nil
}
// XMLMarshaler implements Marshaler and Unmarshaler
type XMLMarshaler struct {
Indent bool
}
// Unmarshal implements Unmarshaler
func (*XMLMarshaler) Unmarshal(data []byte, _ string, v interface{}) error {
return xml.Unmarshal(data, v)
}
// Marshal implements Marshaler
func (m *XMLMarshaler) Marshal(v interface{}) (data []byte, contentType string, err error) {
if m.Indent {
data, err = xml.MarshalIndent(v, "", " ")
} else {
data, err = xml.Marshal(v)
}
return data, ContentTypeXMLUTF8, err
}
// Apply implements Option
func (m *XMLMarshaler) Apply(r *Requester) error {
r.Marshaler = m
return nil
}
// TextUnmarshaler implements Marshaler and Unmarshaler
type TextUnmarshaler struct {
Indent bool
}
// Unmarshal implements Unmarshaler
func (*TextUnmarshaler) Unmarshal(data []byte, _ string, v interface{}) error {
*(v.(*string)) = string(data)
return nil
}
// Marshal implements Marshaler
func (m *TextUnmarshaler) Marshal(v interface{}) (data []byte, contentType string, err error) {
data = []byte(fmt.Sprintf("%v", v))
return data, ContentTypeTextUTF8, nil
}
// Apply implements Option
func (m *TextUnmarshaler) Apply(r *Requester) error {
r.Marshaler = m
return nil
}
// FormMarshaler implements Marshaler
type FormMarshaler struct{}
// Marshal implements Marshaler
func (*FormMarshaler) Marshal(v interface{}) (data []byte, contentType string, err error) {
switch t := v.(type) {
case map[string][]string:
urlV := url.Values(t)
return []byte(urlV.Encode()), ContentTypeForm, nil
case map[string]string:
urlV := url.Values{}
for key, value := range t {
urlV.Set(key, value)
}
return []byte(urlV.Encode()), ContentTypeForm, nil
case url.Values:
return []byte(t.Encode()), ContentTypeForm, nil
default:
values, err := goquery.Values(v)
if err != nil {
return nil, "", fmt.Errorf("invalid form struct: %w", err)
}
return []byte(values.Encode()), ContentTypeForm, nil
}
}
// Apply implements Option
func (m *FormMarshaler) Apply(r *Requester) error {
r.Marshaler = m
return nil
}
// ContentTypeUnmarshaler selects an unmarshaler based on the content type
type ContentTypeUnmarshaler struct {
Unmarshalers map[string]Unmarshaler
}
// NewContentTypeUnmarshaler returns a new ContentTypeUnmarshaler preconfigured to
// handle application/json and application/xml
func NewContentTypeUnmarshaler() *ContentTypeUnmarshaler {
return &ContentTypeUnmarshaler{
Unmarshalers: defaultUnmarshalers(),
}
}
func defaultUnmarshalers() map[string]Unmarshaler {
return map[string]Unmarshaler{
ContentTypeJSON: &JSONMarshaler{},
ContentTypeXML: &XMLMarshaler{},
ContentTypeText: &TextUnmarshaler{},
}
}
// Unmarshal implements Unmarshaler
func (c *ContentTypeUnmarshaler) Unmarshal(data []byte, contentType string, v interface{}) error {
if c.Unmarshalers == nil {
c.Unmarshalers = defaultUnmarshalers()
}
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return fmt.Errorf(" %w: failed to parse content type: %s", err, contentType)
}
if u := c.Unmarshalers[mediaType]; u != nil {
return u.Unmarshal(data, contentType, v)
}
if ct := generalMediaType(mediaType); ct != "" {
if u := c.Unmarshalers[ct]; u != nil {
return u.Unmarshal(data, contentType, v)
}
}
return fmt.Errorf("%w: %s", ErrUnsupportedContentType, contentType)
}
// Apply implements Option
func (c *ContentTypeUnmarshaler) Apply(r *Requester) error {
r.Unmarshaler = c
return nil
}
// generalMediaType will return a media type with just the suffix as the subtype, e.g.
// application/vnd.api+json -> application/json
func generalMediaType(s string) string {
i2 := strings.LastIndex(s, "+")
if i2 > -1 && len(s) > i2+1 {
i := strings.Index(s, "/")
if i > -1 {
return s[:i+1] + s[i2+1:]
}
}
return ""
}