Skip to content

Fix handling of custom gen packages #3508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions expr/grpc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,13 @@ func (e *GRPCEndpointExpr) Finalize() {
}
}
if ut, ok := e.MethodExpr.Payload.Type.(UserType); ok {
// merge Meta defined in the method payload type with the request
if ut.Attribute().Meta != nil {
// propagate the user set protobuf struct name from the user type to
// the request message.
if proto, ok := ut.Attribute().Meta.Last("struct:name:proto"); ok {
if e.Request.Meta == nil {
e.Request.Meta = ut.Attribute().Meta
} else {
e.Request.Meta.Merge(ut.Attribute().Meta)
e.Request.Meta["struct:name:proto"] = []string{proto}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions expr/grpc_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ func (r *GRPCResponseExpr) Finalize(a *GRPCEndpointExpr, svcAtt *AttributeExpr)
}
}
if ut, ok := svcAtt.Type.(UserType); ok {
// merge Meta defined in the method result type with the response
if ut.Attribute().Meta != nil {
// propagate the user set protobuf struct name from the user type to
// the request message.
if proto, ok := ut.Attribute().Meta.Last("struct:name:proto"); ok {
if r.Message.Meta == nil {
r.Message.Meta = ut.Attribute().Meta
} else {
r.Message.Meta.Merge(ut.Attribute().Meta)
r.Message.Meta["struct:name:proto"] = []string{proto}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions grpc/codegen/server_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestServerTypeFiles(t *testing.T) {
{"server-payload-with-duplicate-use", testdata.PayloadWithMultipleUseTypesDSL, testdata.PayloadWithMultipleUseTypesServerTypeCode},
{"server-payload-with-alias-type", testdata.PayloadWithAliasTypeDSL, testdata.PayloadWithAliasTypeServerTypeCode},
{"server-payload-with-mixed-attributes", testdata.PayloadWithMixedAttributesDSL, testdata.PayloadWithMixedAttributesServerTypeCode},
{"server-payload-with-custom-type-package", testdata.PayloadWithCustomTypePackageDSL, testdata.PayloadWithCustomTypePackageServerTypeCode},
{"server-result-collection", testdata.ResultWithCollectionDSL, testdata.ResultWithCollectionServerTypeCode},
{"server-with-errors", testdata.UnaryRPCWithErrorsDSL, testdata.WithErrorsServerTypeCode},
{"server-elem-validation", testdata.ElemValidationDSL, testdata.ElemValidationServerTypesFile},
Expand Down
14 changes: 14 additions & 0 deletions grpc/codegen/testdata/dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,20 @@ var PayloadWithMultipleUseTypesDSL = func() {
})
}

var PayloadWithCustomTypePackageDSL = func() {
var CustomType = Type("CustomType", func() {
Field(1, "Field", Int)
Meta("struct:pkg:path", "types")
})
Service("ServicePayloadWithCustomTypePackage", func() {
Method("MethodPayloadWithCustomTypePackage", func() {
Payload(CustomType)
Result(CustomType)
GRPC(func() {})
})
})
}

var PayloadWithAliasTypeDSL = func() {
var IntAlias = Type("IntAlias", Int)
var PayloadAliasT = Type("PayloadAliasT", func() {
Expand Down
25 changes: 25 additions & 0 deletions grpc/codegen/testdata/server_type_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,31 @@ func NewStreamingMethodStreamingRequestAPayload(v *service_payload_with_mixed_at
}
`

const PayloadWithCustomTypePackageServerTypeCode = `// NewMethodPayloadWithCustomTypePackagePayload builds the payload of the
// "MethodPayloadWithCustomTypePackage" endpoint of the
// "ServicePayloadWithCustomTypePackage" service from the gRPC request type.
func NewMethodPayloadWithCustomTypePackagePayload(message *service_payload_with_custom_type_packagepb.MethodPayloadWithCustomTypePackageRequest) *types.CustomType {
v := &types.CustomType{}
if message.Field != nil {
field := int(*message.Field)
v.Field = &field
}
return v
}

// NewProtoMethodPayloadWithCustomTypePackageResponse builds the gRPC response
// type from the result of the "MethodPayloadWithCustomTypePackage" endpoint of
// the "ServicePayloadWithCustomTypePackage" service.
func NewProtoMethodPayloadWithCustomTypePackageResponse(result *types.CustomType) *service_payload_with_custom_type_packagepb.MethodPayloadWithCustomTypePackageResponse {
message := &service_payload_with_custom_type_packagepb.MethodPayloadWithCustomTypePackageResponse{}
if result.Field != nil {
field := int32(*result.Field)
message.Field = &field
}
return message
}
`

const WithErrorsServerTypeCode = `// NewMethodUnaryRPCWithErrorsPayload builds the payload of the
// "MethodUnaryRPCWithErrors" endpoint of the "ServiceUnaryRPCWithErrors"
// service from the gRPC request type.
Expand Down