forked from utrack/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.go
29 lines (27 loc) · 822 Bytes
/
swagger.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
package main
import (
"github.com/golang/glog"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/genswagger"
)
func genSwaggerDef(reg *descriptor.Registry, req *plugin.CodeGeneratorRequest) (map[string][]byte, error) {
gsw := genswagger.New(reg)
var targets []*descriptor.File
for _, target := range req.FileToGenerate {
f, err := reg.LookupFile(target)
if err != nil {
glog.Fatal(err)
}
targets = append(targets, f)
}
outSwag, err := gsw.Generate(targets)
if err != nil {
return nil, err
}
ret := make(map[string][]byte, len(outSwag))
for pos := range outSwag {
ret[req.FileToGenerate[pos]] = []byte(outSwag[pos].GetContent())
}
return ret, nil
}