public TupleValue parse()

in core/src/main/java/com/datastax/oss/driver/internal/core/type/codec/TupleCodec.java [160:246]


  public TupleValue parse(@Nullable String value) {
    if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
      return null;
    }

    TupleValue tuple = cqlType.newValue();
    int length = value.length();

    int position = ParseUtils.skipSpaces(value, 0);
    if (value.charAt(position) != '(') {
      throw new IllegalArgumentException(
          String.format(
              "Cannot parse tuple value from \"%s\", at character %d expecting '(' but got '%c'",
              value, position, value.charAt(position)));
    }

    position++;
    position = ParseUtils.skipSpaces(value, position);

    CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();

    int field = 0;
    while (position < length) {
      if (value.charAt(position) == ')') {
        position = ParseUtils.skipSpaces(value, position + 1);
        if (position == length) {
          return tuple;
        }
        throw new IllegalArgumentException(
            String.format(
                "Cannot parse tuple value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"",
                value, position, value.substring(position)));
      }
      int n;
      try {
        n = ParseUtils.skipCQLValue(value, position);
      } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
            String.format(
                "Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d)",
                value, field, position),
            e);
      }

      String fieldValue = value.substring(position, n);
      DataType elementType = cqlType.getComponentTypes().get(field);
      TypeCodec<Object> codec = registry.codecFor(elementType);
      Object parsed;
      try {
        parsed = codec.parse(fieldValue);
      } catch (Exception e) {
        throw new IllegalArgumentException(
            String.format(
                "Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d): %s",
                value, field, position, e.getMessage()),
            e);
      }
      tuple = tuple.set(field, parsed, codec);

      position = n;

      position = ParseUtils.skipSpaces(value, position);
      if (position == length) {
        throw new IllegalArgumentException(
            String.format(
                "Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' or ')', but got EOF",
                value, field, position));
      }
      if (value.charAt(position) == ')') {
        continue;
      }
      if (value.charAt(position) != ',') {
        throw new IllegalArgumentException(
            String.format(
                "Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' but got '%c'",
                value, field, position, value.charAt(position)));
      }
      ++position; // skip ','

      position = ParseUtils.skipSpaces(value, position);
      field += 1;
    }
    throw new IllegalArgumentException(
        String.format(
            "Cannot parse tuple value from \"%s\", at field %d (character %d) expecting CQL value or ')', got EOF",
            value, field, position));
  }