Zenroom icon indicating copy to clipboard operation
Zenroom copied to clipboard

New statement copy to insert the content of a string dictionary into another string dictionary

Open andrea-dintino opened this issue 4 years ago • 3 comments

Problem I need to insert a string dictionary passed via REST APi, of which we don't know the name into another string dictionary.

Solution a statement like:

When I insert the content of 'announce' in 'listOfInstances'

Zencode:

Given that I have a 'string dictionary' named 'listOfInstances'
Given that I have a 'string dictionary' named 'announce'

When I insert the content of 'announce' in 'listOfInstances'

Then print data 

Keys:

{
	"listOfInstances": {
		"InstanceA1": {
			"ip": "127.0.0.10",
			"pubkey": "abcde"
		},
		"InstanceB2": {
			"ip": "127.0.0.11",
			"pubkey": "abcdefg"
		}
	}
}

Data:

{
	"announce": {
		"InstanceA1": {
			"ip": "0.0.0.1",
			"pubkey": "abcdesfdsdsdfd",
			"ciccio": "pesce"
		}
	}
}

The output should be:

{
  "listOfInstances": {
    "InstanceA1": {
      "ciccio": "pesce",
      "ip": "0.0.0.1",
      "pubkey": "abcdesfdsdsdfd"
    },
    "InstanceB2": {
      "ip": "127.0.0.11",
      "pubkey": "abcdefg"
    }
  }
}

andrea-dintino avatar Feb 02 '22 16:02 andrea-dintino

From the example you give I understand this is not an insert operation, but a merge. It implies updating (eventually overwriting) contents of dictionaries at a 1st level of depth.

jaromil avatar Apr 05 '22 05:04 jaromil

Oh no sorry now I see from the pubkey that this is simply an insert with overwrite. the fact we are overwriting the destination when present should be made explicit, since zenroom by default does not overwrite.

jaromil avatar Apr 05 '22 05:04 jaromil

When I merge the contents of dictionary 'left' in dictionary 'right' (errors out on 2nd level key collision)

When I overwrite dictionary 'right' with contents of dictionary 'left' (no errors on 2nd level key collision, just overwrite)

To be implemented with low priority, not clear use-case yet

jaromil avatar Oct 20 '23 19:10 jaromil