setRecursiveProperty function

Map setRecursiveProperty(
  1. List properties,
  2. dynamic value,
  3. Map current
)

Implementation

Map<dynamic, dynamic> setRecursiveProperty(
    List properties, dynamic value, Map<dynamic, dynamic> current) {
  dynamic property = properties.iterator.moveNext() ? properties.first : null;
  if (property == null) return current;
  if (properties.length == 1) {
    current[property] = value;
  } else {
    if (current[property] == null) {
      if (property.contains('[') && property.contains(']')) {
        String key = property.split("[").first;
        int index = int.parse(property.split("[").last.split("]").first);
        if (current[key] == null) {
          current[key] = [];
        }
        if (nth(current[key], n: index) == null) current[key].add({});
        current[key][index] = setRecursiveProperty(
            properties.sublist(1), value, current[key][index]);
      } else {
        current[property] =
            setRecursiveProperty(properties.sublist(1), value, {});
      }
    } else {
      current[property] =
          setRecursiveProperty(properties.sublist(1), value, current[property]);
    }
  }
  return current;
}