From 6c93504b2cdb5aa19c4b5b752a36917a00f2b8fa Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Mon, 3 Feb 2025 21:27:19 +0100 Subject: [PATCH] autoprotomap: record support --- .../deployment/ProtoSerializerGenerator.java | 9 +++++++-- .../autoprotomap/it/InterfaceObject.java | 8 ++++++++ .../usatiuk/autoprotomap/it/RecordObject.java | 7 +++++++ .../autoprotomap/it/RecordObject2.java | 7 +++++++ .../src/main/proto/autoprotomap_test.proto | 16 ++++++++++++++++ .../it/AutoprotomapResourceTest.java | 19 ++++++++++++++++++- 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/InterfaceObject.java create mode 100644 dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject.java create mode 100644 dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject2.java diff --git a/dhfs-parent/autoprotomap/deployment/src/main/java/com/usatiuk/autoprotomap/deployment/ProtoSerializerGenerator.java b/dhfs-parent/autoprotomap/deployment/src/main/java/com/usatiuk/autoprotomap/deployment/ProtoSerializerGenerator.java index 386f79f1..6ed94f3a 100644 --- a/dhfs-parent/autoprotomap/deployment/src/main/java/com/usatiuk/autoprotomap/deployment/ProtoSerializerGenerator.java +++ b/dhfs-parent/autoprotomap/deployment/src/main/java/com/usatiuk/autoprotomap/deployment/ProtoSerializerGenerator.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Objects; import java.util.function.Consumer; +import java.util.function.Function; import java.util.function.IntConsumer; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -61,7 +62,7 @@ public class ProtoSerializerGenerator { visitor.accept(cur); var next = cur.superClassType().name(); - if (next.equals(DotName.OBJECT_NAME)) break; + if (next.equals(DotName.OBJECT_NAME) || next.equals(DotName.RECORD_NAME)) break; cur = index.getClassByName(next); } } @@ -82,6 +83,10 @@ public class ProtoSerializerGenerator { var objectClass = index.getClassByName(objectType.name().toString()); + Function getterGetter = objectClass.isRecord() + ? Function.identity() + : s -> "get" + capitalize(stripPrefix(s, FIELD_PREFIX)); + for (var f : findAllFields(index, objectClass)) { var consideredFieldName = stripPrefix(f.name(), FIELD_PREFIX); @@ -89,7 +94,7 @@ public class ProtoSerializerGenerator { if ((f.flags() & Opcodes.ACC_PUBLIC) != 0) return bytecodeCreator.readInstanceField(f, object); else { - var fieldGetter = "get" + capitalize(stripPrefix(f.name(), FIELD_PREFIX)); + var fieldGetter = getterGetter.apply(f.name()); return bytecodeCreator.invokeVirtualMethod( MethodDescriptor.ofMethod(objectType.toString(), fieldGetter, f.type().name().toString()), object); } diff --git a/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/InterfaceObject.java b/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/InterfaceObject.java new file mode 100644 index 00000000..7b06b316 --- /dev/null +++ b/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/InterfaceObject.java @@ -0,0 +1,8 @@ +package com.usatiuk.autoprotomap.it; + +import com.usatiuk.autoprotomap.runtime.ProtoMirror; + +@ProtoMirror(InterfaceObjectProto.class) +public interface InterfaceObject { + String key(); +} diff --git a/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject.java b/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject.java new file mode 100644 index 00000000..b314ca9a --- /dev/null +++ b/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject.java @@ -0,0 +1,7 @@ +package com.usatiuk.autoprotomap.it; + +import com.usatiuk.autoprotomap.runtime.ProtoMirror; + +@ProtoMirror(RecordObjectProto.class) +public record RecordObject(String key) implements InterfaceObject { +} diff --git a/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject2.java b/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject2.java new file mode 100644 index 00000000..4c66dfc3 --- /dev/null +++ b/dhfs-parent/autoprotomap/integration-tests/src/main/java/com/usatiuk/autoprotomap/it/RecordObject2.java @@ -0,0 +1,7 @@ +package com.usatiuk.autoprotomap.it; + +import com.usatiuk.autoprotomap.runtime.ProtoMirror; + +@ProtoMirror(RecordObject2Proto.class) +public record RecordObject2(String key, int value) implements InterfaceObject { +} diff --git a/dhfs-parent/autoprotomap/integration-tests/src/main/proto/autoprotomap_test.proto b/dhfs-parent/autoprotomap/integration-tests/src/main/proto/autoprotomap_test.proto index f606b3b4..c60bcec7 100644 --- a/dhfs-parent/autoprotomap/integration-tests/src/main/proto/autoprotomap_test.proto +++ b/dhfs-parent/autoprotomap/integration-tests/src/main/proto/autoprotomap_test.proto @@ -28,4 +28,20 @@ message AbstractProto { SimpleObjectProto simpleObject = 2; CustomObjectProto customObject = 3; } +} + +message RecordObjectProto { + string key = 1; +} + +message RecordObject2Proto { + string key = 1; + int32 value = 2; +} + +message InterfaceObjectProto { + oneof obj { + RecordObjectProto recordObject = 1; + RecordObject2Proto recordObject2 = 2; + } } \ No newline at end of file diff --git a/dhfs-parent/autoprotomap/integration-tests/src/test/java/com/usatiuk/autoprotomap/it/AutoprotomapResourceTest.java b/dhfs-parent/autoprotomap/integration-tests/src/test/java/com/usatiuk/autoprotomap/it/AutoprotomapResourceTest.java index 2d02ffd3..36f63bf6 100644 --- a/dhfs-parent/autoprotomap/integration-tests/src/test/java/com/usatiuk/autoprotomap/it/AutoprotomapResourceTest.java +++ b/dhfs-parent/autoprotomap/integration-tests/src/test/java/com/usatiuk/autoprotomap/it/AutoprotomapResourceTest.java @@ -16,6 +16,8 @@ public class AutoprotomapResourceTest { ProtoSerializer nestedProtoSerializer; @Inject ProtoSerializer abstractProtoSerializer; + @Inject + ProtoSerializer interfaceProtoSerializer; @Test public void testSimple() { @@ -74,7 +76,7 @@ public class AutoprotomapResourceTest { } @Test - public void tesAbstractNested() { + public void testAbstractNested() { var ret = abstractProtoSerializer.serialize( new NestedObject( new SimpleObject(333, "nested so", ByteString.copyFrom(new byte[]{1, 2, 3})), @@ -93,4 +95,19 @@ public class AutoprotomapResourceTest { Assertions.assertEquals("nested obj", des.get_nestedName()); Assertions.assertEquals(ByteString.copyFrom(new byte[]{4, 5, 6}), des.get_nestedSomeBytes()); } + + @Test + public void testInterface() { + var ret = interfaceProtoSerializer.serialize(new RecordObject("record test")); + Assertions.assertEquals("record test", ret.getRecordObject().getKey()); + var des = (RecordObject) interfaceProtoSerializer.deserialize(ret); + Assertions.assertEquals("record test", des.key()); + + var ret2 = interfaceProtoSerializer.serialize(new RecordObject2("record test 2", 1234)); + Assertions.assertEquals("record test 2", ret2.getRecordObject2().getKey()); + Assertions.assertEquals(1234, ret2.getRecordObject2().getValue()); + var des2 = (RecordObject2) interfaceProtoSerializer.deserialize(ret2); + Assertions.assertEquals("record test 2", des2.key()); + Assertions.assertEquals(1234, des2.value()); + } }