Protobuf: 从google.golang.org/protobuf导入已知类型(如timestamp)。

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

Protobuf: Import known types (like timestamp) from google.golang.org/protobuf

问题

直到现在,我一直在使用github.com/golang/protobuf。今天我收到了警告module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.。所以我按照警告的建议进行了更改,并且过渡非常顺利。但是我的项目仍然依赖于github.com/golang/protobuf,这是我想要摆脱的存储库。原因是我在.proto文件中使用了已知类型:

import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";

当我运行protocv3.12.4)生成相应的Go文件时,这将被解析为

import (
	timestamp "github.com/golang/protobuf/ptypes/timestamp"
	wrappers "github.com/golang/protobuf/ptypes/wrappers"
)

这导致仍然在使用已弃用的依赖项。
不确定是否重要,但我依赖于这些protoc插件:

$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.30.0
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0

据我所知,这些是最新的版本。

在寻找最新的timestamp类型时,我偶然发现了https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb。这个URL表明它可能属于期望的google.golang.org/protobuf包,但我不知道如何正确导入它。底层的github存储库包含一个自述文件,告诉我:

types/known/timestamppb: Package timestamppb is the generated package for google/protobuf/timestamp.proto.

但是google/protobuf/timestamp.proto是我当前在.proto文件中使用的导入,我仍然得到import timestamp "github.com/golang/protobuf/ptypes/timestamp",这是已弃用的依赖项。

我对如何解决所有这些依赖关系有点困惑,也不知道在哪里找到缺失的部分以摆脱github.com/golang/protobuf依赖项。是否有任何方法可以解决这个问题?

英文:

Until now, I was using github.com/golang/protobuf. Today I've received the warning module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead. So I did, and the transition went smoothly. But my project is still depending on github.com/golang/protobuf, the repo I want to get rid of. The reason is that I'm using known types in .proto files:

import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";

When I run protoc (v3.12.4) to generate the according Go files, this will be resolved to

import (
	timestamp "github.com/golang/protobuf/ptypes/timestamp"
	wrappers "github.com/golang/protobuf/ptypes/wrappers"
)

, causing the deprecated dependency to be still in use.
Not sure whether it matters, but I'm relying on these protoc plugins:

$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.30.0
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0

, which are the latest as far as I can tell.

Looking for the latest timestamp type, I stumbled upon https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb. The url suggests it might belong to the desired google.golang.org/protobuf package, but I don't know how to import it properly. The underlying github repo contains a readme that tells me:

types/known/timestamppb: Package timestamppb is the generated package for google/protobuf/timestamp.proto.

But google/protobuf/timestamp.proto is the import I'm currently using in my .proto file, and I'm still ending up with import timestamp "github.com/golang/protobuf/ptypes/timestamp", which is the deprecated dependency.

I'm a bit lost on how all to figure out all this dependency resolving, and I don't know where to find the missing piece to get rid of github.com/golang/protobuf dependency. Is there any?

答案1

得分: 0

这是一个关于Google在Go语言中从v1 (github.com/golang/protobuf) 到 v2 (google.golang.org/protobuf) 的改变的问题。

v2的README提供了一个很好的概述,还有一篇关于此改变的博文(我找不到了),但可以参考A New Go API for Protocol Buffers

这个改变(到v2)还包括了Google的Well-Known Types的新(生成的)Go包,可以参考Package index

这些改变的一个结果是你应该更新protoc-gen-goprotoc-gen-go-grpc,同时也更新protocv3.12.4)已经过时(2020年7月)。新的插件将生成正确引用Google提供的新GWT Go stubs的Go存根。

API切换发生在protoc-gen-go1.4.0版本中,可以参考"Overview"。

proto的import引用(例如google/protobuf/timestamp.proto没有改变。

英文:

It's confusing because Google made a v1 (github.com/golang/protobuf) to v2 (google.golang.org/protobuf) change for Go.

The README for v2 provides a good synopsis and there was a blog post about this too (that I can no longer find) but see A New Go API for Protocol Buffers.

This change (to v2) also included new (generated) Go (!) packages for Google's Well-Known Types see Package index.

A consequence of this changes is that you should update protoc-gen-go and protoc-gen-go-grpc and, while you're at it, update protoc (v3.12.4) is old (July 2020). The new plugins will generate Go stubs that correctly reference the new GWT Go stubs provided by Google.

The protoc-gen-go release where the API switch occurred is 1.4.0, see the "Overview".

The proto import references (e.g. google/protobuf/timestamp.proto) have not changed.

huangapple
  • 本文由 发表于 2023年4月1日 01:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75901287.html
匿名

发表评论

匿名网友

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

确定