extractParams function

List<(String, String?)> extractParams(
  1. String text
)

Implementation

List<(String type, String? name)> extractParams(String text) {
  text = text.trim();

  var opening = text.indexOf("(");

  final values = <(String type, String? name)>[];
  final start = opening == -1
      ? text
      : opening == 0
          ? ""
          : text.substring(0, opening - 1);

  if (start.isNotEmpty) {
    if (start.startsWith('(')) {
      values.add(start.splitParam());
    } else if (start.contains(',')) {
      values.addAll(
        start.split(',').map((s) => s.splitParam()),
      );
    } else {
      values.add(start.splitParam());
    }
  }
  if (opening != -1) {
    var closing = -1;
    var nested = 0;
    for (var i = opening; i < text.length; i++) {
      final char = text[i];
      if (nested == 0 && char == ',') {
        closing = i;
        break;
      }

      if (char == "(") nested++;
      if (char == ")") nested--;
    }

    if (closing == -1) closing = text.length;

    var tuple = text.substring(opening, closing).splitParam();
    values.add(tuple);

    if (closing < text.length) {
      values.addAll(
        extractParams(
          text.substring(closing + 1),
        ),
      );
    }
  }

  return values;
}