/
echo.go
157 lines (139 loc) · 4.25 KB
/
echo.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
package main
import (
"fmt"
"github.com/douyu/jupiter/pkg"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
var version = pkg.JupiterVersion()
const (
httpPkg = protogen.GoImportPath("net/http")
contextPkg = protogen.GoImportPath("context")
echoPkg = protogen.GoImportPath("github.com/labstack/echo/v4")
metadataPkg = protogen.GoImportPath("google.golang.org/grpc/metadata")
deprecationComment = "// Deprecated: Do not use."
)
var methodSets = make(map[string]int)
// generateFile generates a _echo.pb.go file.
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Services) == 0 {
return nil
}
filename := file.GeneratedFilenamePrefix + "_echo.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by github.com/douyu/jupiter/cmd/protoc-gen-go-echo. DO NOT EDIT.")
g.P("// versions:")
g.P(fmt.Sprintf("// - protoc-gen-go-echo %s", version))
g.P("// - protoc ", protocVersion(gen))
g.P()
g.P("package ", file.GoPackageName)
g.P()
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the github.com/douyu/jupiter/cmd/protoc-gen-go-echo package it is being compiled against.")
g.P("var _ = ", httpPkg.Ident("StatusOK"))
g.P("var _ = new(", contextPkg.Ident("Context"), ")")
g.P("var _ = ", metadataPkg.Ident("New"))
g.P("var _ = ", echoPkg.Ident("DefaultBinder"), "{}")
g.P()
for _, service := range file.Services {
genService(gen, file, g, service)
}
return g
}
func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, s *protogen.Service) {
if s.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() {
g.P("//")
g.P(deprecationComment)
}
// HTTP Server.
sd := &service{
Name: s.GoName,
FullName: string(s.Desc.FullName()),
FilePath: file.Desc.Path(),
}
for _, method := range s.Methods {
sd.Methods = append(sd.Methods, genMethod(method)...)
}
g.P(sd.execute())
}
func genMethod(m *protogen.Method) []*method {
var methods []*method
// 存在 http rule 配置
rule, ok := proto.GetExtension(m.Desc.Options(), annotations.E_Http).(*annotations.HttpRule)
if rule != nil && ok {
for _, bind := range rule.AdditionalBindings {
methods = append(methods, buildHTTPRule(m, bind))
}
methods = append(methods, buildHTTPRule(m, rule))
return methods
}
// 不存在走默认流程
methods = append(methods, defaultMethod(m))
return methods
}
func defaultMethod(m *protogen.Method) *method {
return &method{
Name: m.GoName,
Num: methodSets[m.GoName],
Request: m.Input.GoIdent.GoName,
Reply: m.Output.GoIdent.GoName,
Path: "/" + string(m.Parent.Desc.FullName()) + "/" + m.GoName,
Method: "POST",
Comment: m.Comments.Leading.String(),
}
}
func buildHTTPRule(m *protogen.Method, rule *annotations.HttpRule) *method {
var (
path string
method string
)
switch pattern := rule.Pattern.(type) {
case *annotations.HttpRule_Get:
path = pattern.Get
method = "GET"
case *annotations.HttpRule_Put:
path = pattern.Put
method = "PUT"
case *annotations.HttpRule_Post:
path = pattern.Post
method = "POST"
case *annotations.HttpRule_Delete:
path = pattern.Delete
method = "DELETE"
case *annotations.HttpRule_Patch:
path = pattern.Patch
method = "PATCH"
case *annotations.HttpRule_Custom:
path = pattern.Custom.Path
method = pattern.Custom.Kind
}
md := buildMethodDesc(m, method, path)
return md
}
func buildMethodDesc(m *protogen.Method, httpMethod, path string) *method {
defer func() { methodSets[m.GoName]++ }()
md := &method{
Name: m.GoName,
Num: methodSets[m.GoName],
Request: m.Input.GoIdent.GoName,
Reply: m.Output.GoIdent.GoName,
Path: path,
Method: httpMethod,
Comment: m.Comments.Leading.String(),
}
md.initPathParams()
return md
}
func protocVersion(gen *protogen.Plugin) string {
v := gen.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}