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
437 stars 70 forks source link

Unwanted files generated when have external import and set `all=true` #100

Open huangjunwen opened 6 years ago

huangjunwen commented 6 years ago

Here's an example .proto with "timestamp.proto" imported:

syntax = "proto3";

package timesvc;

import "google/protobuf/timestamp.proto";

message NowReq {}

message NowReply {
        google.protobuf.Timestamp now = 1;
}

service TimeSvc {
        rpc Now(NowReq) returns (NowReply) {}
}

And template looks like:

$ ls
{{.File.Package}}.xx.go.tmpl

After running

$  protoc --go_out=. --gotemplate_out=all=true,template_dir=.:. *.proto
$ ls
{{.File.Package}}.xx.go.tmpl  google.protobuf.xx.go  timesvc.pb.go  timesvc.proto  timesvc.xx.go

A 'google.protobuf.xx.go' is generated as well. And i don't know how to avoid generating it.

jhayotte commented 6 years ago

Instead of {{.File.Package}} use {{.File.Name}} and you will get: {{.File.Name}}.xx.go.tmpl timesvc.pb.go timesvc.proto timesvc.xx.go

huangjunwen commented 6 years ago

Hi, but i get this:

$ tree
.
├── google
│   └── protobuf
│       └── timestamp.proto.xx.go
├── timesvc.pb.go
├── timesvc.proto
├── timesvc.proto.xx.go
└── {{.File.Name}}.xx.go.tmpl

A "google/protobuf/timestamp.proto.xx.go" is generated

huangjunwen commented 6 years ago

I think the problem is here in the loop in main.go:

  // Generate the encoders
  log.Printf(">> files to genrate: %#v\n", g.Request.FileToGenerate)
  for _, file := range g.Request.GetProtoFile() {
    log.Printf(">> files and their depencies: %+q %+q\n", *file.Name, *file.Package)
    if all {
      if singlePackageMode {
      ...

Rerun the above commands will get:

protoc --go_out=. --gotemplate_out=all=true,template_dir=.:. *.proto
2018/08/02 10:21:09 >> files to genrate: []string{"timesvc.proto"}
2018/08/02 10:21:09 >> files and their depencies: "google/protobuf/timestamp.proto" "google.protobuf"
2018/08/02 10:21:09 >> files and their depencies: "timesvc.proto" "timesvc"

The loop should ignore files that's not in g.Request.FileToGenerate, as described in comment:

// The .proto files that were explicitly listed on the command-line. The // code generator should generate code only for these files. Each file's // descriptor will be included in proto_file, below. FileToGenerate []string protobuf:"bytes,1,rep,name=file_to_generate,json=fileToGenerate" json:"file_to_generate,omitempty"

huangjunwen commented 6 years ago

Hi, can anybody look into this ? Thx