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

Manually parsing options returns an empty array #1547

Open
factoidforrest opened this issue May 24, 2023 · 1 comment
Open

Manually parsing options returns an empty array #1547

factoidforrest opened this issue May 24, 2023 · 1 comment

Comments

@factoidforrest
Copy link

factoidforrest commented May 24, 2023

I am attempting to extend my proto files using a custom option. However, when parsing out the option from the AST using getUninterpretedOptions, I get an empty array. Serializing it with .String() does however return what you would expect.

// Definition of the `build.info` custom option
extend google.protobuf.MethodOptions {
  Runtime protoflow = 1;
}

// Definition of the `BuildMetadata` message used in `build.info`
message Runtime {
  string name = 1;
}

// Trying to use the option
rpc ExternalMethod (google.protobuf.Empty) returns (MethodOneResponse) {
    option (protoflow) = {
      name: "External Method",
    };
  };

And here is the line that is returning an empty array

func TestParseProto(t *testing.T) {
  res, err := ParseProto("")
  if err != nil {
  	t.Fatal(err)
  }
  m := map[string]interface{}{}
  for _, r := range res {
  	buildUninterpretedMapForFile(r.GetFile(), m)
  	for _, s := range r.GetServices() {
  		t.Log(s.GetFullyQualifiedName())
  		for _, m := range s.GetMethods() {
  			t.Log(m.GetFullyQualifiedName())
  			mo := m.GetMethodOptions()
  			
                              // ------- THIS IS RETURNING EMPTY ARRAY  --------
  			mo.GetUninterpretedOption()
                             // But this .String method IS returning the serialized message properly
  			t.Log(mo.String())
  			
  			for _, o := range mo.GetUninterpretedOption() {
  				for _, n := range o.GetName() {
  					t.Log(n)
  				}
  			}
  		}
  	}
  }
}

Here is the full parser code in case more context is needed

https://gist.github.com/factoidforrest/95ded881160638dbb024de43dd764b93

Does anyone have any sample code of parsing and using custom options with this library?

@factoidforrest factoidforrest changed the title Manually parsing options returns a blank string Manually parsing options returns an emptty array May 24, 2023
@factoidforrest factoidforrest changed the title Manually parsing options returns an emptty array Manually parsing options returns an empty array May 24, 2023
@stapelberg
Copy link

Hey @factoidforrest

I tried saving your https://gist.github.com/factoidforrest/95ded881160638dbb024de43dd764b93 into repro_test.go, but cannot run it:

go test -v 
# b1547 [b1547.test]
./repro_test.go:14:14: undefined: ParseProto

Can you please provide steps to reproduce that are complete and tested?

Also, I noticed your code is importing github.com/jhump/protoreflect, which is a third-party parser that is not supported by the Go Protobuf maintainers. Can you reproduce your issue with only the official Go Protobuf packages? If not, you should probably file an issue with github.com/jhump/protoreflect instead.

Thanks

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

2 participants