Skip to content

Commit

Permalink
Merge pull request #369 from lwlee2608/feature/add-enum-type-support
Browse files Browse the repository at this point in the history
add enum type support for protobuf-codegen
  • Loading branch information
vietj authored Sep 15, 2023
2 parents 643d1c2 + 7ca4470 commit 397d976
Show file tree
Hide file tree
Showing 12 changed files with 923 additions and 739 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public boolean process() {
if (modelElt.getKind() != ElementKind.ENUM) {
throw new GenException(modelElt, "@VertxGen can only be used with interfaces or enums" + modelElt.asType().toString());
}
processTypeAnnotations();
doc = docFactory.createDoc(modelElt);
if (doc != null) {
doc.getBlockTags().stream().filter(tag -> tag.getName().equals("deprecated")).findFirst().ifPresent(tag ->
deprecatedDesc = new Text(Helper.normalizeWhitespaces(tag.getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt))
);
);
}
type = (EnumTypeInfo) typeMirrorFactory.create(modelElt.asType());
Helper.checkUnderModule(this, "@VertxGen");
Expand Down Expand Up @@ -101,6 +102,11 @@ private void processTypeAnnotations() {
this.annotations = elementUtils.getAllAnnotationMirrors(modelElt).stream().map(annotationValueInfoFactory::processAnnotation).collect(Collectors.toList());
}

@Override
public List<AnnotationValueInfo> getAnnotations() {
return annotations;
}

/**
* @return the type of this enum model
*/
Expand Down
77 changes: 42 additions & 35 deletions vertx-codegen-protobuf/src/converters/generated/dataobjects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,55 @@ import "vertx-struct.proto";
import "datetime.proto";

message Address {
float latitude = 1;
string name = 1;
float longitude = 2;
string name = 3;
float latitude = 3;
}

enum EnumType {
A = 0;
B = 1;
C = 2;
}

message RecursiveItem {
RecursiveItem childA = 1;
RecursiveItem childB = 2;
RecursiveItem childC = 3;
string id = 4;
string id = 1;
RecursiveItem childA = 2;
RecursiveItem childB = 3;
RecursiveItem childC = 4;
}

