/
oapi.go
231 lines (202 loc) · 4.86 KB
/
oapi.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
package oapi
import (
"context"
"encoding/json"
"os"
"github.com/buypal/oapi-go/internal/container"
"github.com/buypal/oapi-go/internal/logging"
"github.com/buypal/oapi-go/internal/oapi"
"github.com/buypal/oapi-go/internal/oapi/resolver"
"github.com/buypal/oapi-go/internal/oapi/scan/cmds"
"github.com/buypal/oapi-go/internal/oapi/scan/specs"
"github.com/buypal/oapi-go/internal/oapi/scan/types"
"github.com/buypal/oapi-go/internal/oapi/spec"
"github.com/buypal/oapi-go/internal/pkgutil"
"github.com/buypal/oapi-go/internal/pointer"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
)
const pkgMode = packages.NeedSyntax | packages.NeedTypes | packages.NeedImports | packages.NeedDeps | packages.NeedName | packages.NeedModule
var order = []string{
"openapi",
"info",
"components",
"paths",
}
// OAPI specification holds valid openapi specification.
type OAPI struct {
o spec.OpenAPI
c container.Container
}
// newOAPI creates new specs from container
func newOAPI(c container.Container) (x OAPI, err error) {
x = OAPI{c: c}
err = json.Unmarshal(c.Bytes(), &x.o.OpenAPI)
return x, err
}
// Options represents options of scan.
type Options struct {
dir *string
log logging.Printer
override map[string]spec.Schema
defops map[string]spec.Operation
root spec.OpenAPI
}
func (opts *Options) path() (dir string, err error) {
if opts.dir != nil {
dir = *opts.dir
} else {
dir, err = os.Getwd()
}
if err != nil {
err = errors.Wrap(err, "os")
return
}
return
}
// Option for .Scan() method
type Option func(*Options) error
// WithDir sets the directory for scanning.
func WithDir(dir string) Option {
return func(r *Options) error {
r.dir = &dir
return nil
}
}
// WithLog will set new logger.
func WithLog(l logging.Printer) Option {
return func(r *Options) error {
if l == nil {
r.log = logging.Void()
}
r.log = l
return nil
}
}
// WithOverride will add sets of overrides.
// These can be used to override pointers which already exists.
func WithOverride(or map[string]spec.Schema) Option {
return func(r *Options) error {
r.override = or
return nil
}
}
// WithDefOps is shorthant for with default operations.
func WithDefOps(defops map[string]spec.Operation) Option {
return func(r *Options) error {
r.defops = defops
return nil
}
}
// WithRootSchema is option to provide root schema.
// This is useful if you have global components.
func WithRootSchema(oapi spec.OpenAPI) Option {
return func(r *Options) error {
r.root = oapi
return nil
}
}
// Scan will scan all types in given directory (by deault cwd), and merge all
// openapi files into single document returned as OAPI.
func Scan(ctx context.Context, options ...Option) (s OAPI, err error) {
opts := &Options{
override: make(map[string]spec.Schema),
}
for _, opt := range options {
err = opt(opts)
if err != nil {
return
}
}
dir, err := opts.path()
if err != nil {
return
}
pkgs, err := packages.Load(&packages.Config{
Mode: pkgMode,
Dir: dir,
Context: ctx,
})
if err != nil {
err = errors.Wrap(err, "packages")
return
}
// Here wi will start scanning commands, comments in go code
cmdsScanner := cmds.NewScanner()
err = pkgutil.Scan(pkgs, cmdsScanner)
if err != nil {
return
}
exports, err := cmdsScanner.ExportedComponents()
if err != nil {
return
}
// Now we scan for yaml files specifications
specsScanner := specs.NewScanner()
err = pkgutil.Scan(pkgs, specsScanner)
if err != nil {
return
}
c, err := specsScanner.Merge()
if err != nil {
return
}
pp := make(pointer.Pointers)
pp = pp.Merge(cmdsScanner.Commands.Pointers())
pp = pp.Merge(specsScanner.Pointers)
// collect and handle types
tps := types.NewScanner(pp)
err = pkgutil.Scan(pkgs, tps)
if err != nil {
return
}
err = oapi.MergeWithRoot(opts.root, c)
if err != nil {
return
}
cnt, err := resolver.Resolve(c, exports, func(ptr pointer.Pointer) (e spec.Entiter, err error) {
if ovrd, ok := opts.override[ptr.String()]; ok {
return ovrd, nil
}
switch ptr.Scheme {
case "go":
e, err = tps.Resolve(ptr)
default:
err = errors.New("unknown protocol to resolve")
}
return
})
if err != nil {
return
}
err = oapi.SetPathsDefaults(cnt, opts.defops)
if err != nil {
return
}
return newOAPI(cnt)
}
// Format will format given specs into given format.
func Format(f string, oapi OAPI) (data []byte, err error) {
sorter := container.SortMapMarhsaler(order)
cont := container.NewSortMarshaller(oapi.c, sorter)
switch f {
case "yaml", "yml":
return cont.MarshalYAML()
case "json":
return cont.MarshalJSON()
case "json:pretty":
return cont.MarshalIndentJSON("", " ")
case "go":
data, err = cont.MarshalJSON()
if err != nil {
return nil, err
}
data, err = produceGoFile(tpldata{
PkgName: "main",
OpenAPIJson: string(data),
})
return
default:
return nil, errors.New("unknown format")
}
}