Making a simple cloning node

Following this discussion

I'm thinking of making a simple contrib node to let users make a copy of an object e.g msg/flow/global.xxx to msg/flow/global.yyy

(I searched for clone and copy in the palette and didn't find anything suitable)

But just posting this in case someone says - no need - you can simply use zzzz node to do it

Doing this mainly as a learning exercise for me in creating a working contrib node

Hi Simon,

You could use my node-red-contrib-msg-resend with number of resends equal to 1 and force-cloning-advanced activated. But I think publishing a new extra simple node-red-contrib-msg-clone node cannot harm anybody, and is much more clear in what it does ...
Bart

That only clones the whole message and not any flow or global context properties

That is true. Go for it, I would say :wink:

1 Like

Isn't this doable with just a one-line function node?

return RED.util.cloneMessage(msg);

Does that do a deep clone? In other words if msg.payload is a reference to an object in global context will it make a copy of the object itself or just make a copy of the reference?

Yes if you just want a copy of the msg.

This is for something that lets you choose what object you want you want to copy inc flow/global contexts - and to give a choice of where to stick the copy

[edit]And for users who want to avoid writing functions[/edit]

Good Question: all I know about it is what's in the Function Node section of the User guide:
image

But I'm assuming Nick has worked through the details of what cloning the msg object should do...

I think that cloneMessage has a couple of tricks - doesn't it strip circular references and possibly some of the c***p that ExpressJS adds to an incoming request. Otherwise, I think that it is a straight deep clone.

Heh... well the idea is useful, imo -- but sounds like it should be an option on the change node to me. The main sticky bit will be that the change node does not currently let you "replace" the entire incoming msg with a changed (or cloned) object or partial object (although it has been discussed in the past as a good thing to have).

1 Like

in which clone() is indeed deep-cloning: clone - npm

1 Like

and does indeed do some sanity checking along the way.

I think that would be the ultimate aim - add a copy option to SET/CHANGE/DELETE/MOVE

I agree, there should be a Copy or Clone (to reuse current Node-Red JS terminology) Also the JSONata functions (flow-get) could have an optional parameter to Clone (T/F)

Hi @cymplecy, just read through the complete thread. It was very interesting for me since I am trying to achieve something similar. So Just want to know if this Node available today ?

Sorry - no - I've never got round to making it :frowning:

No problem at all.. Thanks for the confirmation! :slightly_smiling_face: