mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-03 12:03:37 +02:00
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:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user