dongwei8729
dongwei8729
2017-01-22 15:15

proto3->使用自定义扩展名,导致在go代码中导入到程序包(“ google / protobuf”)

已采纳

I am prototyping a meta model on top of proto3. To generate domain specific boilerplate as the go proto3 extension syntax is ridiculously expressive. My domain proto files depend on meta.proto which contain the extensions.

I can compile the these to go. When including the meta.proto file the generated go ends up with the following include block:

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "google/protobuf" <--- this import does not exist !!

My extension file has the following structure(based off this):

syntax = "proto2";
package "...";

option go_package = "..."; 

import "google/protobuf/descriptor.proto"; <--- this causes the import

// message MyExtensionClass ...
// message MyExtensionField ...

extend google.protobuf.MessageOptions {
    optional MyExtensionClass class = 50000;
}

extend google.protobuf.FieldOptions {
    optional MyExtensionField field = 50001;
}

I know the solution is likely very simple, the google/protobuf include is meant for C++ generation.


In my workspace the included package should be "github.com/golang/protobuf/protoc-gen-go/descriptor"

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doupo5861 doupo5861 4年前

    Poor mans solution. Not ideal, directing it to the relevant go import works:

    sed -i '' -e 's/import google_protobuf \"google\/protobuf\"/import google_protobuf \"github.com\/golang\/protobuf\/protoc-gen-go\/descriptor\"/g' pkg/domain/proto/extensions/*.pb.go
    
    点赞 评论 复制链接分享

相关推荐