message User {
Address address = 1;
string userName = 1;
int32 age = 2;
bool boolField = 3;
int32 byteField = 4;
int32 charField = 5;
double doubleField = 6;
float floatField = 7;
io.vertx.protobuf.Instant instantField = 8;
repeated int32 integerListField = 9;
map<string, int32> integerValueMap = 10;
repeated io.vertx.protobuf.Struct jsonListField = 11;
io.vertx.protobuf.Struct jsonObjectField = 12;
map<string, io.vertx.protobuf.Struct> jsonValueMap = 13;
int64 longField = 14;
bool primitiveBoolean = 15;
int32 primitiveByte = 16;
int32 primitiveChar = 17;
double primitiveDouble = 18;
float primitiveFloat = 19;
int32 primitiveInt = 20;
int64 primitiveLong = 21;
int32 primitiveShort = 22;
int32 shortField = 23;
map<string, string> stringValueMap = 24;
repeated Address structListField = 25;
map<string, Address> structValueMap = 26;
string userName = 27;
io.vertx.protobuf.ZonedDateTime zonedDateTimeField = 28;
repeated io.vertx.protobuf.ZonedDateTime zonedDateTimeListField = 29;
map<string, io.vertx.protobuf.ZonedDateTime> zonedDateTimeValueMap = 30;
repeated int32 integerListField = 3;
repeated Address structListField = 4;
repeated io.vertx.protobuf.ZonedDateTime zonedDateTimeListField = 5;
repeated io.vertx.protobuf.Struct jsonListField = 6;
Address address = 7;
int32 byteField = 8;
double doubleField = 9;
float floatField = 10;
int64 longField = 11;
bool boolField = 12;
int32 shortField = 13;
int32 charField = 14;
map<string, string> stringValueMap = 15;
map<string, int32> integerValueMap = 16;
map<string, Address> structValueMap = 17;
map<string, io.vertx.protobuf.Struct> jsonValueMap = 18;
map<string, io.vertx.protobuf.ZonedDateTime> zonedDateTimeValueMap = 19;
io.vertx.protobuf.ZonedDateTime zonedDateTimeField = 20;
io.vertx.protobuf.Instant instantField = 21;
io.vertx.protobuf.Struct jsonObjectField = 22;
bool primitiveBoolean = 23;
int32 primitiveByte = 24;
int32 primitiveShort = 25;
int32 primitiveInt = 26;
int64 primitiveLong = 27;
float primitiveFloat = 28;
double primitiveDouble = 29;
int32 primitiveChar = 30;
EnumType enumType = 31;
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public static void fromProto(CodedInputStream input, Address obj) throws IOExcep
int tag;
while ((tag = input.readTag()) != 0) {
switch (tag) {
case 13: {
obj.setLatitude(input.readFloat());
case 10: {
obj.setName(input.readString());
break;
}
case 21: {
obj.setLongitude(input.readFloat());
break;
}
case 26: {
obj.setName(input.readString());
case 29: {
obj.setLatitude(input.readFloat());
break;
}
}
Expand All @@ -44,14 +44,14 @@ public static void toProto(Address obj, CodedOutputStream output) throws IOExcep

public static int toProto(Address obj, CodedOutputStream output, ExpandableIntArray cache, int index) throws IOException {
index = index + 1;
if (obj.getLatitude() != null) {
output.writeFloat(1, obj.getLatitude());
if (obj.getName() != null) {
output.writeString(1, obj.getName());
}
if (obj.getLongitude() != null) {
output.writeFloat(2, obj.getLongitude());
}
if (obj.getName() != null) {
output.writeString(3, obj.getName());
if (obj.getLatitude() != null) {
output.writeFloat(3, obj.getLatitude());
}
return index;
}
Expand All @@ -65,14 +65,14 @@ public static int computeSize(Address obj) {
public static int computeSize(Address obj, ExpandableIntArray cache, final int baseIndex) {
int size = 0;
int index = baseIndex + 1;
if (obj.getLatitude() != null) {
size += CodedOutputStream.computeFloatSize(1, obj.getLatitude());
if (obj.getName() != null) {
size += CodedOutputStream.computeStringSize(1, obj.getName());
}
if (obj.getLongitude() != null) {
size += CodedOutputStream.computeFloatSize(2, obj.getLongitude());
}
if (obj.getName() != null) {
size += CodedOutputStream.computeStringSize(3, obj.getName());
if (obj.getLatitude() != null) {
size += CodedOutputStream.computeFloatSize(3, obj.getLatitude());
}
cache.set(baseIndex, size);
return index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static void fromProto(CodedInputStream input, RecursiveItem obj) throws I
while ((tag = input.readTag()) != 0) {
switch (tag) {
case 10: {
obj.setId(input.readString());
break;
}
case 18: {
int length = input.readUInt32();
int limit = input.pushLimit(length);
RecursiveItem nested = new RecursiveItem();
Expand All @@ -29,7 +33,7 @@ public static void fromProto(CodedInputStream input, RecursiveItem obj) throws I
input.popLimit(limit);
break;
}
case 18: {
case 26: {
int length = input.readUInt32();
int limit = input.pushLimit(length);
RecursiveItem nested = new RecursiveItem();
Expand All @@ -38,7 +42,7 @@ public static void fromProto(CodedInputStream input, RecursiveItem obj) throws I
input.popLimit(limit);
break;
}
case 26: {
case 34: {
int length = input.readUInt32();
int limit = input.pushLimit(length);
RecursiveItem nested = new RecursiveItem();
Expand All @@ -47,10 +51,6 @@ public static void fromProto(CodedInputStream input, RecursiveItem obj) throws I
input.popLimit(limit);
break;
}
case 34: {
obj.setId(input.readString());
break;
}
}
}
}
Expand All @@ -63,24 +63,24 @@ public static void toProto(RecursiveItem obj, CodedOutputStream output) throws I

public static int toProto(RecursiveItem obj, CodedOutputStream output, ExpandableIntArray cache, int index) throws IOException {
index = index + 1;
if (obj.getId() != null) {
output.writeString(1, obj.getId());
}
if (obj.getChildA() != null) {
output.writeUInt32NoTag(10);
output.writeUInt32NoTag(18);
output.writeUInt32NoTag(cache.get(index));
index = RecursiveItemProtoConverter.toProto(obj.getChildA(), output, cache, index);
}
if (obj.getChildB() != null) {
output.writeUInt32NoTag(18);
output.writeUInt32NoTag(26);
output.writeUInt32NoTag(cache.get(index));
index = RecursiveItemProtoConverter.toProto(obj.getChildB(), output, cache, index);
}
if (obj.getChildC() != null) {
output.writeUInt32NoTag(26);
output.writeUInt32NoTag(34);
output.writeUInt32NoTag(cache.get(index));
index = RecursiveItemProtoConverter.toProto(obj.getChildC(), output, cache, index);
}
if (obj.getId() != null) {
output.writeString(4, obj.getId());
}
return index;
}

Expand All @@ -93,33 +93,33 @@ public static int computeSize(RecursiveItem obj) {
public static int computeSize(RecursiveItem obj, ExpandableIntArray cache, final int baseIndex) {
int size = 0;
int index = baseIndex + 1;
if (obj.getId() != null) {
size += CodedOutputStream.computeStringSize(1, obj.getId());
}
if (obj.getChildA() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(10);
size += CodedOutputStream.computeUInt32SizeNoTag(18);
int savedIndex = index;
index = RecursiveItemProtoConverter.computeSize(obj.getChildA(), cache, index);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
}
if (obj.getChildB() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(18);
size += CodedOutputStream.computeUInt32SizeNoTag(26);
int savedIndex = index;
index = RecursiveItemProtoConverter.computeSize(obj.getChildB(), cache, index);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
}
if (obj.getChildC() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(26);
size += CodedOutputStream.computeUInt32SizeNoTag(34);
int savedIndex = index;
index = RecursiveItemProtoConverter.computeSize(obj.getChildC(), cache, index);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
}
if (obj.getId() != null) {
size += CodedOutputStream.computeStringSize(4, obj.getId());
}
cache.set(baseIndex, size);
return index;
}
Expand Down
Loading

0 comments on commit 397d976

Please sign in to comment.