-
Notifications
You must be signed in to change notification settings - Fork 2
/
args.go
219 lines (183 loc) · 3.77 KB
/
args.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
package xweb
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"os"
"reflect"
"runtime"
"github.com/cxuhua/xweb/martini"
)
var (
FormFileType = reflect.TypeOf(FormFile{})
)
type protoError struct {
Code int
Message string
File string
}
func (this protoError) GetCode() string {
return fmt.Sprintf("%d", this.Code)
}
func (this protoError) Error() string {
if this.File != "" {
return this.Message + "\n" + this.File
}
return this.Message
}
func ProtoError(code int, format string, args ...interface{}) *protoError {
if code == 0 {
panic(errors.New("code != 0,0 is success"))
}
// 返回错误信息
r := &protoError{}
r.Code = code
r.Message = fmt.Sprintf(format, args...)
// 开发模式返回出错位置
if martini.Env == martini.Dev {
_, file, line, _ := runtime.Caller(1)
r.File = fmt.Sprintf("%s:%d", file, line)
}
return r
}
type FormFile struct {
*multipart.FileHeader
}
func (this FormFile) Write(data []byte, pfunc func(string) string) (string, error) {
md5 := MD5Bytes(data)
path := pfunc(md5)
//exists file check
if info, err := os.Stat(path); err == nil && info.Size() > 0 {
return md5, nil
}
return md5, ioutil.WriteFile(path, data, 0666)
}
func (this FormFile) ToJson(v interface{}) error {
data, err := this.ReadAll()
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
func (this FormFile) ToXml(v interface{}) error {
data, err := this.ReadAll()
if err != nil {
return err
}
return xml.Unmarshal(data, v)
}
func (this FormFile) ToString() (string, error) {
data, err := this.ReadAll()
return string(data), err
}
//read file data
func (this FormFile) ReadAll() ([]byte, error) {
if this.FileHeader == nil {
return nil, errors.New("file header nil")
}
f, err := this.FileHeader.Open()
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
//req type
const (
AT_NONE = iota
AT_FORM //表单数据解析 use:form tag
AT_JSON //json数据解析 use:json tag
AT_XML //xml数据解析 use:xml tag
AT_URL //url可以和以上结构体混用 use:url tag
)
type IArgs interface {
//是否校验参数
IsValidate() bool
//参数校验失败
Validate(*ValidateModel, IMVC) error
//参数解析类型
ReqType() int
//返回默认的输出模型
Model() IModel
//body sha256
GetSignBytes() []byte
//put sign bytes
PutSignBytes(data []byte) error
}
type xArgs struct {
data []byte
IArgs
}
func (this *xArgs) PutSignBytes(data []byte) error {
this.data = data
return nil
}
func (this *xArgs) GetSignBytes() []byte {
return this.data
}
func (this *xArgs) Model() IModel {
return &HtmlModel{}
}
func (this *xArgs) ReqType() int {
return AT_NONE
}
func (this *xArgs) IsValidate() bool {
return true
}
func (this *xArgs) Validate(m *ValidateModel, c IMVC) error {
v := &StringModel{Text: m.ToTEXT()}
c.SetModel(v)
c.SetRender(TEXT_RENDER)
return nil
}
type URLArgs struct {
xArgs
}
func (this *URLArgs) ReqType() int {
return AT_URL
}
type FORMArgs struct {
xArgs
}
func (this *FORMArgs) Validate(m *ValidateModel, c IMVC) error {
c.SetModel(m)
c.SetRender(JSON_RENDER)
return nil
}
func (this *FORMArgs) ReqType() int {
return AT_FORM
}
func (this *FORMArgs) Model() IModel {
return &HTTPModel{}
}
type JSONArgs struct {
xArgs
}
func (this *JSONArgs) Validate(m *ValidateModel, c IMVC) error {
c.SetModel(m)
c.SetRender(JSON_RENDER)
return nil
}
func (this *JSONArgs) ReqType() int {
return AT_JSON
}
func (this *JSONArgs) Model() IModel {
return NewHTTPSuccess()
}
type XMLArgs struct {
xArgs
}
func (this *XMLArgs) Validate(m *ValidateModel, c IMVC) error {
c.SetModel(m)
c.SetRender(XML_RENDER)
return nil
}
func (this *XMLArgs) ReqType() int {
return AT_XML
}
func (this *XMLArgs) Model() IModel {
return NewHTTPSuccess()
}