-
Notifications
You must be signed in to change notification settings - Fork 21
/
transport_grpc_client.go
159 lines (144 loc) · 4.93 KB
/
transport_grpc_client.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
package template
import (
"context"
"errors"
. "github.com/dave/jennifer/jen"
"github.com/devimteam/microgen/generator/write_strategy"
"github.com/vetcher/go-astra/types"
)
var (
ErrProtobufEmpty = errors.New("protobuf package is empty")
)
type gRPCClientTemplate struct {
info *GenerationInfo
}
func NewGRPCClientTemplate(info *GenerationInfo) Template {
return &gRPCClientTemplate{
info: info,
}
}
// Render whole grpc client file.
//
// // This file was automatically generated by "microgen" utility.
// // DO NOT EDIT.
// package transportgrpc
//
// import (
// svc "github.com/devimteam/microgen/examples/svc"
// protobuf "github.com/devimteam/microgen/examples/svc/transport/converter/protobuf"
// grpc1 "github.com/go-kit/kit/transport/grpc"
// stringsvc "gitlab.devim.team/protobuf/stringsvc"
// grpc "google.golang.org/grpc"
// )
//
// func NewGRPCClient(conn *grpc.ClientConn, opts ...grpc1.ClientOption) svc.StringService {
// return &svc.Endpoints{CountEndpoint: grpc1.NewClient(
// conn,
// "devim.string.protobuf.StringService",
// "Count",
// protobuf.EncodeCountRequest,
// protobuf.DecodeCountResponse,
// stringsvc.CountResponse{},
// opts...,
// ).Endpoint()}
// }
//
func (t *gRPCClientTemplate) Render(ctx context.Context) write_strategy.Renderer {
f := NewFile("transportgrpc")
f.ImportAlias(t.info.ProtobufPackageImport, "pb")
f.ImportAlias(t.info.SourcePackageImport, serviceAlias)
f.ImportAlias(PackagePathGoKitTransportGRPC, "grpckit")
f.HeaderComment(t.info.FileHeader)
f.Func().Id("NewGRPCClient").
ParamsFunc(func(p *Group) {
p.Id("conn").Op("*").Qual(PackagePathGoogleGRPC, "ClientConn")
p.Id("addr").Id("string")
p.Id("opts").Op("...").Qual(PackagePathGoKitTransportGRPC, "ClientOption")
}).Qual(t.info.OutputPackageImport+"/transport", EndpointsSetName).
BlockFunc(func(g *Group) {
if t.info.ProtobufClientAddr != "" {
g.If(Id("addr").Op("==").Lit("")).Block(
Id("addr").Op("=").Lit(t.info.ProtobufClientAddr),
)
}
g.Return().Qual(t.info.OutputPackageImport+"/transport", EndpointsSetName).Values(DictFunc(func(d Dict) {
for _, m := range t.info.Iface.Methods {
if !t.info.AllowedMethods[m.Name] {
continue
}
client := &Statement{}
client.Qual(PackagePathGoKitTransportGRPC, "NewClient").Call(
Line().Id("conn"), Id("addr"), Lit(m.Name),
Line().Id(encodeRequestName(m)),
Line().Id(decodeResponseName(m)),
Line().Add(t.replyType(m)),
Line().Add(t.clientOpts(m)).Op("...").Line(),
).Dot("Endpoint").Call()
d[Id(endpointsStructFieldName(m.Name))] = client
}
}))
})
if Tags(ctx).Has(TracingMiddlewareTag) {
f.Line().Func().Id("TracingGRPCClientOptions").Params(
Id("tracer").Qual(PackagePathOpenTracingGo, "Tracer"),
Id("logger").Qual(PackagePathGoKitLog, "Logger"),
).Params(
Func().Params(Op("[]").Qual(PackagePathGoKitTransportGRPC, "ClientOption")).Params(Op("[]").Qual(PackagePathGoKitTransportGRPC, "ClientOption")),
).Block(
Return().Func().Params(Id("opts").Op("[]").Qual(PackagePathGoKitTransportGRPC, "ClientOption")).Params(Op("[]").Qual(PackagePathGoKitTransportGRPC, "ClientOption")).Block(
Return().Append(Id("opts"), Qual(PackagePathGoKitTransportGRPC, "ClientBefore").Call(
Line().Qual(PackagePathGoKitTracing, "ContextToGRPC").Call(Id("tracer"), Id("logger")).Op(",").Line(),
)),
),
)
}
return f
}
// Renders reply type argument
// stringsvc.CountResponse{}
func (t *gRPCClientTemplate) replyType(signature *types.Function) *Statement {
results := removeErrorIfLast(signature.Results)
if len(results) == 0 {
return Qual(PackagePathEmptyProtobuf, "Empty").Values()
}
if len(results) == 1 {
sp := specialReplyType(results[0].Type)
if sp != nil {
return sp
}
}
return Qual(t.info.ProtobufPackageImport, responseStructName(signature)).Values()
}
func specialReplyType(p types.Type) *Statement {
name := types.TypeName(p)
imp := types.TypeImport(p)
// *string -> *wrappers.StringValue
if name != nil && *name == "string" && imp == nil {
ptr, ok := p.(types.TPointer)
if ok && ptr.NumberOfPointers == 1 {
return (&Statement{}).Qual(GolangProtobufWrappers, "StringValue").Values()
}
}
// *float64 -> *wrappers.DoubleValue
if name != nil && *name == "float64" && imp == nil {
ptr, ok := p.(types.TPointer)
if ok && ptr.NumberOfPointers == 1 {
return (&Statement{}).Qual(GolangProtobufWrappers, "DoubleValue").Values()
}
}
return nil
}
func (gRPCClientTemplate) DefaultPath() string {
return filenameBuilder(PathTransport, "grpc", "client")
}
func (t *gRPCClientTemplate) Prepare(ctx context.Context) error {
return nil
}
func (t *gRPCClientTemplate) ChooseStrategy(ctx context.Context) (write_strategy.Strategy, error) {
return write_strategy.NewCreateFileStrategy(t.info.OutputFilePath, t.DefaultPath()), nil
}
func (t *gRPCClientTemplate) clientOpts(fn *types.Function) *Statement {
s := &Statement{}
s.Id("opts")
return s
}