private static Value from()

in openfeature-provider/src/main/java/com/spotify/confidence/OpenFeatureTypeMapper.java [21:85]


  private static Value from(com.google.protobuf.Value value, FlagSchema schema) {
    if (schema.getSchemaTypeCase() == SchemaTypeCase.SCHEMATYPE_NOT_SET) {
      throw new ParseError("schemaType not set in FlagSchema");
    }

    final String mismatchPrefix = "Mismatch between schema and value:";
    switch (value.getKindCase()) {
      case NULL_VALUE:
        try {
          return new Value((Object) null);
        } catch (InstantiationException e) {
          throw new RuntimeException(e);
        }
      case NUMBER_VALUE:
        switch (schema.getSchemaTypeCase()) {
          case INT_SCHEMA:
            final int intVal = (int) value.getNumberValue();
            if (intVal != value.getNumberValue()) {
              throw new ParseError(
                  String.format(
                      "%s value should be an int, but it is a double/long", mismatchPrefix));
            }
            return new Value(intVal);
          case DOUBLE_SCHEMA:
            return new Value(value.getNumberValue());
          default:
            throw new ParseError("Number field must have schema type int or double");
        }
      case STRING_VALUE:
        if (schema.getSchemaTypeCase() != SchemaTypeCase.STRING_SCHEMA) {
          throw new ParseError(
              String.format(
                  "%s value is a String, but it should be something else", mismatchPrefix));
        }
        return new Value(value.getStringValue());
      case BOOL_VALUE:
        if (schema.getSchemaTypeCase() != SchemaTypeCase.BOOL_SCHEMA) {
          throw new ParseError(
              String.format("%s value is a bool, but should be something else", mismatchPrefix));
        }
        return new Value(value.getBoolValue());
      case STRUCT_VALUE:
        if (schema.getSchemaTypeCase() != SchemaTypeCase.STRUCT_SCHEMA) {
          throw new ParseError(
              String.format("%s value is a struct, but should be something else", mismatchPrefix));
        }

        return from(value.getStructValue(), schema.getStructSchema());
      case LIST_VALUE:
        if (schema.getSchemaTypeCase() != SchemaTypeCase.LIST_SCHEMA) {
          throw new ParseError(
              String.format("%s value is a list, but should be something else", mismatchPrefix));
        }

        final List<Value> mappedList =
            value.getListValue().getValuesList().stream()
                .map(val -> from(val, schema.getListSchema().getElementSchema()))
                .collect(Collectors.toList());
        return new Value(mappedList);
      case KIND_NOT_SET:
        throw new ParseError("kind not set in com.google.protobuf.Value");
      default:
        throw new ParseError("Unknown value type");
    }
  }