英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论