proto3 -> go with custom extensions resulting in imports to package ("google/protobuf") in go code

huangapple go评论80阅读模式
英文:

proto3 -> go with custom extensions resulting in imports to package ("google/protobuf") in go code

问题

我正在原型化一个基于proto3的元模型。由于go proto3扩展语法非常灵活,我可以生成特定领域的样板代码。我的领域proto文件依赖于包含扩展的meta.proto文件。

我可以将它们编译成go代码。当包含meta.proto文件时,生成的go代码会包含以下导入块:

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "google/protobuf" // 这个导入不存在!!

我的扩展文件具有以下结构(基于这个链接):

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

option go_package = "...";

import "google/protobuf/descriptor.proto"; // 这个导入引起了问题

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

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

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

我知道解决方案可能非常简单,google/protobuf的导入是为了C++生成而存在。


在我的工作空间中,应该包含的包是"github.com/golang/protobuf/protoc-gen-go/descriptor"

英文:

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

得分: 1

贫民解决方案。虽然不理想,但将其指向相关的Go导入可以解决问题:

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
英文:

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

huangapple
  • 本文由 发表于 2017年1月22日 23:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/41792591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定