-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec.go
More file actions
123 lines (105 loc) · 2.85 KB
/
spec.go
File metadata and controls
123 lines (105 loc) · 2.85 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
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
package r5t
import (
"github.com/the-pawn-2017/r5t/api"
"github.com/the-pawn-2017/r5t/model"
"github.com/the-pawn-2017/r5t/path"
"github.com/the-pawn-2017/r5t/security"
"github.com/the-pawn-2017/r5t/spec"
"github.com/getkin/kin-openapi/openapi3"
"gopkg.in/yaml.v3"
)
type Spec struct {
// all information
root openapi3.T
}
func (s *Spec) MarshalJSON() ([]byte, error) {
return s.root.MarshalJSON()
}
func (s *Spec) MarshalYAML() ([]byte, error) {
return yaml.Marshal(s.root)
}
func (s *Spec) UnMarshalYAML(src []byte) error {
return yaml.Unmarshal(src, &s.root)
}
// 新建一个Spec
func NewSpec(specs ...spec.SpecOpts) *Spec {
var s Spec
s.root = openapi3.T{
Info: &openapi3.Info{},
Components: &openapi3.Components{
Schemas: make(openapi3.Schemas),
SecuritySchemes: make(openapi3.SecuritySchemes),
},
OpenAPI: "3.0.0.",
}
for _, v := range specs {
v(&s.root)
}
return &s
}
// export
func (s *Spec) ExportData() *openapi3.T {
return &s.root
}
func (s *Spec) addNewApi(path string, method string, opts []path.PathOpts) *api.API {
var newApi *api.API = &api.API{
Operation: &openapi3.Operation{
Description: "",
Responses: &openapi3.Responses{},
},
Schemas: &s.root.Components.Schemas,
}
// 属于kin-openapi自带的添加路由的方法
s.root.AddOperation(path, method, newApi.Operation)
// 处理该路径配置
newApi.DealPathItem(newApi.Operation, opts)
return newApi
}
// some rest function
func (s *Spec) Get(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "GET", opts)
}
func (s *Spec) Post(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "POST", opts)
}
func (s *Spec) Delete(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "DELETE", opts)
}
func (s *Spec) Put(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "PUT", opts)
}
func (s *Spec) Options(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "OPTIONS", opts)
}
func (s *Spec) Patch(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "PATCH", opts)
}
func (s *Spec) Trace(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "TRACE", opts)
}
func (s *Spec) Head(path string, opts ...path.PathOpts) *api.API {
return s.addNewApi(path, "HEAD", opts)
}
// registerModel
func (s *Spec) RegisterModel(m model.Model, opts ...model.ModelOpts) *Spec {
schema := new(openapi3.Schema)
model.ParseModel(m.Type, schema)
for _, v := range opts {
v(schema)
}
s.root.Components.Schemas[m.Type.Name()] = &openapi3.SchemaRef{
Value: schema,
}
return s
}
// security
func (s *Spec) Security(opts ...security.SecurityModelOpts) *Spec {
for _, v := range opts {
ss := &openapi3.SecurityScheme{}
name := v(ss)
s.root.Components.SecuritySchemes[name] = &openapi3.SecuritySchemeRef{
Value: ss,
}
}
return s
}