Go 取消对已更改嵌入类型的消息的编组时出现未知字段错误
给出这样一个原型:Go 取消对已更改嵌入类型的消息的编组时出现未知字段错误,go,protocol-buffers,proto3,Go,Protocol Buffers,Proto3,给出这样一个原型: message Request { uint64 account_id = 1; message Foo{ uint64 foo_id = 1; } repeated Foo foos = 2; 当我添加一个名为bar\u id message Request { uint64 account_id = 1; message Foo{ uint64 foo_id = 1; ui
message Request {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
}
repeated Foo foos = 2;
当我添加一个名为bar\u 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)
使用旧的客户端进行反序列化时出错。错误是服务A中的未知字段名“bar\u id”。请求\u Foo
我知道proto-3中的
未知字段
处理有很多更改,但这不是预期的,因为它似乎违反了前向兼容性(新服务器向旧客户端发送请求)。这与使用嵌入式类型有关吗?在不强制客户端更新的情况下更新服务器的最佳方法是什么?看起来您正在使用的是不推荐使用的。使用
UPD:使用
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:
输出:
2021/04/07 old: account_id:1 foos:{foo_id:2}
2021/04/07 new: account_id:1 foos:{foo_id:2}
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}