rpcperms: add replaceProtoMsg

Because of the way the gRPC Receive() method is designed, we need a way
to replace a proto message with the content of another one without
replacing the original instance itself (e.g. overwrite all values in the
existing struct instance).
This commit is contained in:
Oliver Gugger committed 2022-07-14 09:36:36 +02:00
1 parent 66258ee7b5
commit dc32ca61f8
2 files changed
+116

No files matched your search

+26
View File
@@ -529,3 +529,29 @@ func parseProto(typeName string, serialized []byte) (proto.Message, error) {
return msg.Interface(), nil
}
// replaceProtoMsg replaces the given target message with the content of the
// replacement message.
func replaceProtoMsg(target interface{}, replacement interface{}) error {
targetMsg, ok := target.(proto.Message)
if !ok {
return fmt.Errorf("target is not a proto message: %v", target)
}
replacementMsg, ok := replacement.(proto.Message)
if !ok {
return fmt.Errorf("replacement is not a proto message: %v",
replacement)
}
if targetMsg.ProtoReflect().Type() !=
replacementMsg.ProtoReflect().Type() {
return fmt.Errorf("replacement message is of wrong type")
}
proto.Reset(targetMsg)
proto.Merge(targetMsg, replacementMsg)
return nil
}