Skip to content

Commit

Permalink
add ProtobufEncodingMode enum
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Oct 14, 2023
1 parent 5bfddc8 commit 52d560b
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import io.vertx.codegen.protobuf.ProtobufEncodingMode;
import io.vertx.core.json.JsonObject;
import io.vertx.codegen.protobuf.utils.ExpandableIntArray;
import io.vertx.codegen.protobuf.converters.*;

public class AddressProtoConverter {

public static void fromProto(CodedInputStream input, Address obj) throws IOException {
fromProto(input, obj, false);
fromProto(input, obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void fromProto(CodedInputStream input, Address obj, boolean compatibleMode) throws IOException {
public static void fromProto(CodedInputStream input, Address obj, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
if (compatibleMode) {
obj.setName("");
obj.setLongitude(0f);
Expand All @@ -46,16 +48,17 @@ public static void fromProto(CodedInputStream input, Address obj, boolean compat
}

public static void toProto(Address obj, CodedOutputStream output) throws IOException {
toProto(obj, output, false);
toProto(obj, output, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void toProto(Address obj, CodedOutputStream output, boolean compatibleMode) throws IOException {
public static void toProto(Address obj, CodedOutputStream output, ProtobufEncodingMode encodingMode) throws IOException {
ExpandableIntArray cache = new ExpandableIntArray(16);
AddressProtoConverter.computeSize(obj, cache, 0, compatibleMode);
AddressProtoConverter.toProto(obj, output, cache, 0, compatibleMode);
AddressProtoConverter.computeSize(obj, cache, 0, encodingMode);
AddressProtoConverter.toProto(obj, output, cache, 0, encodingMode);
}

static int toProto(Address obj, CodedOutputStream output, ExpandableIntArray cache, int index, boolean compatibleMode) throws IOException {
static int toProto(Address obj, CodedOutputStream output, ExpandableIntArray cache, int index, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
index = index + 1;
// name
if (compatibleMode && obj.getName() == null) {
Expand All @@ -82,16 +85,16 @@ static int toProto(Address obj, CodedOutputStream output, ExpandableIntArray cac
}

public static int computeSize(Address obj) {
return computeSize(obj, false);
return computeSize(obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static int computeSize(Address obj, boolean compatibleMode) {
public static int computeSize(Address obj, ProtobufEncodingMode encodingMode) {
ExpandableIntArray cache = new ExpandableIntArray(16);
AddressProtoConverter.computeSize(obj, cache, 0, compatibleMode);
AddressProtoConverter.computeSize(obj, cache, 0, encodingMode);
return cache.get(0);
}

static int computeSize(Address obj, ExpandableIntArray cache, final int baseIndex, boolean compatibleMode) {
static int computeSize(Address obj, ExpandableIntArray cache, final int baseIndex, ProtobufEncodingMode encodingMode) {
int size = 0;
int index = baseIndex + 1;
if (obj.getName() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import io.vertx.codegen.protobuf.ProtobufEncodingMode;
import io.vertx.core.json.JsonObject;
import io.vertx.codegen.protobuf.utils.ExpandableIntArray;
import io.vertx.codegen.protobuf.converters.*;

public class RecursiveItemProtoConverter {

public static void fromProto(CodedInputStream input, RecursiveItem obj) throws IOException {
fromProto(input, obj, false);
fromProto(input, obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void fromProto(CodedInputStream input, RecursiveItem obj, boolean compatibleMode) throws IOException {
public static void fromProto(CodedInputStream input, RecursiveItem obj, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
if (compatibleMode) {
obj.setId("");
}
Expand Down Expand Up @@ -63,16 +65,17 @@ public static void fromProto(CodedInputStream input, RecursiveItem obj, boolean
}

public static void toProto(RecursiveItem obj, CodedOutputStream output) throws IOException {
toProto(obj, output, false);
toProto(obj, output, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void toProto(RecursiveItem obj, CodedOutputStream output, boolean compatibleMode) throws IOException {
public static void toProto(RecursiveItem obj, CodedOutputStream output, ProtobufEncodingMode encodingMode) throws IOException {
ExpandableIntArray cache = new ExpandableIntArray(16);
RecursiveItemProtoConverter.computeSize(obj, cache, 0, compatibleMode);
RecursiveItemProtoConverter.toProto(obj, output, cache, 0, compatibleMode);
RecursiveItemProtoConverter.computeSize(obj, cache, 0, encodingMode);
RecursiveItemProtoConverter.toProto(obj, output, cache, 0, encodingMode);
}

static int toProto(RecursiveItem obj, CodedOutputStream output, ExpandableIntArray cache, int index, boolean compatibleMode) throws IOException {
static int toProto(RecursiveItem obj, CodedOutputStream output, ExpandableIntArray cache, int index, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
index = index + 1;
// id
if (compatibleMode && obj.getId() == null) {
Expand All @@ -85,34 +88,34 @@ static int toProto(RecursiveItem obj, CodedOutputStream output, ExpandableIntArr
if (obj.getChildA() != null) {
output.writeUInt32NoTag(18);
output.writeUInt32NoTag(cache.get(index));
index = RecursiveItemProtoConverter.toProto(obj.getChildA(), output, cache, index, compatibleMode);
index = RecursiveItemProtoConverter.toProto(obj.getChildA(), output, cache, index, encodingMode);
}
// childB
if (obj.getChildB() != null) {
output.writeUInt32NoTag(26);
output.writeUInt32NoTag(cache.get(index));
index = RecursiveItemProtoConverter.toProto(obj.getChildB(), output, cache, index, compatibleMode);
index = RecursiveItemProtoConverter.toProto(obj.getChildB(), output, cache, index, encodingMode);
}
// childC
if (obj.getChildC() != null) {
output.writeUInt32NoTag(34);
output.writeUInt32NoTag(cache.get(index));
index = RecursiveItemProtoConverter.toProto(obj.getChildC(), output, cache, index, compatibleMode);
index = RecursiveItemProtoConverter.toProto(obj.getChildC(), output, cache, index, encodingMode);
}
return index;
}

public static int computeSize(RecursiveItem obj) {
return computeSize(obj, false);
return computeSize(obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static int computeSize(RecursiveItem obj, boolean compatibleMode) {
public static int computeSize(RecursiveItem obj, ProtobufEncodingMode encodingMode) {
ExpandableIntArray cache = new ExpandableIntArray(16);
RecursiveItemProtoConverter.computeSize(obj, cache, 0, compatibleMode);
RecursiveItemProtoConverter.computeSize(obj, cache, 0, encodingMode);
return cache.get(0);
}

static int computeSize(RecursiveItem obj, ExpandableIntArray cache, final int baseIndex, boolean compatibleMode) {
static int computeSize(RecursiveItem obj, ExpandableIntArray cache, final int baseIndex, ProtobufEncodingMode encodingMode) {
int size = 0;
int index = baseIndex + 1;
if (obj.getId() != null) {
Expand All @@ -121,23 +124,23 @@ static int computeSize(RecursiveItem obj, ExpandableIntArray cache, final int ba
if (obj.getChildA() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(18);
int savedIndex = index;
index = RecursiveItemProtoConverter.computeSize(obj.getChildA(), cache, index, compatibleMode);
index = RecursiveItemProtoConverter.computeSize(obj.getChildA(), cache, index, encodingMode);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
}
if (obj.getChildB() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(26);
int savedIndex = index;
index = RecursiveItemProtoConverter.computeSize(obj.getChildB(), cache, index, compatibleMode);
index = RecursiveItemProtoConverter.computeSize(obj.getChildB(), cache, index, encodingMode);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
}
if (obj.getChildC() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(34);
int savedIndex = index;
index = RecursiveItemProtoConverter.computeSize(obj.getChildC(), cache, index, compatibleMode);
index = RecursiveItemProtoConverter.computeSize(obj.getChildC(), cache, index, encodingMode);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import io.vertx.codegen.protobuf.ProtobufEncodingMode;
import io.vertx.core.json.JsonObject;
import io.vertx.codegen.protobuf.utils.ExpandableIntArray;
import io.vertx.codegen.protobuf.converters.*;

public class SimplePojoProtoConverter {

public static void fromProto(CodedInputStream input, SimplePojo obj) throws IOException {
fromProto(input, obj, false);
fromProto(input, obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void fromProto(CodedInputStream input, SimplePojo obj, boolean compatibleMode) throws IOException {
public static void fromProto(CodedInputStream input, SimplePojo obj, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
if (compatibleMode) {
obj.setIntegerField(0);
obj.setLongField(0L);
Expand Down Expand Up @@ -51,16 +53,17 @@ public static void fromProto(CodedInputStream input, SimplePojo obj, boolean com
}

public static void toProto(SimplePojo obj, CodedOutputStream output) throws IOException {
toProto(obj, output, false);
toProto(obj, output, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void toProto(SimplePojo obj, CodedOutputStream output, boolean compatibleMode) throws IOException {
public static void toProto(SimplePojo obj, CodedOutputStream output, ProtobufEncodingMode encodingMode) throws IOException {
ExpandableIntArray cache = new ExpandableIntArray(16);
SimplePojoProtoConverter.computeSize(obj, cache, 0, compatibleMode);
SimplePojoProtoConverter.toProto(obj, output, cache, 0, compatibleMode);
SimplePojoProtoConverter.computeSize(obj, cache, 0, encodingMode);
SimplePojoProtoConverter.toProto(obj, output, cache, 0, encodingMode);
}

static int toProto(SimplePojo obj, CodedOutputStream output, ExpandableIntArray cache, int index, boolean compatibleMode) throws IOException {
static int toProto(SimplePojo obj, CodedOutputStream output, ExpandableIntArray cache, int index, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
index = index + 1;
// integerField
if (compatibleMode && obj.getIntegerField() == null) {
Expand Down Expand Up @@ -94,16 +97,16 @@ static int toProto(SimplePojo obj, CodedOutputStream output, ExpandableIntArray
}

public static int computeSize(SimplePojo obj) {
return computeSize(obj, false);
return computeSize(obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static int computeSize(SimplePojo obj, boolean compatibleMode) {
public static int computeSize(SimplePojo obj, ProtobufEncodingMode encodingMode) {
ExpandableIntArray cache = new ExpandableIntArray(16);
SimplePojoProtoConverter.computeSize(obj, cache, 0, compatibleMode);
SimplePojoProtoConverter.computeSize(obj, cache, 0, encodingMode);
return cache.get(0);
}

static int computeSize(SimplePojo obj, ExpandableIntArray cache, final int baseIndex, boolean compatibleMode) {
static int computeSize(SimplePojo obj, ExpandableIntArray cache, final int baseIndex, ProtobufEncodingMode encodingMode) {
int size = 0;
int index = baseIndex + 1;
if (obj.getIntegerField() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import io.vertx.codegen.protobuf.ProtobufEncodingMode;
import io.vertx.core.json.JsonObject;
import io.vertx.codegen.protobuf.utils.ExpandableIntArray;
import io.vertx.codegen.protobuf.converters.*;

public class UserProtoConverter {

public static void fromProto(CodedInputStream input, User obj) throws IOException {
fromProto(input, obj, false);
fromProto(input, obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void fromProto(CodedInputStream input, User obj, boolean compatibleMode) throws IOException {
public static void fromProto(CodedInputStream input, User obj, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
if (compatibleMode) {
obj.setUserName("");
obj.setAge(0);
Expand Down Expand Up @@ -288,16 +290,17 @@ public static void fromProto(CodedInputStream input, User obj, boolean compatibl
}

public static void toProto(User obj, CodedOutputStream output) throws IOException {
toProto(obj, output, false);
toProto(obj, output, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static void toProto(User obj, CodedOutputStream output, boolean compatibleMode) throws IOException {
public static void toProto(User obj, CodedOutputStream output, ProtobufEncodingMode encodingMode) throws IOException {
ExpandableIntArray cache = new ExpandableIntArray(16);
UserProtoConverter.computeSize(obj, cache, 0, compatibleMode);
UserProtoConverter.toProto(obj, output, cache, 0, compatibleMode);
UserProtoConverter.computeSize(obj, cache, 0, encodingMode);
UserProtoConverter.toProto(obj, output, cache, 0, encodingMode);
}

static int toProto(User obj, CodedOutputStream output, ExpandableIntArray cache, int index, boolean compatibleMode) throws IOException {
static int toProto(User obj, CodedOutputStream output, ExpandableIntArray cache, int index, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
index = index + 1;
// userName
if (compatibleMode && obj.getUserName() == null) {
Expand Down Expand Up @@ -335,7 +338,7 @@ static int toProto(User obj, CodedOutputStream output, ExpandableIntArray cache,
for (Address element: obj.getStructListField()) {
output.writeUInt32NoTag(34);
output.writeUInt32NoTag(cache.get(index));
index = AddressProtoConverter.toProto(element, output, cache, index, compatibleMode);
index = AddressProtoConverter.toProto(element, output, cache, index, encodingMode);
}
}
// zonedDateTimeListField
Expand All @@ -362,7 +365,7 @@ static int toProto(User obj, CodedOutputStream output, ExpandableIntArray cache,
if (obj.getAddress() != null) {
output.writeUInt32NoTag(58);
output.writeUInt32NoTag(cache.get(index));
index = AddressProtoConverter.toProto(obj.getAddress(), output, cache, index, compatibleMode);
index = AddressProtoConverter.toProto(obj.getAddress(), output, cache, index, encodingMode);
}
// byteField
if (obj.getByteField() != null) {
Expand Down Expand Up @@ -460,7 +463,7 @@ static int toProto(User obj, CodedOutputStream output, ExpandableIntArray cache,
output.writeString(1, entry.getKey());
output.writeUInt32NoTag(18);
output.writeUInt32NoTag(elementSize);
index = AddressProtoConverter.toProto(entry.getValue(), output, cache, index, compatibleMode);
index = AddressProtoConverter.toProto(entry.getValue(), output, cache, index, encodingMode);
}
}
// jsonValueMap
Expand Down Expand Up @@ -581,16 +584,16 @@ static int toProto(User obj, CodedOutputStream output, ExpandableIntArray cache,
}

public static int computeSize(User obj) {
return computeSize(obj, false);
return computeSize(obj, ProtobufEncodingMode.VERTX_NULLABLE);
}

public static int computeSize(User obj, boolean compatibleMode) {
public static int computeSize(User obj, ProtobufEncodingMode encodingMode) {
ExpandableIntArray cache = new ExpandableIntArray(16);
UserProtoConverter.computeSize(obj, cache, 0, compatibleMode);
UserProtoConverter.computeSize(obj, cache, 0, encodingMode);
return cache.get(0);
}

static int computeSize(User obj, ExpandableIntArray cache, final int baseIndex, boolean compatibleMode) {
static int computeSize(User obj, ExpandableIntArray cache, final int baseIndex, ProtobufEncodingMode encodingMode) {
int size = 0;
int index = baseIndex + 1;
if (obj.getUserName() != null) {
Expand Down Expand Up @@ -618,7 +621,7 @@ static int computeSize(User obj, ExpandableIntArray cache, final int baseIndex,
for (Address element: obj.getStructListField()) {
size += CodedOutputStream.computeUInt32SizeNoTag(34);
int savedIndex = index;
index = AddressProtoConverter.computeSize(element, cache, index, compatibleMode);
index = AddressProtoConverter.computeSize(element, cache, index, encodingMode);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
Expand Down Expand Up @@ -652,7 +655,7 @@ static int computeSize(User obj, ExpandableIntArray cache, final int baseIndex,
if (obj.getAddress() != null) {
size += CodedOutputStream.computeUInt32SizeNoTag(58);
int savedIndex = index;
index = AddressProtoConverter.computeSize(obj.getAddress(), cache, index, compatibleMode);
index = AddressProtoConverter.computeSize(obj.getAddress(), cache, index, encodingMode);
int dataSize = cache.get(savedIndex);
size += CodedOutputStream.computeUInt32SizeNoTag(dataSize);
size += dataSize;
Expand Down Expand Up @@ -713,7 +716,7 @@ static int computeSize(User obj, ExpandableIntArray cache, final int baseIndex,
dataSize += CodedOutputStream.computeStringSize(1, entry.getKey());
// value
int savedIndex = index;
index = AddressProtoConverter.computeSize(entry.getValue(), cache, index, compatibleMode);
index = AddressProtoConverter.computeSize(entry.getValue(), cache, index, encodingMode);
int elementSize = cache.get(savedIndex);
dataSize += CodedOutputStream.computeInt32SizeNoTag(18);
dataSize += CodedOutputStream.computeInt32SizeNoTag(elementSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.vertx.codegen.protobuf;

/**
* Enumeration representing different encoding modes for Protocol Buffers encoding.
*/
public enum ProtobufEncodingMode {
/**
* In this encoding mode, the converter uses a non-standard protobuf encoding to allow boxed types
* (e.g., Integer, Double) and String to be nullable. This encoding mode is intended for use when
* communicating with another Vert.x converter that supports nullable values.
*/
VERTX_NULLABLE,

/**
* In this encoding mode, the converter uses the standard protobuf encoding, which is compatible with
* Google's protobuf decoder. Null values are not allowed for boxed types and String in this mode.
*/
GOOGLE_COMPATIBLE,
}
Loading

0 comments on commit 52d560b

Please sign in to comment.