private static Signature parseArgumentTypes()

in tools/automatic_query_fixer/src/main/java/com/google/cloud/bigquery/utils/queryfixer/errors/NoMatchingSignatureError.java [109:147]


  private static Signature parseArgumentTypes(String signature) {
    if (signature.isEmpty()) {
      return Signature.empty();
    }

    List<String> contents = PatternMatcher.extract(signature, SIGNATURE_REGEX);
    if (contents == null) {
      return null;
    }

    List<ArgumentType> requiredArgs = new ArrayList<>();
    // There is no need to check the null of the first grouping because it is (.*?). The worst case
    // would match an empty string, but still not null.
    String requiredPart = contents.get(0);
    for (String dataType : requiredPart.split(", ")) {
      requiredArgs.add(ArgumentType.of(dataType, /*repeated=*/ false));
    }

    List<ArgumentType> optionalArgs = new ArrayList<>();
    // 2nd group represents the regex "(\\[(.*?)\\])?", indicating the existence of optional
    // arguments.
    if (contents.get(2) != null) {
      // 3rd group is the content inside the bracket, which represents optinoal arguments.
      String optionalPart = contents.get(3);
      for (String dataType : optionalPart.split(", ")) {
        // If current is "...", the set previous argument to be optional.
        if (dataType.equals("...")) {
          if (!optionalArgs.isEmpty()) {
            optionalArgs.get(optionalArgs.size() - 1).repeated = true;
          }
          // Since "..." is the end of a signature, we can break directly.
          break;
        }
        optionalArgs.add(ArgumentType.of(dataType, /*repeated=*/ false));
      }
    }

    return Signature.of(requiredArgs, optionalArgs);
  }