-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery_form.go
More file actions
50 lines (43 loc) · 1.43 KB
/
query_form.go
File metadata and controls
50 lines (43 loc) · 1.43 KB
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
package echopen
import (
"fmt"
"reflect"
v310 "github.com/richjyoung/echopen/openapi/v3.1.0"
)
// WithQueryStruct extracts type information from a provided struct to populate the OpenAPI operation parameters.
// A bound struct of the same type is added to the context under the key "query" during each request
func WithQueryStruct(target interface{}) RouteConfigFunc {
t := reflect.TypeOf(target)
if t.Kind() != reflect.Struct {
panic(fmt.Errorf("echopen: struct expected, received %s", t.Kind()))
}
return func(rw *RouteWrapper) *RouteWrapper {
s := rw.API.StructTypeToSchema(t, "query")
rw.QuerySchema = s
for name, prop := range s.Properties {
rw.Operation.AddParameter(&v310.Parameter{
Name: name,
In: "query",
Required: false,
Description: prop.Value.Description,
Style: "form",
Schema: &v310.Schema{
Type: prop.Value.Type,
Items: prop.Value.Items,
Enum: prop.Value.Enum,
Default: prop.Value.Default,
},
})
}
return rw
}
}
// WithFormStruct extracts type information from a provided struct to populate the OpenAPI operation parameters.
// A bound struct of the same type is added to the context under the key "form" during each request
// Binding will use either request body or query params (GET/DELETE only)
func WithFormStruct(target interface{}) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
// TODO
return rw
}
}