Skip to content

Commit

Permalink
Add basic inet codec
Browse files Browse the repository at this point in the history
  • Loading branch information
tatu-at-datastax committed Oct 8, 2024
1 parent 9709e6e commit 2c48705
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ public abstract class JSONCodecRegistries {

// Other codecs
JSONCodecs.BINARY,
JSONCodecs.BOOLEAN));
JSONCodecs.BOOLEAN,
JSONCodecs.INET_FROM_STRING));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, java.net.InetAddress> INET_FROM_STRING =
new JSONCodec<>(
GenericType.STRING,
DataTypes.INET,
JSONCodec.ToCQL.safeFromString(JSONCodec.ToCQL::inetAddressFromString),
(objectMapper, fromCQLType, value) ->
objectMapper.getNodeFactory().textNode(value.getHostAddress()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ private static Stream<Arguments> validCodecToJSONTestCasesUuid() {
JSONS.textNode(TEST_DATA.UUID_VALID_STR_UC.toLowerCase())));
}

private static Stream<Arguments> validCodecToJSONTestCasesOther() {
private static Stream<Arguments> validCodecToJSONTestCasesOther() throws Exception {
// Arguments: (CQL-type, from-CQL-result-set, JsonNode-to-serialize)
return Stream.of(
// Short regular base64-encoded string
Expand Down

0 comments on commit 2c48705

Please sign in to comment.