Written by

Senior Development Manager at InterSystems Corporation
Question Timothy Leavitt · Feb 11, 2016

Merging two %Objects (2016.1/2016.2)

Comments

Stefan Wittmann · Feb 12, 2016

Hi Tim,

$compose works with deep structures but it replaces values of key-value pairs at the top level. It does not merge sub-arrays or sub-objects by default. Consider the following code:

SAMPLES>set o1 = {"a":1,"c":2,"deep":{"f":1,"g":1}}
 
SAMPLES>set o2 = {"b":1,"c":1,"deep":{"f":3,"j":1}}
 
SAMPLES>s o3 = o2.$compose({"returnValue":o1})
 
SAMPLES>w o3.$toJSON()
{"a":1,"c":1,"deep":{"f":3,"j":1},"b":1

You can easily spot, that the path "deep" in o3 has the value from o2 and completely misses "deep.g" from o1. We just copied the path "deep" from o2 into o1. We do provide a way  to override this behavior if you want to, but in a future release, we plan to ease this by introducing compose maps. I will cover this topic in more detail in an upcoming post.

0