Skip to content

Commit

Permalink
Replace string switch with enum switch (elastic#110942)
Browse files Browse the repository at this point in the history
Found another place where we were switching on datatype names instead of
enums.
  • Loading branch information
not-napoleon committed Jul 16, 2024
1 parent 14bce35 commit 5f5893c
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,28 @@ public abstract class AbstractFunctionTestCase extends ESTestCase {
* Generate a random value of the appropriate type to fit into blocks of {@code e}.
*/
public static Literal randomLiteral(DataType type) {
return new Literal(Source.EMPTY, switch (type.typeName()) {
case "boolean" -> randomBoolean();
case "byte" -> randomByte();
case "short" -> randomShort();
case "integer", "counter_integer" -> randomInt();
case "unsigned_long", "long", "counter_long" -> randomLong();
case "date_period" -> Period.of(randomIntBetween(-1000, 1000), randomIntBetween(-13, 13), randomIntBetween(-32, 32));
case "datetime" -> randomMillisUpToYear9999();
case "double", "scaled_float", "counter_double" -> randomDouble();
case "float" -> randomFloat();
case "half_float" -> HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat()));
case "keyword" -> new BytesRef(randomAlphaOfLength(5));
case "ip" -> new BytesRef(InetAddressPoint.encode(randomIp(randomBoolean())));
case "time_duration" -> Duration.ofMillis(randomLongBetween(-604800000L, 604800000L)); // plus/minus 7 days
case "text" -> new BytesRef(randomAlphaOfLength(50));
case "version" -> randomVersion().toBytesRef();
case "geo_point" -> GEO.asWkb(GeometryTestUtils.randomPoint());
case "cartesian_point" -> CARTESIAN.asWkb(ShapeTestUtils.randomPoint());
case "geo_shape" -> GEO.asWkb(GeometryTestUtils.randomGeometry(randomBoolean()));
case "cartesian_shape" -> CARTESIAN.asWkb(ShapeTestUtils.randomGeometry(randomBoolean()));
case "null" -> null;
case "_source" -> {
return new Literal(Source.EMPTY, switch (type) {
case BOOLEAN -> randomBoolean();
case BYTE -> randomByte();
case SHORT -> randomShort();
case INTEGER, COUNTER_INTEGER -> randomInt();
case UNSIGNED_LONG, LONG, COUNTER_LONG -> randomLong();
case DATE_PERIOD -> Period.of(randomIntBetween(-1000, 1000), randomIntBetween(-13, 13), randomIntBetween(-32, 32));
case DATETIME -> randomMillisUpToYear9999();
case DOUBLE, SCALED_FLOAT, COUNTER_DOUBLE -> randomDouble();
case FLOAT -> randomFloat();
case HALF_FLOAT -> HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat()));
case KEYWORD -> new BytesRef(randomAlphaOfLength(5));
case IP -> new BytesRef(InetAddressPoint.encode(randomIp(randomBoolean())));
case TIME_DURATION -> Duration.ofMillis(randomLongBetween(-604800000L, 604800000L)); // plus/minus 7 days
case TEXT -> new BytesRef(randomAlphaOfLength(50));
case VERSION -> randomVersion().toBytesRef();
case GEO_POINT -> GEO.asWkb(GeometryTestUtils.randomPoint());
case CARTESIAN_POINT -> CARTESIAN.asWkb(ShapeTestUtils.randomPoint());
case GEO_SHAPE -> GEO.asWkb(GeometryTestUtils.randomGeometry(randomBoolean()));
case CARTESIAN_SHAPE -> CARTESIAN.asWkb(ShapeTestUtils.randomGeometry(randomBoolean()));
case NULL -> null;
case SOURCE -> {
try {
yield BytesReference.bytes(
JsonXContent.contentBuilder().startObject().field(randomAlphaOfLength(3), randomAlphaOfLength(10)).endObject()
Expand All @@ -124,7 +124,9 @@ public static Literal randomLiteral(DataType type) {
throw new UncheckedIOException(e);
}
}
default -> throw new IllegalArgumentException("can't make random values for [" + type.typeName() + "]");
case UNSUPPORTED, OBJECT, NESTED, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG -> throw new IllegalArgumentException(
"can't make random values for [" + type.typeName() + "]"
);
}, type);
}

Expand Down

0 comments on commit 5f5893c

Please sign in to comment.