mergeObjectList function

WithWarning<Map> mergeObjectList(
  1. List list,
  2. String path, [
  3. int idx = -1
])

Implementation

WithWarning<Map> mergeObjectList(List<dynamic> list, String path,
    [int idx = -1]) {
  var warnings = <Warning>{};
  var obj = {};
  for (var i = 0; i < list.length; i++) {
    final toMerge = list[i];
    if (toMerge is Map) {
      toMerge.forEach((k, v) {
        final t = getTypeName(obj[k]);
        if (obj[k] == null) {
          obj[k] = v;
        } else {
          final otherType = getTypeName(v);
          if (t != otherType) {
            if (t == 'int' && otherType == 'double') {
              // if double was found instead of int, assign the double
              obj[k] = v;
            } else if (t != 'double' && otherType != 'int') {
              // if types are not equal, then
              var realIndex = i;
              if (idx != -1) {
                realIndex = idx - i;
              }
              final ambiguosTypePath = '$path[$realIndex]/$k';
              warnings.add(newAmbiguousType(ambiguosTypePath));
            }
          } else if (t == 'List') {
            var l = List.from(obj[k] as Iterable);
            final beginIndex = l.length;
            l.addAll(v as Iterable);
            // bug is here
            final mergeableType = mergeableListType(l);
            if (ListType.Object == mergeableType.listType) {
              var mergedList = mergeObjectList(l, '$path[$i]/$k', beginIndex);
              warnings.addAll(mergedList.warnings);
              obj[k] = List.filled(1, mergedList.result);
            } else {
              if (l.isNotEmpty) {
                obj[k] = List.filled(1, l[0]);
              }
              if (mergeableType.isAmbigous) {
                warnings.add(newAmbiguousType('$path[$i]/$k'));
              }
            }
          } else if (t == 'Class') {
            var properIndex = i;
            if (idx != -1) {
              properIndex = i - idx;
            }
            var mergedObj = mergeObj(
              obj[k] as Map,
              v as Map,
              '$path[$properIndex]/$k',
            );
            warnings.addAll(mergedObj.warnings);
            obj[k] = mergedObj.result;
          }
        }
      });
    }
  }
  return WithWarning(obj, warnings.toList());
}