setByPath static method

void setByPath(
  1. dynamic map,
  2. JsonPath path,
  3. dynamic value
)

Implementation

static void setByPath(map, JsonPath path, value) {
  final lastSegment = path.last;
  final parents = path.chop;
  var container = map;
  for (var segment in parents.segments) {
    var child = container[segment];
    if (child == null && container is! Map) {
      throw Exception("Missing container in heirarchy.  Full path: $path.  Error found at segment $segment");
    } else if (child == null) {
      child = <String, dynamic>{};
      container[segment] = child;
    }
    container = child;
  }
  if (value == null && container is Map) {
    container.remove(lastSegment);
  } else {
    container[lastSegment] = value;
  }
}