/
helper.go
276 lines (241 loc) · 8.14 KB
/
helper.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2017 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package definition
import (
"context"
"fmt"
"reflect"
)
// MIME types
const (
// MIMEAll indicates a accept type from http request.
// It means client can receive any content.
// Request content type in header "Content-Type" must not set to "*/*".
// It only can exist in request header "Accept".
// In most time, it locate at the last element of "Accept".
// It's default value if client have not set "Accept" header.
MIMEAll = "*/*"
MIMENone = ""
MIMEText = "text/plain"
MIMEHTML = "text/html"
MIMEJSON = "application/json"
MIMEXML = "application/xml"
MIMEOctetStream = "application/octet-stream"
MIMEURLEncoded = "application/x-www-form-urlencoded"
MIMEFormData = "multipart/form-data"
)
// DataErrorResults returns the most frequently-used results.
// Definition function should have two results. The first is
// any type for data, and the last is error.
func DataErrorResults(description string) []Result {
return []Result{
{
Destination: Data,
Description: description,
},
{
Destination: Error,
},
}
}
// ParameterFor creates a simple parameter.
func ParameterFor(source Source, name string, description string, operators ...Operator) Parameter {
return Parameter{
Source: source,
Name: name,
Description: description,
Operators: operators,
}
}
// PathParameterFor creates a path parameter
func PathParameterFor(name string, description string, operators ...Operator) Parameter {
return ParameterFor(Path, name, description, operators...)
}
// QueryParameterFor creates a query parameter
func QueryParameterFor(name string, description string, operators ...Operator) Parameter {
return ParameterFor(Query, name, description, operators...)
}
// HeaderParameterFor creates a header parameter
func HeaderParameterFor(name string, description string, operators ...Operator) Parameter {
return ParameterFor(Header, name, description, operators...)
}
// FormParameterFor creates a form parameter
func FormParameterFor(name string, description string, operators ...Operator) Parameter {
return ParameterFor(Form, name, description, operators...)
}
// FileParameterFor creates a file parameter
func FileParameterFor(name string, description string, operators ...Operator) Parameter {
return ParameterFor(File, name, description, operators...)
}
// BodyParameterFor creates a body parameter
func BodyParameterFor(description string, operators ...Operator) Parameter {
return ParameterFor(Body, "", description, operators...)
}
// PrefabParameterFor creates a prefab parameter
func PrefabParameterFor(name string, description string, operators ...Operator) Parameter {
return ParameterFor(Prefab, name, description, operators...)
}
// AutoParameterFor creates an auto parameter
func AutoParameterFor(description string, operators ...Operator) Parameter {
return ParameterFor(Auto, "", description, operators...)
}
// ResultFor creates a simple result.
func ResultFor(dest Destination, description string, operators ...Operator) Result {
return Result{
Destination: dest,
Description: description,
Operators: operators,
}
}
// MetaResultFor creates meta result.
func MetaResultFor(description string, operators ...Operator) Result {
return ResultFor(Meta, description, operators...)
}
// DataResultFor creates data result.
func DataResultFor(description string, operators ...Operator) Result {
return ResultFor(Data, description, operators...)
}
// ErrorResult creates error result.
func ErrorResult() Result {
return ResultFor(Error, "")
}
// OperatorFunc creates operator by function.
// function must has signature:
// func f(context.Context, string, AnyType) (AnyType, error)
// The second parameter is a string that is used to identify field.
// AnyType can be any type in go. But struct type and
// built-in data type is recommended.
func OperatorFunc(kind string, f interface{}) Operator {
typ := reflect.TypeOf(f)
if typ.Kind() != reflect.Func {
panic(fmt.Sprintf("Parameter f in OperatorFunc must be a function, but got: %s", typ.Kind()))
}
if typ.NumIn() != 3 {
panic(fmt.Sprintf("Function must have 3 parameters, but got: %d", typ.NumIn()))
}
if typ.In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() {
panic(fmt.Sprintf("The first parameter of function must be context.Context, but got: %v", typ.In(0)))
}
if typ.In(1) != reflect.TypeOf("") {
panic(fmt.Sprintf("The second parameter of function must be string, but got: %v", typ.In(0)))
}
if typ.NumOut() != 2 {
panic(fmt.Sprintf("Parameter f in OperatorFunc must have two results, but got: %d", typ.NumOut()))
}
if typ.Out(1).String() != "error" {
panic(fmt.Sprintf("The last result of parameter f in OperatorFunc must be error, but got: %v", typ.Out(1)))
}
return &operatorRef{
kind: kind,
in: typ.In(2),
out: typ.Out(0),
value: reflect.ValueOf(f),
}
}
// NewOperator creates operator by function.
// function must has signature:
// func f(context.Context, AnyType) (AnyType, error)
// AnyType can be any type in go. But struct type and
// built-in data type is recommended.
func NewOperator(kind string, in, out reflect.Type, f func(ctx context.Context, field string, object interface{}) (interface{}, error)) Operator {
return &operator{
kind: kind,
in: in,
out: out,
f: f,
}
}
type operator struct {
kind string
in reflect.Type
out reflect.Type
f func(ctx context.Context, field string, object interface{}) (interface{}, error)
}
// Kind indicates operator type.
func (o *operator) Kind() string {
return o.kind
}
// In returns the type of the only object parameter of operator.
func (o *operator) In() reflect.Type {
return o.in
}
// Out returns the type of the only object result of operator.
func (o *operator) Out() reflect.Type {
return o.out
}
// Operate operates an object and return one.
func (o *operator) Operate(ctx context.Context, field string, object interface{}) (interface{}, error) {
return o.f(ctx, field, object)
}
type operatorRef struct {
kind string
in reflect.Type
out reflect.Type
value reflect.Value
}
// Kind indicates operator type.
func (o *operatorRef) Kind() string {
return o.kind
}
// In returns the type of the only object parameter of operator.
func (o *operatorRef) In() reflect.Type {
return o.in
}
// Out returns the type of the only object result of operator.
func (o *operatorRef) Out() reflect.Type {
return o.out
}
// Operate operates an object and return one.
func (o *operatorRef) Operate(ctx context.Context, field string, object interface{}) (interface{}, error) {
var objectValue reflect.Value
if object == nil {
objectValue = reflect.Zero(o.in)
} else {
objectValue = reflect.ValueOf(object)
}
results := o.value.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(field), objectValue})
v := results[1]
if v.IsNil() {
return results[0].Interface(), nil
}
return nil, results[1].Interface().(error)
}
// SimpleDescriptor creates a simple REST descriptor for handler.
// The descriptor consumes all content types and produces all accept types.
func SimpleDescriptor(method Method, path string, f interface{}) Descriptor {
return Descriptor{
Path: path,
Definitions: []Definition{
{
Method: method,
Function: f,
Consumes: []string{MIMEAll},
Produces: []string{MIMEAll},
},
},
}
}
// SimpleRPCDescriptor creates a simple RPC descriptor for handler.
// The descriptor consumes all content types and produces all accept types.
func SimpleRPCDescriptor(path string, f interface{}) RPCDescriptor {
return RPCDescriptor{
Path: path,
Actions: []RPCAction{
{
Function: f,
Consumes: []string{MIMEAll},
Produces: []string{MIMEAll},
},
},
}
}