# Prototype for the DSL flatten function
func u_flatten(str prefix, str delim, var input): var {
  if (is_map(input) || is_array(input)) {
    return _flatten_aux(prefix, delim, input)
  } else {
    return input
  }
}

func _flatten_aux(str prefix, str delim, var input): map {
  map retval = {};

  if (is_map(input) || is_array(input)) {
    for (k, v in input) {
      next_prefix = string(k);
      if (prefix != "") {
        next_prefix = prefix . delim . string(k);
      }
      if (is_map(v) || is_array(v)) {
        retval = mapsum(retval, _flatten_aux(next_prefix, delim, v))
      } else {
        retval[next_prefix] = v
      }
    }
  }

  else {
    retval = { prefix: input }
  }
  return retval
}
