From 67131728d9e01cd40d987a6e8717cd498701b543 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 3 Sep 2023 11:23:50 +0800 Subject: [PATCH 1/2] refactor ProtoProperty.java --- .../src/test/proto/datetime.proto | 17 ----- .../src/test/proto/json-object.proto | 36 ---------- .../src/test/proto/test.proto | 44 ------------ .../generator/DataObjectProtobufGen.java | 61 ++++++++-------- .../protobuf/generator/ProtoFileGen.java | 5 +- .../protobuf/generator/ProtoProperty.java | 71 ++++++++++--------- 6 files changed, 68 insertions(+), 166 deletions(-) delete mode 100644 vertx-codegen-processor/src/test/proto/datetime.proto delete mode 100644 vertx-codegen-processor/src/test/proto/json-object.proto delete mode 100644 vertx-codegen-processor/src/test/proto/test.proto diff --git a/vertx-codegen-processor/src/test/proto/datetime.proto b/vertx-codegen-processor/src/test/proto/datetime.proto deleted file mode 100644 index b44f4af9e..000000000 --- a/vertx-codegen-processor/src/test/proto/datetime.proto +++ /dev/null @@ -1,17 +0,0 @@ -syntax = "proto3"; - -option java_multiple_files = true; -option java_package = "io.vertx.test.protoc.gen"; - -package io.vertx.protobuf; - -message ZonedDateTime { - int32 nanos = 1; - int64 seconds = 2; - string zoneId = 3; -} - -message Instant { - int32 nanos = 1; - int64 seconds = 2; -} diff --git a/vertx-codegen-processor/src/test/proto/json-object.proto b/vertx-codegen-processor/src/test/proto/json-object.proto deleted file mode 100644 index 274cb9216..000000000 --- a/vertx-codegen-processor/src/test/proto/json-object.proto +++ /dev/null @@ -1,36 +0,0 @@ -syntax = "proto3"; - -option java_multiple_files = true; -option java_package = "io.vertx.protobuf"; - -package io.vertx.protobuf; - -import "datetime.proto"; - -message JsonObject { - map fields = 1; -} - -message Value { - oneof kind { - NullValue null_value = 1; - JsonObject json_object_value = 2; - JsonArray json_array_value = 3; - bool bool_value = 4; - string string_value = 5; - int32 integer_value = 6; - int64 long_value = 7; - double double_value = 8; - float float_value = 9; - Instant instant_value = 10; - bytes binary_value = 11; - } -} - -enum NullValue { - NULL_VALUE = 0; -} - -message JsonArray { - repeated Value values = 1; -} diff --git a/vertx-codegen-processor/src/test/proto/test.proto b/vertx-codegen-processor/src/test/proto/test.proto deleted file mode 100644 index daf585b28..000000000 --- a/vertx-codegen-processor/src/test/proto/test.proto +++ /dev/null @@ -1,44 +0,0 @@ -syntax = "proto3"; - -option java_multiple_files = true; -option java_package = "io.vertx.test.protoc.gen"; - -package testing; - -import "vertx-struct.proto"; -import "datetime.proto"; - -message User { - Address address = 1; - int32 age = 2; - bool boolField = 3; - int32 charField = 4; - double doubleField = 5; - io.vertx.protobuf.Instant instantField = 6; - repeated int32 integerListField = 7; - map integerValueMap = 8; - io.vertx.protobuf.JsonObject jsonObjectField = 9; - int64 longField = 10; - bool primitiveBoolean = 11; - int32 primitiveByte = 12; - int32 primitiveChar = 13; - double primitiveDouble = 14; - float primitiveFloat = 15; - int32 primitiveInt = 16; - int64 primitiveLong = 17; - int32 primitiveShort = 18; - int32 shortField = 19; - map stringValueMap = 20; - repeated Address structListField = 21; - map structValueMap = 22; - string username = 23; - io.vertx.protobuf.ZonedDateTime zonedDateTimeField = 24; - repeated io.vertx.protobuf.ZonedDateTime zonedDateTimeListField = 25; - map zonedDateTimeValueMap = 26; -} - -message Address { - float latitude = 1; - float longitude = 2; - string name = 3; -} diff --git a/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/DataObjectProtobufGen.java b/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/DataObjectProtobufGen.java index 80c0e5cf2..82906c470 100644 --- a/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/DataObjectProtobufGen.java +++ b/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/DataObjectProtobufGen.java @@ -14,7 +14,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; -import java.util.Optional; /** * @author Julien Viet @@ -99,14 +98,14 @@ public String renderProto(DataObjectModel model, int index, int size, Map());\n"); writer.print(" }\n"); - writer.print(" obj." + prop.getGetterMethod() + "().add(" + ProtoProperty.getProtoConverter(builtInType, jsonProtoEncoding) + ".fromProto(input));\n"); + writer.print(" obj." + prop.getGetterMethod() + "().add(" + ProtoProperty.getBuiltInProtoConverter(builtInType, jsonProtoEncoding) + ".fromProto(input));\n"); writer.print(" input.popLimit(limit);\n"); writer.print(" break;\n"); } else { @@ -139,8 +138,8 @@ public String renderProto(DataObjectModel model, int index, int size, Map map = obj." + prop.getGetterMethod() + "();\n"); @@ -152,7 +151,7 @@ public String renderProto(DataObjectModel model, int index, int size, Map entry : obj." + prop.getGetterMethod() + "().entrySet()) {\n"); writer.print(" output.writeUInt32NoTag(" + protoProperty.getTag() + ");\n"); writer.print(" // calculate data size\n"); - writer.print(" int elementSize = " + ProtoProperty.getProtoConverter(builtInType, jsonProtoEncoding) + ".computeSize(entry.getValue());\n"); + writer.print(" int elementSize = " + ProtoProperty.getBuiltInProtoConverter(builtInType, jsonProtoEncoding) + ".computeSize(entry.getValue());\n"); writer.print(" int dataSize = 0;\n"); writer.print(" dataSize += CodedOutputStream.computeStringSize(1, entry.getKey());\n"); writer.print(" dataSize += CodedOutputStream.computeInt32SizeNoTag(18);\n"); @@ -308,7 +307,7 @@ public String renderProto(DataObjectModel model, int index, int size, Map entry : obj." + prop.getGetterMethod() + "().entrySet()) {\n"); @@ -334,11 +333,11 @@ public String renderProto(DataObjectModel model, int index, int size, Map 0) {\n"); writer.print(" for (" + builtInType + " element: obj." + prop.getGetterMethod() + "()) {\n"); writer.print(" size += CodedOutputStream.computeUInt32SizeNoTag(" + protoProperty.getTag() + ");\n"); - writer.print(" int dataSize = " + ProtoProperty.getProtoConverter(builtInType, jsonProtoEncoding) + ".computeSize(element);\n"); + writer.print(" int dataSize = " + ProtoProperty.getBuiltInProtoConverter(builtInType, jsonProtoEncoding) + ".computeSize(element);\n"); writer.print(" size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);\n"); writer.print(" size += dataSize;\n"); writer.print(" }\n"); @@ -431,8 +430,8 @@ public String renderProto(DataObjectModel model, int index, int size, Map entry : obj." + prop.getGetterMethod() + "().entrySet()) {\n"); writer.print(" size += CodedOutputStream.computeUInt32SizeNoTag(" + protoProperty.getTag() + ");\n"); writer.print(" // calculate data size\n"); @@ -440,7 +439,7 @@ public String renderProto(DataObjectModel model, int index, int size, Map Date: Sun, 3 Sep 2023 11:29:01 +0800 Subject: [PATCH 2/2] swap determineBuiltInType and determineIsNullable method to make the diff in PR less confusing --- .../protobuf/generator/ProtoProperty.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/ProtoProperty.java b/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/ProtoProperty.java index b3e246d06..f32429a3c 100644 --- a/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/ProtoProperty.java +++ b/vertx-codegen-protobuf/src/main/java/io/vertx/codegen/protobuf/generator/ProtoProperty.java @@ -97,21 +97,6 @@ private static ProtoType determinePrimitiveProtoType(String javaDataType) { } } - // Find out if the data type are io.vertx.protobuf builtin type, return Proto type - private static String determineBuiltInType(PropertyInfo prop) { - String javaDataType = prop.getType().getName(); - switch (javaDataType) { - case "java.time.ZonedDateTime": - return "ZonedDateTime"; - case "java.time.Instant": - return "Instant"; - case "io.vertx.core.json.JsonObject": - return "Struct"; - default: - return null; - } - } - // Anything other than java primitive type should be nullable private static boolean determineIsNullable(String javaDataType) { switch (javaDataType) { @@ -129,6 +114,21 @@ private static boolean determineIsNullable(String javaDataType) { } } + // Find out if the data type are io.vertx.protobuf builtin type, return Proto type + private static String determineBuiltInType(PropertyInfo prop) { + String javaDataType = prop.getType().getName(); + switch (javaDataType) { + case "java.time.ZonedDateTime": + return "ZonedDateTime"; + case "java.time.Instant": + return "Instant"; + case "io.vertx.core.json.JsonObject": + return "Struct"; + default: + return null; + } + } + static String getBuiltInProtoConverter(String builtInType, JsonProtoEncoding jsonProtoEncoding) { switch (builtInType) { case "ZonedDateTime":