in sdk-java/src/main/java/com/spotify/confidence/ConfidenceTypeMapper.java [16:86]
private static ConfidenceValue from(com.google.protobuf.Value value, FlagSchema schema)
throws ParseError {
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:
return ConfidenceValue.NULL_VALUE;
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 %s should be an int, but it is a double/long",
mismatchPrefix, value.getNumberValue()));
}
return ConfidenceValue.of(intVal);
case DOUBLE_SCHEMA:
return ConfidenceValue.of(value.getNumberValue());
default:
throw new ParseError(
String.format(
"%s %s is a Number, but it should be %s",
mismatchPrefix, value, schema.getSchemaTypeCase()));
}
case STRING_VALUE:
if (schema.getSchemaTypeCase() != SchemaTypeCase.STRING_SCHEMA) {
throw new ParseError(
String.format(
"%s %s is a String, but it should be %s",
mismatchPrefix, value, schema.getSchemaTypeCase()));
}
return ConfidenceValue.of(value.getStringValue());
case BOOL_VALUE:
if (schema.getSchemaTypeCase() != SchemaTypeCase.BOOL_SCHEMA) {
throw new ParseError(
String.format(
"%s %s is a Bool, but it should be %s",
mismatchPrefix, value, schema.getSchemaTypeCase()));
}
return ConfidenceValue.of(value.getBoolValue());
case STRUCT_VALUE:
if (schema.getSchemaTypeCase() != SchemaTypeCase.STRUCT_SCHEMA) {
throw new ParseError(
String.format(
"%s %s is a Struct, but it should be %s",
mismatchPrefix, value, schema.getSchemaTypeCase()));
}
return from(value.getStructValue(), schema.getStructSchema());
case LIST_VALUE:
if (schema.getSchemaTypeCase() != SchemaTypeCase.LIST_SCHEMA) {
throw new ParseError(
String.format(
"%s %s is a List, but it should be %s",
mismatchPrefix, value, schema.getSchemaTypeCase()));
}
final List<ConfidenceValue> mappedList =
value.getListValue().getValuesList().stream()
.map(val -> from(val, schema.getListSchema().getElementSchema()))
.collect(Collectors.toList());
return ConfidenceValue.of(mappedList);
case KIND_NOT_SET:
throw new ParseError("kind not set in com.google.protobuf.Value");
default:
throw new ParseError("Unknown value type");
}
}