-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathserver.go
More file actions
204 lines (193 loc) · 6.56 KB
/
server.go
File metadata and controls
204 lines (193 loc) · 6.56 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package codegen
import (
"fmt"
"path"
"path/filepath"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
// ServerFiles returns all the server files for every gRPC service. The files
// contain the server which implements the generated gRPC server interface and
// encoders and decoders to transform protocol buffer types and gRPC metadata
// into goa types and vice versa.
func ServerFiles(genpkg string, services *ServicesData) []*codegen.File {
svcLen := len(services.Root.API.GRPC.Services)
fw := make([]*codegen.File, 2*svcLen)
for i, svc := range services.Root.API.GRPC.Services {
fw[i] = serverFile(genpkg, svc, services)
}
for i, svc := range services.Root.API.GRPC.Services {
fw[i+svcLen] = serverEncodeDecode(genpkg, svc, services)
}
return fw
}
// serverFile returns the files defining the gRPC server.
func serverFile(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData) *codegen.File {
var (
fpath string
sections []*codegen.SectionTemplate
data = services.Get(svc.Name())
)
{
svcName := data.Service.PathName
fpath = filepath.Join(codegen.Gendir, "grpc", svcName, "server", "server.go")
imports := []*codegen.ImportSpec{
{Path: "context"},
{Path: "errors"},
codegen.GoaImport(""),
codegen.GoaNamedImport("grpc", "goagrpc"),
{Path: "google.golang.org/grpc/codes"},
{Path: path.Join(genpkg, svcName), Name: data.Service.PkgName},
{Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
for _, e := range data.Endpoints {
if e.Request.StreamEnvelope != nil {
imports = append(imports, &codegen.ImportSpec{Path: "io"})
break
}
}
sections = []*codegen.SectionTemplate{
codegen.Header(svc.Name()+" gRPC server", "server", imports),
{
Name: "server-struct",
Source: grpcTemplates.Read(grpcServerStructTypeT),
Data: data,
},
}
for _, e := range data.Endpoints {
if e.ServerStream != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-stream-struct-type",
Source: grpcTemplates.Read(grpcStreamStructTypeT),
Data: e.ServerStream,
})
}
}
sections = append(sections, &codegen.SectionTemplate{
Name: "server-init",
Source: grpcTemplates.Read(grpcServerInitT),
Data: data,
})
for _, e := range data.Endpoints {
sections = append(sections, &codegen.SectionTemplate{
Name: "grpc-handler-init",
Source: grpcTemplates.Read(grpcHandlerInitT),
Data: e,
}, &codegen.SectionTemplate{
Name: "server-grpc-interface",
Source: grpcTemplates.Read(grpcServerGRPCInterfaceT),
Data: e,
})
}
for _, e := range data.Endpoints {
if e.ServerStream != nil {
if e.ServerStream.SendConvert != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-stream-send",
Source: grpcTemplates.Read(grpcStreamSendT),
Data: e.ServerStream,
})
}
if e.Method.StreamKind == expr.ClientStreamKind || e.Method.StreamKind == expr.BidirectionalStreamKind {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-stream-recv",
Source: grpcTemplates.Read(grpcStreamRecvT),
Data: e.ServerStream,
})
}
if e.ServerStream.MustClose {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-stream-close",
Source: grpcTemplates.Read(grpcStreamCloseT),
Data: e.ServerStream,
})
}
if e.Method.ViewedResult != nil && e.Method.ViewedResult.ViewName == "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-stream-set-view",
Source: grpcTemplates.Read(grpcStreamSetViewT),
Data: e.ServerStream,
})
}
}
}
}
return &codegen.File{Path: fpath, SectionTemplates: sections}
}
// serverEncodeDecode returns the file defining the gRPC server encoding and
// decoding logic.
func serverEncodeDecode(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData) *codegen.File {
var (
fpath string
sections []*codegen.SectionTemplate
data = services.Get(svc.Name())
)
{
svcName := data.Service.PathName
fpath = filepath.Join(codegen.Gendir, "grpc", svcName, "server", "encode_decode.go")
title := fmt.Sprintf("%s gRPC server encoders and decoders", svc.Name())
imports := []*codegen.ImportSpec{
{Path: "context"},
{Path: "strings"},
{Path: "strconv"},
{Path: "unicode/utf8"},
{Path: "google.golang.org/grpc"},
{Path: "google.golang.org/grpc/metadata"},
codegen.GoaImport(""),
codegen.GoaNamedImport("grpc", "goagrpc"),
{Path: path.Join(genpkg, svcName), Name: data.Service.PkgName},
{Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
sections = []*codegen.SectionTemplate{codegen.Header(title, "server", imports)}
for _, e := range data.Endpoints {
if e.Response.ServerConvert != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "response-encoder",
Source: grpcTemplates.Read(grpcResponseEncoderT, grpcConvertTypeToStringP, "string_conversion"),
Data: e,
FuncMap: map[string]any{
"typeConversionData": typeConversionData,
"metadataEncodeDecodeData": metadataEncodeDecodeData,
},
})
}
if e.PayloadRef != "" {
fm := transTmplFuncs(svc, services)
fm["isEmpty"] = isEmpty
sections = append(sections, &codegen.SectionTemplate{
Name: "request-decoder",
Source: grpcTemplates.Read(grpcRequestDecoderT, grpcConvertStringToTypeP, "type_conversion", "slice_conversion", "slice_item_conversion"),
Data: e,
FuncMap: fm,
})
}
}
}
return &codegen.File{Path: fpath, SectionTemplates: sections}
}
func transTmplFuncs(s *expr.GRPCServiceExpr, services *ServicesData) map[string]any {
return map[string]any{
"goTypeRef": func(dt expr.DataType) string {
return services.ServicesData.Get(s.Name()).Scope.GoTypeRef(&expr.AttributeExpr{Type: dt})
},
}
}
// typeConversionData produces the template data suitable for executing the
// "type_conversion" template.
func typeConversionData(dt expr.DataType, varName, target string) map[string]any {
return map[string]any{
"Type": dt,
"VarName": varName,
"Target": target,
}
}
// metadataEncodeDecodeData produces the template data suitable for executing the
// "metadata_decoder" and "metadata_encoder" template.
func metadataEncodeDecodeData(md *MetadataData, vname string) map[string]any {
return map[string]any{
"Metadata": md,
"VarName": vname,
}
}