给出一个这样的原型:
message Request {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
}
repeated Foo foos = 2;当我添加一个名为bar_id的字段时
message Request {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
uint64 bar_id = 2;
}
repeated Foo foos = 2;在通过proto.UnmarshalText(msg, request)使用旧的client进行反序列化时,我得到了一个错误。错误是unknown field name "bar_id" in serviceA.Request_Foo。
我知道proto-3中的unknown field处理有很多变化,但这是意想不到的,因为它似乎违反了前向兼容性(新服务器向旧客户端发送请求)。这与使用嵌入式类型有关吗?在不强制客户端更新的情况下更新服务器的最佳方法是什么?
发布于 2021-04-08 02:44:50
看起来您使用的是已弃用的github.com/golang/protobuf。使用google.golang.org/protobuf/encoding/prototext
更新:使用DiscardUnknown
(prototext.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(b, msg)pb.proto:
syntax = "proto3";
// protoc --go_out=. *.proto
package pb;
option go_package = "./pb";
message RequestOld {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
}
repeated Foo foos = 2;
}
message RequestNew {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
uint64 bar_id = 2;
}
repeated Foo foos = 2;
}func:
import "google.golang.org/protobuf/encoding/prototext"
// marshal old message
msgOld := &pb.RequestOld{
AccountId: 1,
Foos: []*pb.RequestOld_Foo{
{
FooId: 2,
},
},
}
log.Println("old:", msgOld.String())
bOld, err := prototext.Marshal(msgOld)
if err != nil {
panic(err)
}
// unmarshal to new message
msgNew := &pb.RequestNew{}
if err := prototext.Unmarshal(bOld, msgNew); err != nil {
panic(err)
}
log.Println("new:", msgNew.String())输出:
2021/04/07 old: account_id:1 foos:{foo_id:2}
2021/04/07 new: account_id:1 foos:{foo_id:2}https://stackoverflow.com/questions/66991293
复制相似问题