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
2022-07-06 21:16:56 +02:00
parent 66258ee7b5
commit dc32ca61f8
2 changed files with 116 additions and 0 deletions

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
}