-
Notifications
You must be signed in to change notification settings - Fork 21
/
transport_client.go
180 lines (166 loc) · 5.98 KB
/
transport_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package template
import (
"context"
. "github.com/dave/jennifer/jen"
"github.com/devimteam/microgen/generator/strings"
mstrings "github.com/devimteam/microgen/generator/strings"
"github.com/devimteam/microgen/generator/write_strategy"
"github.com/vetcher/go-astra/types"
)
type endpointsClientTemplate struct {
info *GenerationInfo
}
func NewEndpointsClientTemplate(info *GenerationInfo) Template {
return &endpointsClientTemplate{
info: info,
}
}
// Renders endpoints file.
//
// // This file was automatically generated by "microgen" utility.
// // DO NOT EDIT.
// package stringsvc
//
// import (
// context "context"
// endpoint "github.com/go-kit/kit/endpoint"
// )
//
// type Endpoints struct {
// CountEndpoint endpoint.Endpoint
// }
//
// func (e *Endpoints) Count(ctx context.Context, text string, symbol string) (count int, positions []int) {
// req := CountRequest{
// Symbol: symbol,
// Text: text,
// }
// resp, err := e.CountEndpoint(ctx, &req)
// if err != nil {
// return
// }
// return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
// }
//
// func CountEndpoint(svc StringService) endpoint.Endpoint {
// return func(ctx context.Context, request interface{}) (interface{}, error) {
// req := request.(*CountRequest)
// count, positions := svc.Count(ctx, req.Text, req.Symbol)
// return &CountResponse{
// Count: count,
// Positions: positions,
// }, nil
// }
// }
//
func (t *endpointsClientTemplate) Render(ctx context.Context) write_strategy.Renderer {
f := NewFile("transport")
f.HeaderComment(t.info.FileHeader)
if Tags(ctx).HasAny(TracingMiddlewareTag) {
f.Comment("TraceClientEndpoints is used for tracing endpoints on client side.")
f.Add(t.clientTracingMiddleware()).Line()
}
for _, signature := range t.info.Iface.Methods {
f.Add(t.serviceEndpointMethod(ctx, signature)).Line().Line()
}
return f
}
func (endpointsClientTemplate) DefaultPath() string {
return filenameBuilder(PathTransport, "client")
}
func (t *endpointsClientTemplate) Prepare(ctx context.Context) error {
return nil
}
func (t *endpointsClientTemplate) ChooseStrategy(ctx context.Context) (write_strategy.Strategy, error) {
return write_strategy.NewCreateFileStrategy(t.info.OutputFilePath, t.DefaultPath()), nil
}
// Render full endpoints method.
//
// func (e *Endpoints) Count(ctx context.Context, text string, symbol string) (count int, positions []int) {
// req := CountRequest{
// Symbol: symbol,
// Text: text,
// }
// resp, err := e.CountEndpoint(ctx, &req)
// if err != nil {
// return
// }
// return resp.(*CountResponse).Count, resp.(*CountResponse).Positions
// }
//
func (t *endpointsClientTemplate) serviceEndpointMethod(ctx context.Context, signature *types.Function) *Statement {
normal := normalizeFunction(signature)
return methodDefinitionFull(ctx, EndpointsSetName, &normal.Function).
BlockFunc(t.serviceEndpointMethodBody(ctx, signature, &normal.Function))
}
// Render interface method body.
//
// endpointCountRequest := CountRequest{
// Symbol: symbol,
// Text: text,
// }
// endpointCountResponse, err := E.CountEndpoint(ctx, &endpointCountRequest)
// if err != nil {
// return
// }
// return endpointCountResponse.(*CountResponse).Count, endpointCountResponse.(*CountResponse).Positions, err
//
func (t *endpointsClientTemplate) serviceEndpointMethodBody(ctx context.Context, fn *types.Function, normal *types.Function) func(g *Group) {
reqName := "request"
respName := "response"
return func(g *Group) {
if !t.info.AllowedMethods[fn.Name] {
g.Return()
return
}
g.Id(reqName).Op(":=").Id(requestStructName(fn)).Values(dictByNormalVariables(RemoveContextIfFirst(fn.Args), RemoveContextIfFirst(normal.Args)))
g.Add(endpointResponse(respName, normal)).Id(strings.LastWordFromName(EndpointsSetName)).Dot(endpointsStructFieldName(fn.Name)).Call(Id(firstArgName(normal)), Op("&").Id(reqName))
g.If(Id(nameOfLastResultError(normal)).Op("!=").Nil().BlockFunc(func(ifg *Group) {
if Tags(ctx).HasAny(GrpcTag, GrpcClientTag, GrpcServerTag) {
ifg.Add(checkGRPCError(normal))
}
ifg.Return()
}))
g.ReturnFunc(func(group *Group) {
for _, field := range removeErrorIfLast(fn.Results) {
group.Id(respName).Assert(Op("*").Id(responseStructName(fn))).Op(".").Add(structFieldName(&field))
}
group.Id(nameOfLastResultError(normal))
})
}
}
func checkGRPCError(fn *types.Function) *Statement {
s := &Statement{}
s.If(List(Id("e"), Id("ok")).Op(":=").Qual(PackagePathGoogleGRPCStatus, "FromError").Call(Id(nameOfLastResultError(fn))),
Id("ok").Op("||").
Id("e").Dot("Code").Call().Op("==").Qual(PackagePathGoogleGRPCCodes, "Internal").Op("||").
Id("e").Dot("Code").Call().Op("==").Qual(PackagePathGoogleGRPCCodes, "Unknown"),
).Block(
Id(nameOfLastResultError(fn)).Op("=").Qual("errors", "New").Call(Id("e").Dot("Message").Call()),
)
return s
}
// Helper func for `serviceEndpointMethodBody`
func endpointResponse(respName string, fn *types.Function) *Statement {
if len(removeErrorIfLast(fn.Results)) > 0 {
return List(Id(respName), Id(nameOfLastResultError(fn))).Op(":=")
}
return List(Id("_"), Id(nameOfLastResultError(fn))).Op("=")
}
// For custom ctx in service interface (e.g. context or ctxxx).
func firstArgName(signature *types.Function) string {
return mstrings.ToLowerFirst(signature.Args[0].Name)
}
func (t *endpointsClientTemplate) clientTracingMiddleware() *Statement {
s := &Statement{}
s.Func().Id("TraceClientEndpoints").Call(Id("endpoints").Id(EndpointsSetName), Id("tracer").Qual(PackagePathOpenTracingGo, "Tracer")).Id(EndpointsSetName).BlockFunc(func(g *Group) {
g.Return(Id(EndpointsSetName).Values(DictFunc(func(d Dict) {
for _, signature := range t.info.Iface.Methods {
if t.info.AllowedMethods[signature.Name] {
d[Id(endpointsStructFieldName(signature.Name))] = Qual(PackagePathGoKitTracing, "TraceClient").Call(Id("tracer"), Lit(signature.Name)).Call(Id("endpoints").Dot(endpointsStructFieldName(signature.Name)))
}
}
})))
})
return s
}