Skip to content

Commit c971207

Browse files
authored
Qualify streaming CLI payload builder name (goadesign#3178)
Multiple builder methods may be needed for multiple source types.
1 parent 2699781 commit c971207

File tree

7 files changed

+104
-57
lines changed

7 files changed

+104
-57
lines changed

codegen/cli/cli.go

-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ func PayloadBuilderSection(buildFunction *BuildFunctionData) *codegen.SectionTem
308308
// description is the flag description
309309
// required determines if the flag is required
310310
// example is an example value for the flag
311-
//
312311
func NewFlagData(svcn, en, name, typeName, description string, required bool, example, def interface{}) *FlagData {
313312
ex := jsonExample(example)
314313
fn := goifyTerms(svcn, en, name)

grpc/codegen/service_data.go

+37-34
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ func buildRequestConvertData(request, payload *expr.AttributeExpr, md []*Metadat
859859
// server side
860860
var data *InitData
861861
{
862-
data = buildInitData(request, payload, "message", "v", svcCtx, false, svr, sd)
862+
data = buildInitData(request, payload, "message", "v", svcCtx, false, svr, false, sd)
863863
data.Name = fmt.Sprintf("New%sPayload", codegen.Goify(e.Name(), true))
864864
data.Description = fmt.Sprintf("%s builds the payload of the %q endpoint of the %q service from the gRPC request type.", data.Name, e.Name(), svc.Name)
865865
for _, m := range md {
@@ -895,7 +895,7 @@ func buildRequestConvertData(request, payload *expr.AttributeExpr, md []*Metadat
895895
data *InitData
896896
)
897897
{
898-
data = buildInitData(payload, request, "payload", "message", svcCtx, true, svr, sd)
898+
data = buildInitData(payload, request, "payload", "message", svcCtx, true, svr, false, sd)
899899
data.Description = fmt.Sprintf("%s builds the gRPC request type from the payload of the %q endpoint of the %q service.", data.Name, e.Name(), svc.Name)
900900
}
901901
return &ConvertData{
@@ -929,7 +929,7 @@ func buildResponseConvertData(response, result *expr.AttributeExpr, svcCtx *code
929929

930930
var data *InitData
931931
{
932-
data = buildInitData(result, response, "result", "message", svcCtx, true, svr, sd)
932+
data = buildInitData(result, response, "result", "message", svcCtx, true, svr, false, sd)
933933
data.Description = fmt.Sprintf("%s builds the gRPC response type from the result of the %q endpoint of the %q service.", data.Name, e.Name(), svc.Name)
934934
}
935935
return &ConvertData{
@@ -945,7 +945,7 @@ func buildResponseConvertData(response, result *expr.AttributeExpr, svcCtx *code
945945

946946
var data *InitData
947947
{
948-
data = buildInitData(response, result, "message", "result", svcCtx, false, svr, sd)
948+
data = buildInitData(response, result, "message", "result", svcCtx, false, svr, false, sd)
949949
data.Name = fmt.Sprintf("New%sResult", codegen.Goify(e.Name(), true))
950950
data.Description = fmt.Sprintf("%s builds the result type of the %q endpoint of the %q service from the gRPC response type.", data.Name, e.Name(), svc.Name)
951951
for _, m := range hdrs {
@@ -1000,7 +1000,7 @@ func buildResponseConvertData(response, result *expr.AttributeExpr, svcCtx *code
10001000
// svcCtx is the attribute context for service type
10011001
// proto if true indicates the target type is a protocol buffer type
10021002
// svr if true indicates the code is generated for conversion server side
1003-
func buildInitData(source, target *expr.AttributeExpr, sourceVar, targetVar string, svcCtx *codegen.AttributeContext, proto, svr bool, sd *ServiceData) *InitData {
1003+
func buildInitData(source, target *expr.AttributeExpr, sourceVar, targetVar string, svcCtx *codegen.AttributeContext, proto, svr, usesrc bool, sd *ServiceData) *InitData {
10041004
var (
10051005
name string
10061006
isStruct bool
@@ -1024,6 +1024,9 @@ func buildInitData(source, target *expr.AttributeExpr, sourceVar, targetVar stri
10241024
name += "Proto"
10251025
}
10261026
isStruct = expr.IsObject(target.Type) || expr.IsUnion(target.Type)
1027+
if _, ok := source.Type.(expr.UserType); ok && usesrc {
1028+
name += protoBufGoTypeName(source, sd.Scope)
1029+
}
10271030
n := protoBufGoTypeName(target, sd.Scope)
10281031
if !isStruct {
10291032
// If target is array, map, or primitive the name will be suffixed with
@@ -1103,7 +1106,7 @@ func buildErrorConvertData(ge *expr.GRPCErrorExpr, e *expr.GRPCEndpointExpr, sd
11031106

11041107
var data *InitData
11051108
{
1106-
data = buildInitData(ge.ErrorExpr.AttributeExpr, ge.Response.Message, "er", "message", svcCtx, true, svr, sd)
1109+
data = buildInitData(ge.ErrorExpr.AttributeExpr, ge.Response.Message, "er", "message", svcCtx, true, svr, false, sd)
11071110
data.Name = fmt.Sprintf("New%s%sError", codegen.Goify(e.Name(), true), codegen.Goify(ge.Name, true))
11081111
data.Description = fmt.Sprintf("%s builds the gRPC error response type from the error of the %q endpoint of the %q service.", data.Name, e.Name(), svc.Name)
11091112
}
@@ -1120,7 +1123,7 @@ func buildErrorConvertData(ge *expr.GRPCErrorExpr, e *expr.GRPCEndpointExpr, sd
11201123

11211124
var data *InitData
11221125
{
1123-
data = buildInitData(ge.Response.Message, ge.ErrorExpr.AttributeExpr, "message", "er", svcCtx, false, svr, sd)
1126+
data = buildInitData(ge.Response.Message, ge.ErrorExpr.AttributeExpr, "message", "er", svcCtx, false, svr, false, sd)
11241127
data.Name = fmt.Sprintf("New%s%sError", codegen.Goify(e.Name(), true), codegen.Goify(ge.Name, true))
11251128
data.Description = fmt.Sprintf("%s builds the error type of the %q endpoint of the %q service from the gRPC error response type.", data.Name, e.Name(), svc.Name)
11261129
}
@@ -1139,19 +1142,19 @@ func buildErrorConvertData(ge *expr.GRPCErrorExpr, e *expr.GRPCEndpointExpr, sd
11391142
// svr param indicates that the stream data is built for the server.
11401143
func buildStreamData(e *expr.GRPCEndpointExpr, sd *ServiceData, svr bool) *StreamData {
11411144
var (
1142-
varn string
1143-
intName string
1144-
svcInt string
1145-
sendName string
1146-
sendDesc string
1147-
sendRef string
1148-
sendType *ConvertData
1149-
recvName string
1150-
recvDesc string
1151-
recvRef string
1152-
recvType *ConvertData
1153-
mustClose bool
1154-
typ string
1145+
varn string
1146+
intName string
1147+
svcInt string
1148+
sendName string
1149+
sendDesc string
1150+
sendRef string
1151+
sendConvert *ConvertData
1152+
recvName string
1153+
recvDesc string
1154+
recvRef string
1155+
recvConvert *ConvertData
1156+
mustClose bool
1157+
typ string
11551158

11561159
svc = sd.Service
11571160
ed = sd.Endpoint(e.Name())
@@ -1172,23 +1175,23 @@ func buildStreamData(e *expr.GRPCEndpointExpr, sd *ServiceData, svr bool) *Strea
11721175
if e.MethodExpr.Result.Type != expr.Empty {
11731176
sendName = md.ServerStream.SendName
11741177
sendRef = ed.ResultRef
1175-
sendType = &ConvertData{
1178+
sendConvert = &ConvertData{
11761179
SrcName: resCtx.Scope.Name(result, resCtx.Pkg(result), resCtx.Pointer, resCtx.UseDefault),
11771180
SrcRef: resCtx.Scope.Ref(result, resCtx.Pkg(result)),
11781181
TgtName: protoBufGoFullTypeName(e.Response.Message, sd.PkgName, sd.Scope),
11791182
TgtRef: protoBufGoFullTypeRef(e.Response.Message, sd.PkgName, sd.Scope),
1180-
Init: buildInitData(result, e.Response.Message, resVar, "v", resCtx, true, svr, sd),
1183+
Init: buildInitData(result, e.Response.Message, resVar, "v", resCtx, true, svr, true, sd),
11811184
}
11821185
}
11831186
if e.MethodExpr.StreamingPayload.Type != expr.Empty {
11841187
recvName = md.ServerStream.RecvName
11851188
recvRef = svcCtx.Scope.Ref(e.MethodExpr.StreamingPayload, svcCtx.Pkg(e.MethodExpr.StreamingPayload))
1186-
recvType = &ConvertData{
1189+
recvConvert = &ConvertData{
11871190
SrcName: protoBufGoFullTypeName(e.StreamingRequest, sd.PkgName, sd.Scope),
11881191
SrcRef: protoBufGoFullTypeRef(e.StreamingRequest, sd.PkgName, sd.Scope),
11891192
TgtName: svcCtx.Scope.Name(e.MethodExpr.StreamingPayload, svcCtx.Pkg(e.MethodExpr.StreamingPayload), svcCtx.Pointer, svcCtx.UseDefault),
11901193
TgtRef: recvRef,
1191-
Init: buildInitData(e.StreamingRequest, e.MethodExpr.StreamingPayload, "v", "spayload", svcCtx, false, svr, sd),
1194+
Init: buildInitData(e.StreamingRequest, e.MethodExpr.StreamingPayload, "v", "spayload", svcCtx, false, svr, true, sd),
11921195
Validation: addValidation(e.StreamingRequest, "stream", sd, true),
11931196
}
11941197
}
@@ -1201,33 +1204,33 @@ func buildStreamData(e *expr.GRPCEndpointExpr, sd *ServiceData, svr bool) *Strea
12011204
if e.MethodExpr.StreamingPayload.Type != expr.Empty {
12021205
sendName = md.ClientStream.SendName
12031206
sendRef = svcCtx.Scope.Ref(e.MethodExpr.StreamingPayload, svcCtx.Pkg(e.MethodExpr.StreamingPayload))
1204-
sendType = &ConvertData{
1207+
sendConvert = &ConvertData{
12051208
SrcName: svcCtx.Scope.Name(e.MethodExpr.StreamingPayload, svcCtx.Pkg(e.MethodExpr.StreamingPayload), svcCtx.Pointer, svcCtx.UseDefault),
12061209
SrcRef: sendRef,
12071210
TgtName: protoBufGoFullTypeName(e.StreamingRequest, sd.PkgName, sd.Scope),
12081211
TgtRef: protoBufGoFullTypeRef(e.StreamingRequest, sd.PkgName, sd.Scope),
1209-
Init: buildInitData(e.MethodExpr.StreamingPayload, e.StreamingRequest, "spayload", "v", svcCtx, true, svr, sd),
1212+
Init: buildInitData(e.MethodExpr.StreamingPayload, e.StreamingRequest, "spayload", "v", svcCtx, true, svr, true, sd),
12101213
}
12111214
}
12121215
if e.MethodExpr.Result.Type != expr.Empty {
12131216
recvName = md.ClientStream.RecvName
12141217
recvRef = ed.ResultRef
1215-
recvType = &ConvertData{
1218+
recvConvert = &ConvertData{
12161219
SrcName: protoBufGoFullTypeName(e.Response.Message, sd.PkgName, sd.Scope),
12171220
SrcRef: protoBufGoFullTypeRef(e.Response.Message, sd.PkgName, sd.Scope),
12181221
TgtName: resCtx.Scope.Name(result, resCtx.Pkg(result), resCtx.Pointer, resCtx.UseDefault),
12191222
TgtRef: resCtx.Scope.Ref(result, resCtx.Pkg(result)),
1220-
Init: buildInitData(e.Response.Message, result, "v", resVar, resCtx, false, svr, sd),
1223+
Init: buildInitData(e.Response.Message, result, "v", resVar, resCtx, false, svr, true, sd),
12211224
Validation: addValidation(e.Response.Message, "stream", sd, false),
12221225
}
12231226
}
12241227
mustClose = md.ClientStream.MustClose
12251228
}
1226-
if sendType != nil {
1227-
sendDesc = fmt.Sprintf("%s streams instances of %q to the %q endpoint gRPC stream.", sendName, sendType.TgtName, md.Name)
1229+
if sendConvert != nil {
1230+
sendDesc = fmt.Sprintf("%s streams instances of %q to the %q endpoint gRPC stream.", sendName, sendConvert.TgtName, md.Name)
12281231
}
1229-
if recvType != nil {
1230-
recvDesc = fmt.Sprintf("%s reads instances of %q from the %q endpoint gRPC stream.", recvName, recvType.SrcName, md.Name)
1232+
if recvConvert != nil {
1233+
recvDesc = fmt.Sprintf("%s reads instances of %q from the %q endpoint gRPC stream.", recvName, recvConvert.SrcName, md.Name)
12311234
}
12321235
}
12331236
return &StreamData{
@@ -1239,11 +1242,11 @@ func buildStreamData(e *expr.GRPCEndpointExpr, sd *ServiceData, svr bool) *Strea
12391242
SendName: sendName,
12401243
SendDesc: sendDesc,
12411244
SendRef: sendRef,
1242-
SendConvert: sendType,
1245+
SendConvert: sendConvert,
12431246
RecvName: recvName,
12441247
RecvDesc: recvDesc,
12451248
RecvRef: recvRef,
1246-
RecvConvert: recvType,
1249+
RecvConvert: recvConvert,
12471250
MustClose: mustClose,
12481251
}
12491252
}

grpc/codegen/streaming_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func TestStreaming(t *testing.T) {
6767
{"server-stream-send", &testdata.ServerStreamingMapServerSendCode},
6868
{"client-stream-recv", &testdata.ServerStreamingMapClientRecvCode},
6969
}},
70+
{"server-streaming-shared-result", testdata.ServerStreamingSharedResultRPCDSL, []*sectionExpectation{
71+
{"client-stream-recv", &testdata.ServerStreamingServerRPCSharedResultRecvCode},
72+
}},
7073

7174
// streaming payload
7275

@@ -112,7 +115,6 @@ func TestStreaming(t *testing.T) {
112115
}
113116
for _, s := range c.Sections {
114117
var (
115-
code string
116118
path string
117119
sections []*codegen.SectionTemplate
118120
)
@@ -124,8 +126,9 @@ func TestStreaming(t *testing.T) {
124126
path = clientfs[0].Path
125127
}
126128
seclen := len(sections)
127-
if seclen > 0 {
128-
code = codegen.SectionCode(t, sections[0])
129+
var code []string
130+
for _, section := range sections {
131+
code = append(code, codegen.SectionCode(t, section))
129132
}
130133
switch {
131134
case seclen == 0 && s.Code == nil:
@@ -137,8 +140,9 @@ func TestStreaming(t *testing.T) {
137140
// Test failed: section exists in file, but no code expected.
138141
t.Errorf("invalid code for %s: got %d %s sections, expected 0.\n%s", path, seclen, s.Name, code)
139142
default:
140-
if code != *s.Code {
141-
t.Errorf("invalid code for %s %s section, got:\n%s\ngot vs. expected:\n%s", path, s.Name, code, codegen.Diff(t, code, *s.Code))
143+
gen := strings.Join(code, "\n")
144+
if gen != *s.Code {
145+
t.Errorf("invalid code for %s %s section, got:\n%s\ngot vs. expected:\n%s", path, s.Name, gen, codegen.Diff(t, gen, *s.Code))
142146
}
143147
}
144148
}

grpc/codegen/testdata/client_type_code.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func ValidateMethodUnaryRPCWithErrorsBadRequestError(errmsg *service_unary_rpc_w
297297
}
298298
`
299299

300-
const BidirectionalStreamingRPCSameTypeClientTypeCode = `func NewUserType(v *service_bidirectional_streaming_rpc_same_typepb.MethodBidirectionalStreamingRPCSameTypeResponse) *servicebidirectionalstreamingrpcsametype.UserType {
300+
const BidirectionalStreamingRPCSameTypeClientTypeCode = `func NewMethodBidirectionalStreamingRPCSameTypeResponseUserType(v *service_bidirectional_streaming_rpc_same_typepb.MethodBidirectionalStreamingRPCSameTypeResponse) *servicebidirectionalstreamingrpcsametype.UserType {
301301
result := &servicebidirectionalstreamingrpcsametype.UserType{
302302
B: v.B,
303303
}
@@ -308,7 +308,7 @@ const BidirectionalStreamingRPCSameTypeClientTypeCode = `func NewUserType(v *ser
308308
return result
309309
}
310310
311-
func NewProtoMethodBidirectionalStreamingRPCSameTypeStreamingRequest(spayload *servicebidirectionalstreamingrpcsametype.UserType) *service_bidirectional_streaming_rpc_same_typepb.MethodBidirectionalStreamingRPCSameTypeStreamingRequest {
311+
func NewProtoUserTypeMethodBidirectionalStreamingRPCSameTypeStreamingRequest(spayload *servicebidirectionalstreamingrpcsametype.UserType) *service_bidirectional_streaming_rpc_same_typepb.MethodBidirectionalStreamingRPCSameTypeStreamingRequest {
312312
v := &service_bidirectional_streaming_rpc_same_typepb.MethodBidirectionalStreamingRPCSameTypeStreamingRequest{
313313
B: spayload.B,
314314
}

grpc/codegen/testdata/dsls.go

+16
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ var ServerStreamingMapDSL = func() {
179179
})
180180
}
181181

182+
var ServerStreamingSharedResultRPCDSL = func() {
183+
var UT = Type("UserType", func() {
184+
Field(1, "IntField", Int)
185+
})
186+
Service("ServiceServerStreamingRPC", func() {
187+
Method("MethodServerStreamingRPC", func() {
188+
StreamingResult(UT)
189+
GRPC(func() {})
190+
})
191+
Method("OtherMethodServerStreamingRPC", func() {
192+
StreamingResult(UT)
193+
GRPC(func() {})
194+
})
195+
})
196+
}
197+
182198
var ServerStreamingResultWithViewsDSL = func() {
183199
var RT = ResultType("application/vnd.result", func() {
184200
TypeName("ResultType")

grpc/codegen/testdata/server_type_code.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func NewProtoStreamingMethodResponse() *service_payload_with_mixed_attributespb.
296296
return message
297297
}
298298
299-
func NewAPayload(v *service_payload_with_mixed_attributespb.StreamingMethodStreamingRequest) *servicepayloadwithmixedattributes.APayload {
299+
func NewStreamingMethodStreamingRequestAPayload(v *service_payload_with_mixed_attributespb.StreamingMethodStreamingRequest) *servicepayloadwithmixedattributes.APayload {
300300
spayload := &servicepayloadwithmixedattributes.APayload{
301301
Required: int(v.Required),
302302
RequiredDefault: int(v.RequiredDefault),

0 commit comments

Comments
 (0)