diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodec.java b/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodec.java index 1bed856fd..16e9e427e 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodec.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodec.java @@ -12,6 +12,8 @@ import io.stargate.sgv2.jsonapi.exception.catchable.ToJSONCodecException; import io.stargate.sgv2.jsonapi.service.shredding.tables.RowShredder; import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.time.DateTimeException; import java.util.function.Function; @@ -363,6 +365,14 @@ static ByteBuffer byteBufferFromEJSON(DataType targetCQLType, EJSONWrapper wrapp } return ByteBuffer.wrap(binaryPayload); } + + static InetAddress inetAddressFromString(String value) throws IllegalArgumentException { + try { + return InetAddress.getByName(value); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("Invalid IP address value '%s'".formatted(value)); + } + } } /** diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistries.java b/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistries.java index a2b0330a0..0154a9008 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistries.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistries.java @@ -54,5 +54,6 @@ public abstract class JSONCodecRegistries { // Other codecs JSONCodecs.BINARY, - JSONCodecs.BOOLEAN)); + JSONCodecs.BOOLEAN, + JSONCodecs.INET_FROM_STRING)); } diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecs.java b/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecs.java index 9854d1531..fe4bf6352 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecs.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecs.java @@ -294,4 +294,13 @@ public abstract class JSONCodecs { DataTypes.TIMEUUID, JSONCodec.ToCQL.safeFromString(UUID::fromString), JSONCodec.ToJSON.toJSONUsingToString()); + + // Misc other Codecs + public static final JSONCodec INET_FROM_STRING = + new JSONCodec<>( + GenericType.STRING, + DataTypes.INET, + JSONCodec.ToCQL.safeFromString(JSONCodec.ToCQL::inetAddressFromString), + (objectMapper, fromCQLType, value) -> + objectMapper.getNodeFactory().textNode(value.getHostAddress())); } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistryTest.java b/src/test/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistryTest.java index cc4bb0b14..7dc26e170 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistryTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistryTest.java @@ -521,7 +521,7 @@ private static Stream validCodecToJSONTestCasesUuid() { JSONS.textNode(TEST_DATA.UUID_VALID_STR_UC.toLowerCase()))); } - private static Stream validCodecToJSONTestCasesOther() { + private static Stream validCodecToJSONTestCasesOther() throws Exception { // Arguments: (CQL-type, from-CQL-result-set, JsonNode-to-serialize) return Stream.of( // Short regular base64-encoded string