-
Notifications
You must be signed in to change notification settings - Fork 4
/
implements.go
130 lines (112 loc) · 2.78 KB
/
implements.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
package webapi
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
)
//JSONSerializer JSON Serializer
var (
Serializers = map[string]Serializer{
"application/x-www-form-urlencoded": &formSerializer{},
"application/json": &jsonSerializer{},
"application/xml": &xmlSerializer{},
"": &jsonSerializer{},
}
)
type (
xmlSerializer struct{}
jsonSerializer struct{}
formSerializer struct{}
responsewriter struct {
ctx *Context
}
)
func (*xmlSerializer) Marshal(obj interface{}) ([]byte, error) {
return xml.Marshal(obj)
}
func (*xmlSerializer) Unmarshal(src []byte, obj interface{}) error {
return xml.Unmarshal(src, obj)
}
func (*xmlSerializer) ContentType() string {
return "application/xml; charset=utf-8"
}
func (*jsonSerializer) Marshal(obj interface{}) ([]byte, error) {
return json.Marshal(obj)
}
func (*jsonSerializer) Unmarshal(src []byte, obj interface{}) error {
return json.Unmarshal(src, obj)
}
func (*jsonSerializer) ContentType() string {
return "application/json; charset=utf-8"
}
func (*formSerializer) Marshal(obj interface{}) ([]byte, error) {
src, err := json.Marshal(obj)
kv := map[string]interface{}{}
if err == nil {
err = json.Unmarshal(src, &kv)
}
if err != nil {
return nil, err
}
var values = url.Values{}
for k, v := range kv {
if t := reflect.TypeOf(v).Kind(); t == reflect.Map || t == reflect.Struct {
continue
}
values.Set(k, fmt.Sprintf("%v", v))
}
return []byte(values.Encode()), nil
}
func (*formSerializer) Unmarshal(src []byte, obj interface{}) error {
val := reflect.ValueOf(obj)
if val.Type().Kind() == reflect.Struct || !val.Elem().CanSet() {
return errors.New("type " + val.Type().String() + " is readonly")
}
values, err := url.ParseQuery(string(src))
if err == nil {
p := ¶m{
Type: reflect.TypeOf(obj),
}
var value *reflect.Value
value, err = p.loadFromValues(values)
if err == nil {
reflect.ValueOf(obj).Elem().Set(value.Elem())
}
}
return err
}
func (*formSerializer) ContentType() string {
return "application/x-www-form-urlencoded; charset=utf-8"
}
// Reply Default implementation of Response
type Reply struct {
Status int
Body interface{}
}
//StatusCode HTTP Status Code
func (reply Reply) StatusCode() int {
return reply.Status
}
//Data Body
func (reply Reply) Data() interface{} {
return reply.Body
}
func (w *responsewriter) Write(p []byte) (int, error) {
defer func() {
if w.ctx.statuscode == 0 {
w.ctx.statuscode = 200 //mark data has been transferred
}
}()
return w.ctx.w.Write(p)
}
func (w *responsewriter) Header() http.Header {
return w.ctx.w.Header()
}
func (w *responsewriter) WriteHeader(statusCode int) {
w.ctx.statuscode = statusCode
w.ctx.w.WriteHeader(statusCode)
}