Skip to content

Commit

Permalink
Merge pull request #374 from lwlee2608/compatibility-check
Browse files Browse the repository at this point in the history
Add New ProtobufEncodingMode for Nullable and Google-Compatible Types
  • Loading branch information
vietj authored Oct 20, 2023
2 parents d44a385 + 39a50a1 commit f1002f4
Show file tree
Hide file tree
Showing 12 changed files with 782 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ message RecursiveItem {
RecursiveItem childC = 4;
}

message SimplePojo {
int32 integerField = 1;
int64 longField = 2;
bool booleanField = 3;
string stringField = 4;
}

message User {
string userName = 1;
int32 age = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@
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, ProtobufEncodingMode.VERTX);
}

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);
obj.setLatitude(0f);
}
int tag;
while ((tag = input.readTag()) != 0) {
switch (tag) {
Expand All @@ -33,36 +44,57 @@ public static void fromProto(CodedInputStream input, Address obj) throws IOExcep
break;
}
}
}
} // while loop
}

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

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

public static int toProto(Address obj, CodedOutputStream output, ExpandableIntArray cache, int index) 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;
if (obj.getName() != null) {
// name
if (compatibleMode && obj.getName() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getName() != null) || (compatibleMode && !obj.getName().isEmpty())) {
output.writeString(1, obj.getName());
}
if (obj.getLongitude() != null) {
// longitude
if (compatibleMode && obj.getLongitude() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getLongitude() != null) || (compatibleMode && obj.getLongitude() != 0f)) {
output.writeFloat(2, obj.getLongitude());
}
if (obj.getLatitude() != null) {
// latitude
if (compatibleMode && obj.getLatitude() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getLatitude() != null) || (compatibleMode && obj.getLatitude() != 0f)) {
output.writeFloat(3, obj.getLatitude());
}
return index;
}

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

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

public static int computeSize(Address obj, ExpandableIntArray cache, final int baseIndex) {
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,13 +10,25 @@
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 BookProtoConverter {

public static void fromProto(CodedInputStream input, Book obj) throws IOException {
fromProto(input, obj, ProtobufEncodingMode.VERTX);
}

public static void fromProto(CodedInputStream input, Book obj, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
if (compatibleMode) {
obj.setName("");
obj.setAuthor("");
obj.setIsbn("");
obj.setGenre("");
}
int tag;
while ((tag = input.readTag()) != 0) {
switch (tag) {
Expand All @@ -37,39 +49,64 @@ public static void fromProto(CodedInputStream input, Book obj) throws IOExceptio
break;
}
}
}
} // while loop
}

public static void toProto(Book obj, CodedOutputStream output) throws IOException {
toProto(obj, output, ProtobufEncodingMode.VERTX);
}

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

public static int toProto(Book obj, CodedOutputStream output, ExpandableIntArray cache, int index) throws IOException {
static int toProto(Book obj, CodedOutputStream output, ExpandableIntArray cache, int index, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
index = index + 1;
if (obj.getName() != null) {
// name
if (compatibleMode && obj.getName() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getName() != null) || (compatibleMode && !obj.getName().isEmpty())) {
output.writeString(1, obj.getName());
}
if (obj.getAuthor() != null) {
// author
if (compatibleMode && obj.getAuthor() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getAuthor() != null) || (compatibleMode && !obj.getAuthor().isEmpty())) {
output.writeString(3, obj.getAuthor());
}
if (obj.getIsbn() != null) {
// isbn
if (compatibleMode && obj.getIsbn() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getIsbn() != null) || (compatibleMode && !obj.getIsbn().isEmpty())) {
output.writeString(10, obj.getIsbn());
}
if (obj.getGenre() != null) {
// genre
if (compatibleMode && obj.getGenre() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getGenre() != null) || (compatibleMode && !obj.getGenre().isEmpty())) {
output.writeString(20, obj.getGenre());
}
return index;
}

public static int computeSize(Book obj) {
return computeSize(obj, ProtobufEncodingMode.VERTX);
}

public static int computeSize(Book obj, ProtobufEncodingMode encodingMode) {
ExpandableIntArray cache = new ExpandableIntArray(16);
BookProtoConverter.computeSize(obj, cache, 0);
BookProtoConverter.computeSize(obj, cache, 0, encodingMode);
return cache.get(0);
}

public static int computeSize(Book obj, ExpandableIntArray cache, final int baseIndex) {
static int computeSize(Book 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,13 +10,22 @@
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 PersonProtoConverter {

public static void fromProto(CodedInputStream input, Person obj) throws IOException {
fromProto(input, obj, ProtobufEncodingMode.VERTX);
}

public static void fromProto(CodedInputStream input, Person obj, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
if (compatibleMode) {
obj.setName("");
}
int tag;
while ((tag = input.readTag()) != 0) {
switch (tag) {
Expand All @@ -29,33 +38,47 @@ public static void fromProto(CodedInputStream input, Person obj) throws IOExcept
break;
}
}
}
} // while loop
}

public static void toProto(Person obj, CodedOutputStream output) throws IOException {
toProto(obj, output, ProtobufEncodingMode.VERTX);
}

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

public static int toProto(Person obj, CodedOutputStream output, ExpandableIntArray cache, int index) throws IOException {
static int toProto(Person obj, CodedOutputStream output, ExpandableIntArray cache, int index, ProtobufEncodingMode encodingMode) throws IOException {
boolean compatibleMode = encodingMode == ProtobufEncodingMode.GOOGLE_COMPATIBLE;
index = index + 1;
if (obj.getName() != null) {
// name
if (compatibleMode && obj.getName() == null) {
throw new IllegalArgumentException("Null values are not allowed for boxed types in compatibility mode");
}
if ((!compatibleMode && obj.getName() != null) || (compatibleMode && !obj.getName().isEmpty())) {
output.writeString(2, obj.getName());
}
// age
if (obj.getAge() != 0) {
output.writeInt32(4, obj.getAge());
}
return index;
}

public static int computeSize(Person obj) {
return computeSize(obj, ProtobufEncodingMode.VERTX);
}

public static int computeSize(Person obj, ProtobufEncodingMode encodingMode) {
ExpandableIntArray cache = new ExpandableIntArray(16);
PersonProtoConverter.computeSize(obj, cache, 0);
PersonProtoConverter.computeSize(obj, cache, 0, encodingMode);
return cache.get(0);
}

public static int computeSize(Person obj, ExpandableIntArray cache, final int baseIndex) {
static int computeSize(Person obj, ExpandableIntArray cache, final int baseIndex, ProtobufEncodingMode encodingMode) {
int size = 0;
int index = baseIndex + 1;
if (obj.getName() != null) {
Expand Down
Loading

0 comments on commit f1002f4

Please sign in to comment.