Skip to content
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

v1.3.1 produces invalid code when aliasing names in a oneof block #666

Open
alexh-ml opened this issue Feb 11, 2020 · 2 comments
Open

v1.3.1 produces invalid code when aliasing names in a oneof block #666

alexh-ml opened this issue Feb 11, 2020 · 2 comments

Comments

@alexh-ml
Copy link

alexh-ml commented Feb 11, 2020

This occurs only in a oneof block when the name of a field is prefixed with the word get_, which golang protobuf files reserve for the nil safe getter methods, which are produced. Eg. the field request will have a method GetRequest, so the name GetRequest cannot be used by another field.

This file is possible to compile with protoc-gen-go

I've isolated the problem down to this simplified case. I'm only seeing it happen when the field is within a oneof block.

syntax = "proto3";

package example;

option go_package="example";

message AnyRequest {
  oneof request {
    PutRequest put_request = 1;
    Request get_request = 2;
  }
}

message PutRequest {
}

message Request {
}

The error message that is produced when compiling example.pb.go

# example
./example.pb.go:99:28: impossible type assertion:
	*AnyRequest_GetRequest_ does not implement isAnyRequest_Request (missing MarshalTo method)
./example.pb.go:270:10: undefined: AnyRequest_GetRequest
./example.pb.go:275:10: undefined: AnyRequest_GetRequest
./example.pb.go:383:10: undefined: AnyRequest_GetRequest
./example.pb.go:522:17: undefined: AnyRequest_GetRequest

The issue is with the type signatures for the following methods. MarshalTo, MarshalToSizedBuffer, and Size do not use the alias that the library has chosen for the field. So these methods get defined on a nonexistent type.

func (m *AnyRequest_GetRequest) MarshalTo(dAtA []byte) (int, error)
func (m *AnyRequest_GetRequest) MarshalToSizedBuffer(dAtA []byte) (int, error)
func (m *AnyRequest_GetRequest) Size() (n int)
@alexh-ml
Copy link
Author

If the oneof field has only one field, different error messages are produced. In this case the field is renamed to Request_, but the methods continue to reference the field as Request

syntax = "proto3";

package example2;

option go_package="example2";

message AnyRequest {
  oneof request {
    Body get_request = 2;
  }
}

message Body {
}

Errors

./example2.pb.go:183:6: m.Request undefined (type *AnyRequest has no field or method Request)
./example2.pb.go:185:13: m.Request undefined (type *AnyRequest has no field or method Request)
./example2.pb.go:187:18: m.Request undefined (type *AnyRequest has no field or method Request)
./example2.pb.go:260:6: m.Request undefined (type *AnyRequest has no field or method Request)
./example2.pb.go:261:9: m.Request undefined (type *AnyRequest has no field or method Request)
./example2.pb.go:361:5: m.Request undefined (type *AnyRequest has no field or method Request)

@alexh-ml
Copy link
Author

#639 It seems like this PR addresses the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant