ethan-gallant commented on PR #2888: URL: https://github.com/apache/thrift/pull/2888#issuecomment-1828052005
> Please create a [jira ticket](https://issues.apache.org/jira/) to describe what problem you are trying to resolve, what's the before and after generated go code from what thrift code. Hello @fishy ! :) The ticket [THRIFT-3037](https://github.com/apache/thrift/pull/2888#top) best describes this issue so I figured it best not to create a duplicate. Here is an example of how the generated code changes. I've listed them as bullet points for the major changes. ## Change description This PR makes it possible for typedefs that don't refer to a primative type to compile. This is accomplished by using a struct instead of declaring a type pointer (fixes the issue with READ methods etc. not existing). ## Code changes Here is the example schema which we will be using to describe the issue. Without the change, the code does not compile. ```thrift struct ExampleRequest { 1: required string example_field } struct ExampleResponse { 1: required string example_field } typedef ExampleRequest TypedefExampleRequest typedef ExampleResponse TypedefExampleResponse service EnvironmentService { ExampleResponse exampleMethod(1: ExampleRequest request) TypedefExampleResponse typedefExampleMethod(1: TypedefExampleRequest request) } ``` Each change will have a title and will be formatted in three sections. Code Change (this PR), previous generated code and new generated code. ### Use Structs instead of aliases This was needed as you can't refer to methods on aliased types. You instead need to implement the existing struct to inherit it's methods. #### Code change https://github.com/apache/thrift/blob/cc2d009a0f036209bc8f39d206e9d2b7cf19f4a1/compiler/cpp/src/thrift/generate/t_go_generator.cc#L769-L773 #### Old Code ```go type TypedefExampleRequest *ExampleRequest func TypedefExampleRequestPtr(v TypedefExampleRequest) *TypedefExampleRequest { return &v } type TypedefExampleResponse *ExampleResponse func TypedefExampleResponsePtr(v TypedefExampleResponse) *TypedefExampleResponse { return &v } ``` #### New Code ```go type TypedefExampleRequest struct { *ExampleRequest } func TypedefExampleRequestPtr(v TypedefExampleRequest) *TypedefExampleRequest { return &v } type TypedefExampleResponse struct { *ExampleResponse } func TypedefExampleResponsePtr(v TypedefExampleResponse) *TypedefExampleResponse { return &v } ``` ### For Forward TypeDefs use the original typedef name This was needed as it wouldn't use the correct type. For example it would use `ExampleRequest`where instead the `TypeDefExampleRequest` should have been used. #### Code change https://github.com/apache/thrift/blob/cc2d009a0f036209bc8f39d206e9d2b7cf19f4a1/compiler/cpp/src/thrift/generate/t_go_generator.cc#L2093-L2094 #### Old Code ```go func (p *EnvironmentServiceTypedefExampleMethodArgs) ReadField1(ctx context.Context, iprot thrift.TProtocol) error { p.Request = &ExampleRequest{} if err := p.Request.Read(ctx, iprot); err != nil { return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Request), err) } return nil } ``` #### New Code ```go func (p *EnvironmentServiceTypedefExampleMethodArgs) ReadField1(ctx context.Context, iprot thrift.TProtocol) error { p.Request = &TypedefExampleRequest{} if err := p.Request.Read(ctx, iprot); err != nil { return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Request), err) } return nil } ``` Pleasse let me know if there's anything else I can clarify :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
