how to decode service's option object
when i parse a proto file importing "google/api/annotations.proto", i cannot get the correct service's option infomation.
for example, when i create a service like this
i'd like get the "post" key, and it's value which means http path.However, i get the results like that
As you can see, all key cannot be corrected parse, which value correct. And I found the first number means the "google.api.http" id in the proto file. But what i care about is the key rather than id.
Wish reply, thank you!
@ybtuteng, what are those screenshots from? That looks like the raw decoded form a proto. FWIW, it does look correct: that value is a binary-encoded proto for the whole google.api.http message field. If you were to decode those bytes, you'd have a message where the post field is that string and also the body field is `"*".
Though the title is "incorrect service option" (as if there is a bug in protoreflect), it doesn't look like you're actually using protoreflect. If you are, can you paste some example code to reproduce what you're seeing. I think you may just be using it incorrectly.
Or are you asking how to use protoreflect to get to those options?
@ybtuteng, what are those screenshots from? That looks like the raw decoded form a proto. FWIW, it does look correct: that value is a binary-encoded proto for the whole
google.api.httpmessage field. If you were to decode those bytes, you'd have a message where thepostfield is that string and also thebodyfield is `"*".Though the title is "incorrect service option" (as if there is a bug in protoreflect), it doesn't look like you're actually using protoreflect. If you are, can you paste some example code to reproduce what you're seeing. I think you may just be using it incorrectly.
Or are you asking how to use protoreflect to get to those options?
Sorry for my poor english, i just cannot express myself properly.
However, my problem is how to get the actual key of option field. This is my code, i just print all method's option string, which looks like not totally string or binary data
for _, service := range fd.GetServices() {
for _, method := range service.GetMethods() {
fmt.Println(method.GetMethodOptions().String())
}
}
it prints these
72295728:"\"\r/CreateDeploy:\x01*"
72295728:"\"\r/DeleteDeploy:\x01*"
my proto looks like this
syntax = "proto3";
package Package.my_service;
option go_package="pb3/Package/my_service";
import "google/api/annotations.proto";
service ManageService {
rpc CreateDeploy(CreateDeployReq) returns (CreateDeployRsp) {
option (google.api.http) = {
post:"/CreateDeploy"
body:"*"
};
}
}
So, if i am not using it correctly, would you mind show me an example?
That output suggests that your program doesn't know about the extension. So it just prints it as an unknown field (which is why it only shows the tag number and the encoded value, instead of interpreting it in a more meaningful way).
I think would be fixed by just importing the package with that extension:
import _ "google.golang.org/genproto/googleapis/api/annotations"
In particular, you need the E_Http extension loaded from that package (which is done during that package's init).
After that, the String() method for method options will know about that extension and be able to print the data more meaningfully.
If you then want programmatic access to that extension, you use the extension-related methods in the proto package. For example:
extVal, err := proto.GetExtension(methodOptions, annotations.E_Http)
if err != nil {
panic(err) // TODO
}
httpRule := extVal.(*annotations.HttpRule)
@ybtuteng, does my last comment address your questions?
@ybtuteng, does my last comment address your questions?
I have my problem solved. Thanks very much! I fixed it like that
rule := &annotations.HttpRule{}
var data []byte
data, err = method.GetMethodOptions().XXX_Marshal(data, false)
if err != nil {
panic(err)
}
err = proto.Unmarshal(data, rule)
if err != nil {
panic(err)
}
It makes me get a HttpRule object which contains all field I need. But when I use another code like this to marshal before unmarshal, it always get a empty object
data, err := proto.Marshal(method.GetMethodOptions())
if err != nil {
panic(err)
}
So, how's the difference between proto.Marshal() and message.XXX_Marshal()?
@ybtuteng, did you try using proto.GetExtension? That is the correct way to do this. You do not need to marshal the options at all. Just retrieve the extension directly from the method.GetMethodOptions() message.
I don't actually know how what you're doing works. It doesn't really make sense to me that either approach would work -- you are marshaling options but then trying to unmarshal that same data as if the marshaled message were just the one custom option.
Hi, I come across a similar problem:
syntax = "proto3";
import "google/protobuf/descriptor.proto";
package helloworld;
// Hello
message HelloRequest {}
message HelloResponse{}
// Bye
message ByeRequest {}
message ByeResponse {}
// trpc desc
message TRPC {
string rpc_name = 1;
}
extend google.protobuf.MethodOptions {
TRPC trpc = 50000;
}
extend google.protobuf.MethodOptions {
string rpc_name = 50001;
}
// service: greeter
service greeter {
rpc SayHello ( HelloRequest ) returns ( HelloResponse ) { option (trpc).rpc_name = "/aaaaaa"; }
rpc SayBye ( ByeRequest ) returns ( ByeResponse ) { option (rpc_name) = "/bbbbbb"; };
}
Following is my go code:
// fd is the FileDescriptorProto
service := fd.GetServices()[0]
for idx, m := range service.GetMethods() {
fmt.Println(m.GetMethodOptions())
...
}
The output is as following:
50000:"\n\a/aaaaaa"
50001:"/bbbbbb"
I think, the message TRPC is defined in current *.proto, protoreflect may should find the extension key TRPC.rpc_name instead of tag number 50000.
Did I miss something? I tried fd.GetExtensions(), it can get the definitions, but I don't know how to associate the extension definition and methodOptions.
I try to modified the protofile, split into two:
trpc.proto:
syntax = "proto3";
import "google/protobuf/descriptor.proto";
package trpc;
// trpc desc
message TRPC {
string rpc_name = 1;
}
extend google.protobuf.MethodOptions {
TRPC trpc = 50000;
}
extend google.protobuf.MethodOptions {
string rpc_name = 50001;
}
helloworld.proto:
syntax = "proto3";
import "google/protobuf/descriptor.proto";
import "trpc.proto";
package app.helloworld;
// Hello
message HelloRequest {
string from = 1; // say hello from
string to = 2; // say hello to
string words = 3; // hello words
}
message HelloResponse {
uint32 err_code = 1; // error code
string err_msg = 2; // error msg
}
// Bye
message ByeRequest {
string from = 1; // say bye from
string to = 2; // say bye to
string words = 3; // bye words
}
message ByeResponse {
uint32 err_code = 1; // error code
string err_msg = 2; // error msg
}
// service: greeter
service greeter {
rpc SayHello ( HelloRequest ) returns ( HelloResponse ) {
option (trpc.rpc_name) = "/xxxx";
}
rpc SayBye ( ByeRequest ) returns ( ByeResponse ) {
option (trpc.rpc_name) = "/bbbbbb";
};
}
change go code:
service := fd.GetServices()[0]
for idx, m := range service.GetMethods() {
// package trpc contains file trpc.pb.go, which is generated by `protoc --go_out=`
v, err := proto.GetExtension(m.GetMethodOptions(), trpc.E_RpcName)
if err != nil {
panic(err)
}
// as trpc.E_RpcName is a string type, so v should be *string
fmt.Println(*(v.(*string)))
...
}
the output is:
/xxxx
/bbbbbb
but how to get the field name, I think field name has already been specified by argument trpc.E_RpcName. My problem has been resolved 👍 Thanks this issue.
@ybtuteng, is there anything else you need, or can I close this issue?
Closing this issue since I got no response to my last ping. Re-open and add a comment if you are still having issues.