Skip to content

Commit

Permalink
Add support for use of maps and arrays in pojo packets
Browse files Browse the repository at this point in the history
  • Loading branch information
rchomczyk committed Feb 28, 2024
1 parent 7e10763 commit feca0d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public PacketPacker packArrayHeader(int value) throws IOException {
@Override
public <V> PacketPacker packArray(final V[] value) throws IOException {
return packOrNil(value, (packer, val) -> {
packer.packString(val.getClass().getComponentType().getName());
packer.packArrayHeader(val.length);
for (final V currentValue : value) {
this.packAuto(currentValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.pivovarit.function.ThrowingBiConsumer;
import com.pivovarit.function.ThrowingFunction;
import java.io.IOException;
import java.lang.reflect.Array;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
Expand Down Expand Up @@ -58,6 +59,8 @@ final class MessagePackPacketPackerUtils {
PACKET_PACKER_BY_BOXED_TYPE.put(Instant.class, (packer, value) -> packer.packInstant((Instant) value));
PACKET_PACKER_BY_BOXED_TYPE.put(Duration.class, (packer, value) -> packer.packDuration((Duration) value));
PACKET_PACKER_BY_BOXED_TYPE.put(Enum.class, (packer, value) -> packer.packEnum((Enum<?>) value));
PACKET_PACKER_BY_BOXED_TYPE.put(Array.class, (packer, value) -> packer.packArray((Object[]) value));
PACKET_PACKER_BY_BOXED_TYPE.put(Map.class, (packer, value) -> packer.packMap((Map<?, ?>) value));
PACKET_UNPACKER_BY_BOXED_TYPE = new HashMap<>();
PACKET_UNPACKER_BY_BOXED_TYPE.put(String.class, PacketUnpacker::unpackString);
PACKET_UNPACKER_BY_BOXED_TYPE.put(Boolean.class, PacketUnpacker::unpackBoolean);
Expand All @@ -71,6 +74,8 @@ final class MessagePackPacketPackerUtils {
PACKET_UNPACKER_BY_BOXED_TYPE.put(Instant.class, PacketUnpacker::unpackInstant);
PACKET_UNPACKER_BY_BOXED_TYPE.put(Duration.class, PacketUnpacker::unpackDuration);
PACKET_UNPACKER_BY_BOXED_TYPE.put(Enum.class, PacketUnpacker::unpackEnum);
PACKET_UNPACKER_BY_BOXED_TYPE.put(Array.class, PacketUnpacker::unpackArray);
PACKET_UNPACKER_BY_BOXED_TYPE.put(Map.class, PacketUnpacker::unpackMap);
}

private MessagePackPacketPackerUtils() {
Expand All @@ -97,6 +102,14 @@ static Class<?> getBoxedType(final Class<?> type) {
return Enum.class;
}

if (type.isArray()) {
return Array.class;
}

if (Map.class.isAssignableFrom(type)) {
return Map.class;
}

return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.pivovarit.function.ThrowingFunction;
import java.io.IOException;
import java.lang.reflect.Array;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
Expand Down Expand Up @@ -50,14 +51,21 @@ public int unpackArrayHeader() throws IOException {

@Override
public @SuppressWarnings("unchecked") <V> V[] unpackArray() throws IOException {
final int length = underlyingUnpacker.unpackArrayHeader();

final V[] result = (V[]) new Object[length];
for (int index = 0; index < length; index++) {
result[index] = unpackAuto();
if (hasNextNilValue()) {
return null;
}

return result;
final String className = underlyingUnpacker.unpackString();
final Class<?> type = getClassByNameOrThrow(className);
return unpackOrNil(unpacker -> {
final int length = unpacker.unpackArrayHeader();
final V[] result = (V[]) Array.newInstance(type, length);
for (int index = 0; index < length; index++) {
result[index] = unpackAuto();
}

return result;
});
}

@Override
Expand Down Expand Up @@ -129,6 +137,10 @@ public int unpackMapHeader() throws IOException {
@Override
public <K, V> Map<K, V> unpackMap()
throws IOException {
if (hasNextNilValue()) {
return null;
}

final int length = unpackMapHeader();

final Map<K, V> result = new HashMap<>(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import moe.rafal.cory.subject.GameState;
Expand Down Expand Up @@ -275,6 +276,8 @@ void packAutoTest() throws IOException {
packer.packAuto(10);
packer.packAuto("test_string");
packer.packAuto(AWAITING);
packer.packAuto(Map.of("key", "value", "key2", "value2"));
packer.packAuto(new String[]{"value", "value1", "value2"});
try (PacketUnpacker unpacker = MessagePackPacketUnpackerFactory.INSTANCE.producePacketUnpacker(
packer.toBinaryArray())) {
assertThat(unpacker.unpackString())
Expand All @@ -287,6 +290,13 @@ void packAutoTest() throws IOException {
.isEqualTo("test_string");
assertThat((GameState) unpacker.unpackEnum())
.isEqualTo(AWAITING);
unpacker.skipValue();
assertThat(unpacker.unpackMap())
.containsKey("key")
.containsKey("key2");
unpacker.skipValue();
assertThat(unpacker.<String>unpackArray())
.contains("value", "value1", "value2");
}
}
}
Expand Down

0 comments on commit feca0d0

Please sign in to comment.