def deep_merge()

in apps/replikate/src/main.py [0:0]


def deep_merge(accum, sample):
  """
  Recursively merges a dictionary on the right to the accumulator on the left
  Returns merge difference (applied values to the accumulator)
  """
  result = {}
  for key, value in sample.items():
    if value == None: continue
    if isinstance(value, dict):
      if key not in accum: accum[key] = {}
      diff = deep_merge(accum[key], value)
      if diff: result[key] = diff
    if isinstance(value, list):
      if key not in accum: accum[key] = []
      diff = merge_list(accum[key], value)
      if diff: result[key] = diff
    else:
      if key not in accum:
        accum[key] = value
        result[key] = accum[key]
  return result