/
handler.go
120 lines (106 loc) · 3.11 KB
/
handler.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
package genhandler
import (
"fmt"
"go/format"
"path"
"path/filepath"
"strings"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
options "google.golang.org/genproto/googleapis/api/annotations"
)
type Generator struct {
reg *descriptor.Registry
baseImports []descriptor.GoPackage
}
// New returns a new generator which generates handler wrappers.
func New(reg *descriptor.Registry) *Generator {
var imports []descriptor.GoPackage
for _, pkgpath := range []string{
"net/http",
"strings",
"github.com/utrack/clay/transport",
"github.com/utrack/clay/transport/httpruntime",
"google.golang.org/grpc",
"github.com/pkg/errors",
} {
pkg := descriptor.GoPackage{
Path: pkgpath,
Name: path.Base(pkgpath),
}
if err := reg.ReserveGoPackageAlias(pkg.Name, pkg.Path); err != nil {
for i := 0; ; i++ {
alias := fmt.Sprintf("%s_%d", pkg.Name, i)
if err := reg.ReserveGoPackageAlias(alias, pkg.Path); err != nil {
continue
}
pkg.Alias = alias
break
}
}
imports = append(imports, pkg)
}
return &Generator{reg: reg, baseImports: imports}
}
func (g *Generator) Generate(targets []*descriptor.File, fileToSwagger map[string][]byte) ([]*plugin.CodeGeneratorResponse_File, error) {
var files []*plugin.CodeGeneratorResponse_File
for _, file := range targets {
glog.V(1).Infof("Processing %s", file.GetName())
code, err := g.getTemplate(fileToSwagger[file.GetName()], file)
if err == errNoTargetService {
glog.V(1).Infof("%s: %v", file.GetName(), err)
continue
}
if err != nil {
return nil, err
}
formatted, err := format.Source([]byte(code))
if err != nil {
glog.Errorf("%v: %s", err, annotateString(code))
return nil, err
}
name := file.GetName()
ext := filepath.Ext(name)
base := strings.TrimSuffix(name, ext)
output := fmt.Sprintf("%s.pb.goclay.go", base)
files = append(files, &plugin.CodeGeneratorResponse_File{
Name: proto.String(output),
Content: proto.String(string(formatted)),
})
glog.V(1).Infof("Will emit %s", output)
}
return files, nil
}
func (g *Generator) getTemplate(swagBuffer []byte, f *descriptor.File) (string, error) {
if len(f.Services) == 0 {
return "", errNoTargetService
}
pkgSeen := make(map[string]bool)
var imports []descriptor.GoPackage
for _, pkg := range g.baseImports {
pkgSeen[pkg.Path] = true
imports = append(imports, pkg)
}
for _, svc := range f.Services {
for _, m := range svc.Methods {
pkg := m.RequestType.File.GoPkg
// Add request type package to imports if needed
if m.Options == nil || !proto.HasExtension(m.Options, options.E_Http) ||
pkg == f.GoPkg || pkgSeen[pkg.Path] {
continue
}
pkgSeen[pkg.Path] = true
imports = append(imports, pkg)
}
}
return applyTemplate(param{SwagBuffer: swagBuffer, File: f, Imports: imports})
}
func annotateString(str string) string {
strs := strings.Split(str, "\n")
for pos := range strs {
strs[pos] = fmt.Sprintf("%v: %v", pos, strs[pos])
}
return strings.Join(strs, "\n")
}