-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-serializers.go
181 lines (157 loc) · 4.23 KB
/
base-serializers.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
package just
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"mime/multipart"
"net/url"
)
var (
ErrSerializeOperationsDisabled = errors.New("serialize operations disabled")
)
// Base JSON serializer.
type JsonSerializer struct {
Ch string // Charset for generate Content-Type header.
}
func (JsonSerializer) Name() string {
return "json"
}
func (s JsonSerializer) Charset() string {
return s.Ch
}
func (s JsonSerializer) DefaultContentType(withCharset bool) string {
if withCharset && len(s.Ch) > 0 {
return "application/json; charset=" + s.Ch
}
return "application/json"
}
func (JsonSerializer) Serialize(v interface{}) ([]byte, error) {
if input, ok := v.(ISerializeInput); ok {
v = input.Data()
}
if IsDebug() {
return json.MarshalIndent(v, "", " ")
}
return json.Marshal(v)
}
func (JsonSerializer) Deserialize(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (s JsonSerializer) Response(status int, data interface{}) IResponse {
b, err := s.Serialize(data)
if err != nil {
return JsonResponse(500, NewError("U500", "Error serialize data to JSON").SetMetadata(H{"error": err.Error()}))
}
return &Response{
Status: status,
Bytes: b,
Headers: map[string]string{"Content-Type": s.DefaultContentType(true)},
}
}
// Base XML serializer.
type XmlSerializer struct {
Ch string // Charset for generate Content-Type header.
}
func (XmlSerializer) Name() string {
return "xml"
}
func (s XmlSerializer) Charset() string {
return s.Ch
}
func (s XmlSerializer) DefaultContentType(withCharset bool) string {
if withCharset && len(s.Ch) > 0 {
return "application/xml; charset=" + s.Ch
}
return "application/xml"
}
func (XmlSerializer) Serialize(v interface{}) ([]byte, error) {
if input, ok := v.(ISerializeInput); ok {
v = input.Data()
}
if IsDebug() {
return xml.MarshalIndent(v, "", " ")
}
return xml.Marshal(v)
}
func (XmlSerializer) Deserialize(data []byte, v interface{}) error {
return xml.Unmarshal(data, v)
}
func (s XmlSerializer) Response(status int, data interface{}) IResponse {
b, err := s.Serialize(data)
if err != nil {
return XmlResponse(500, NewError("U500", "Error serialize data to XML").SetMetadata(H{"error": err.Error()}))
}
return &Response{
Status: status,
Bytes: b,
Headers: map[string]string{"Content-Type": s.DefaultContentType(true)},
}
}
const (
defaultMaxMultipartSize = 32 << 20
defaultMaxUrlSize = 10 << 20
)
// Form serializer (form-data, x-www-form-urlencoded).
type FormSerializer struct {
Ch string
OnlyDeserialize bool
}
func (FormSerializer) Name() string {
return "form"
}
func (s FormSerializer) Charset() string {
return s.Ch
}
func (s FormSerializer) DefaultContentType(withCharset bool) string {
if withCharset && len(s.Ch) > 0 {
return "application/x-www-form-urlencoded; charset=" + s.Ch
}
return "application/x-www-form-urlencoded"
}
func (s FormSerializer) Serialize(v interface{}) ([]byte, error) {
if s.OnlyDeserialize {
return nil, ErrSerializeOperationsDisabled
}
if input, ok := v.(ISerializeInput); ok {
v = input.Data()
}
return marshalUrlValues(v)
}
func (FormSerializer) Deserialize(data []byte, v interface{}) error {
if len(data) <= defaultMaxUrlSize && bytes.IndexByte(data, '\n') < 0 {
values, err := url.ParseQuery(string(data))
if err != nil {
return err
}
return mapForm(values, nil, v)
}
if end := bytes.LastIndex(data, []byte("--")); end > 0 {
if start := bytes.LastIndex(data[:end], []byte("\n--")); start > 0 && end > start {
if boundary := string(data[start+3 : end]); len(boundary) > 0 {
r := multipart.NewReader(bytes.NewReader(data), boundary)
form, err := r.ReadForm(defaultMaxMultipartSize)
if err != nil {
return err
}
return mapForm(form.Value, form.File, v)
}
}
}
return nil
}
func (s FormSerializer) Response(status int, data interface{}) IResponse {
b, err := s.Serialize(data)
if err != nil {
return &Response{
Status: 500,
Bytes: []byte("U500. Error serialize data to form urlencoded\r\n" + err.Error()),
Headers: map[string]string{"Content-Type": "text/plain; charset=utf-8"},
}
}
return &Response{
Status: status,
Bytes: b,
Headers: map[string]string{"Content-Type": s.DefaultContentType(true)},
}
}