moul / protoc-gen-gotemplate

:open_file_folder: generic protocol generator based on golang's text/template (grpc/protobuf)
https://manfred.life/protoc-gen-gotemplate
MIT License
430 stars 70 forks source link

Usage of custom options #150

Closed Mik- closed 2 years ago

Mik- commented 4 years ago

I use custom options in my proto file, but can't get access to it. How should it work?

The proto:

syntax = "proto3";
package myservice;

import "google/protobuf/descriptor.proto";

extend google.protobuf.MethodOptions {
    string comment = 51000;
}

message Data {
    string data = 1;
}

service MyService {
    rpc DoSomething(Data) returns (Data) { 
        option (comment) = "This method does someting."; 
    }
}

and the template:

package {{ .File.Package }}

import (
    "context"
)

type Client interface {
    {{ range .Service.Method }}
    // {{ stringMethodOptionsExtension 51000 . }}
    {{ .Name }}(ctx context.Context, req *{{ .InputType | trimstr "." }}) (*{{ .OutputType | trimstr "." }}, error)
    {{ end }}
}

If I call protoc --gotemplate_out=template_dir=.:. -I. ./my-service.proto, I get the error:

panic: template: client.go.tmpl:9:10: executing "client.go.tmpl" at <stringMethodOptionsExtension 51000 .>: error calling stringMethodOptionsExtension: runtime error: slice bounds out of range [3:2]

If I dump the data in JSON, I can see, that the method options are empty. Is this intended? Extract from JSON:

{
...
  "file": {
...
    "service": [
      {
        "name": "MyService",
        "method": [
          {
            "name": "DoSomething",
            "input_type": ".myservice.Data",
            "output_type": ".myservice.Data",
            "options": {}
          }
        ]
      }
    ],
    "extension": [
      {
        "name": "comment",
        "number": 51000,
        "label": 1,
        "type": 9,
        "extendee": ".google.protobuf.MethodOptions",
        "json_name": "comment"
      }
    ],
...
}

Thanks in advance for any hints.

Mik- commented 4 years ago

After using the current master, the error disapeard and it works as expected.

hifabienne commented 4 years ago

I have exactly the same issue and I'm already on the current master. Is there something else to do to get it work?

Mik- commented 4 years ago

Honestly, I thought a pull fixed the problem. The situation felt a little bit weird, but suddenly it worked. Actually i can't say exactly, what made it in the end. Sorry.

Mik- commented 3 years ago

The issue popped up again.

After a little research and update to the latest github.com/golang/protobuf, I got it working. I will try to provide an according pull request.

Arttii commented 1 year ago

This still does not seem to be working on latest mater and latest google.golang.org/protobuf with the example you have.

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 1.11.3 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: