debugParsingExit property

String debugParsingExit

Returns a human readable String of the requested path and the actual parsed value following the path along (followablePath).

Examples: picked value "b" using pick(json, "a"(b)) picked value "null" using pick(json, "a" (null)) picked value "Instance of 'Object'" using pick(

Implementation

String get debugParsingExit {
  final access = <String>[];

  // The full path to [value] inside of the object
  // I.e. ['shoes', 0, 'name']
  final fullPath = path;

  // The path segments containing non-null values parsing could follow along
  // I.e. ['shoes'] for an empty shoes list
  final followable = followablePath;

  final foundValue = followable.length == fullPath.length;
  var foundNullPart = false;
  for (var i = 0; i < fullPath.length; i++) {
    final full = fullPath[i];
    final part = followable.length > i ? followable[i] : null;
    final nullPart = () {
      if (foundNullPart) return '';
      if (foundValue && i + 1 == fullPath.length) {
        if (value == null) {
          foundNullPart = true;
          return ' (null)';
        } else {
          return '($value)';
        }
      }
      if (part == null) {
        foundNullPart = true;
        return ' (absent)';
      }
      return '';
    }();

    if (full is int) {
      access.add('$full$nullPart');
    } else {
      access.add('"$full"$nullPart');
    }
  }

  var valueOrExit = '';
  if (foundValue) {
    valueOrExit = 'picked value "$value" using';
  } else {
    final firstMissing = fullPath.isEmpty
        ? '<root>'
        : fullPath[followable.isEmpty ? 0 : followable.length];
    final formattedMissing =
        firstMissing is int ? 'list index $firstMissing' : '"$firstMissing"';
    valueOrExit = '$formattedMissing in';
  }

  final params = access.isNotEmpty ? ', ${access.join(', ')}' : '';
  final root = access.isEmpty ? '<root>' : 'json';
  return '$valueOrExit pick($root$params)';
}