Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
  • Loading branch information
anthony-swirldslabs committed May 30, 2024
1 parent 9c9f29c commit a0fc753
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ default OneOfField parent() {
return null;
}

/**
* Extract the name of the Java model class for a message type,
* or null if the type is not a message.
*/
static String extractMessageTypeName(final Protobuf3Parser.Type_Context typeContext) {
return typeContext.messageType() == null ? null : typeContext.messageType().messageName().getText();
}

/**
* Extract the name of the Java package for a given FileType for a message type,
* or null if the type is not a message.
*/
static String extractMessageTypePackage(
final Protobuf3Parser.Type_Context typeContext,
final FileType fileType,
final ContextualLookupHelper lookupHelper) {
return typeContext.messageType() == null || typeContext.messageType().messageName().getText() == null ? null :
lookupHelper.getPackageFieldMessageType(fileType, typeContext);
}

/**
* Field type enum for use in field classes
*/
Expand Down Expand Up @@ -245,6 +265,7 @@ enum FieldType {
* Construct a new FieldType enum
*
* @param javaType The type of field type in Java code
* @param boxedType The boxed type of the field type, e.g. Integer for an int field.
* @param javaDefault The field type default value in Java code
* @param wireType The protobuf wire type for field type
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ public MapField(Protobuf3Parser.MapFieldContext mapContext, final ContextualLook
FieldType.of(mapContext.type_(), lookupHelper),
2,
"___" + mapContext.mapName().getText() + "__value",
mapContext.type_().messageType() == null ? null : mapContext.type_().messageType().messageName().getText(),
mapContext.type_().messageType() == null || mapContext.type_().messageType().messageName().getText() == null ? null :
lookupHelper.getPackageFieldMessageType(FileType.MODEL, mapContext.type_()),
mapContext.type_().messageType() == null || mapContext.type_().messageType().messageName().getText() == null ? null :
lookupHelper.getPackageFieldMessageType(FileType.CODEC, mapContext.type_()),
mapContext.type_().messageType() == null || mapContext.type_().messageType().messageName().getText() == null ? null :
lookupHelper.getPackageFieldMessageType(FileType.TEST, mapContext.type_()),
Field.extractMessageTypeName(mapContext.type_()),
Field.extractMessageTypePackage(mapContext.type_(), FileType.MODEL, lookupHelper),
Field.extractMessageTypePackage(mapContext.type_(), FileType.CODEC, lookupHelper),
Field.extractMessageTypePackage(mapContext.type_(), FileType.TEST, lookupHelper),
"An internal, private map entry value for " + mapContext.mapName().getText(),
false,
null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ public SingleField(Protobuf3Parser.FieldContext fieldContext, final ContextualLo
FieldType.of(fieldContext.type_(), lookupHelper),
Integer.parseInt(fieldContext.fieldNumber().getText()),
fieldContext.fieldName().getText(),
(fieldContext.type_().messageType() == null) ? null :
fieldContext.type_().messageType().messageName().getText(),
(fieldContext.type_().messageType() == null || fieldContext.type_().messageType().messageName().getText() == null) ? null :
lookupHelper.getPackageFieldMessageType(FileType.MODEL, fieldContext),
(fieldContext.type_().messageType() == null || fieldContext.type_().messageType().messageName().getText() == null) ? null :
lookupHelper.getPackageFieldMessageType(FileType.CODEC, fieldContext),
(fieldContext.type_().messageType() == null || fieldContext.type_().messageType().messageName().getText() == null) ? null :
lookupHelper.getPackageFieldMessageType(FileType.TEST, fieldContext),
Field.extractMessageTypeName(fieldContext.type_()),
Field.extractMessageTypePackage(fieldContext.type_(), FileType.MODEL, lookupHelper),
Field.extractMessageTypePackage(fieldContext.type_(), FileType.CODEC, lookupHelper),
Field.extractMessageTypePackage(fieldContext.type_(), FileType.TEST, lookupHelper),
Common.buildCleanFieldJavaDoc(Integer.parseInt(fieldContext.fieldNumber().getText()), fieldContext.docComment()),
getDeprecatedOption(fieldContext.fieldOptions()),
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ private static RuntimeException unsupported() {
// ================================================================================================================
// STANDARD WRITE METHODS

/**
* Write a integer to data output
*
* @param out The data output to write to
* @param field the descriptor for the field we are writing
* @param value the int value to write
*/
public static void writeInteger(WritableSequentialData out, FieldDefinition field, int value) {
writeInteger(out, field, value, true);
}

/**
* Write a integer to data output
*
Expand Down Expand Up @@ -123,6 +134,17 @@ assert switch(field.type()) {
}
}

/**
* Write a long to data output
*
* @param out The data output to write to
* @param field the descriptor for the field we are writing
* @param value the long value to write
*/
public static void writeLong(WritableSequentialData out, FieldDefinition field, long value) {
writeLong(out, field, value, true);
}

/**
* Write a long to data output
*
Expand Down Expand Up @@ -195,6 +217,17 @@ public static void writeDouble(WritableSequentialData out, FieldDefinition field
out.writeDouble(value, ByteOrder.LITTLE_ENDIAN);
}

/**
* Write a boolean to data output
*
* @param out The data output to write to
* @param field the descriptor for the field we are writing
* @param value the boolean value to write
*/
public static void writeBoolean(WritableSequentialData out, FieldDefinition field, boolean value) {
writeBoolean(out, field, value, true);
}

/**
* Write a boolean to data output
*
Expand Down Expand Up @@ -231,6 +264,19 @@ public static void writeEnum(WritableSequentialData out, FieldDefinition field,
out.writeVarInt(enumValue.protoOrdinal(), false);
}

/**
* Write a string to data output, assuming the field is non-repeated.
*
* @param out The data output to write to
* @param field the descriptor for the field we are writing, the field must be non-repeated
* @param value the string value to write
* @throws IOException If a I/O error occurs
*/
public static void writeString(final WritableSequentialData out, final FieldDefinition field,
final String value) throws IOException {
writeString(out, field, value, true);
}

/**
* Write a string to data output, assuming the field is non-repeated.
*
Expand Down Expand Up @@ -261,6 +307,19 @@ public static void writeOneRepeatedString(final WritableSequentialData out, fina
final String value) throws IOException {
assert field.type() == FieldType.STRING : "Not a string type " + field;
assert field.repeated() : "writeOneRepeatedString can only be used with repeated fields";
writeStringNoChecks(out, field, value);
}

/**
* Write a integer to data output - no validation checks.
*
* @param out The data output to write to
* @param field the descriptor for the field we are writing
* @param value the string value to write
* @throws IOException If a I/O error occurs
*/
private static void writeStringNoChecks(final WritableSequentialData out, final FieldDefinition field,
final String value) throws IOException {
writeStringNoChecks(out, field, value, true);
}

Expand All @@ -284,6 +343,20 @@ private static void writeStringNoChecks(final WritableSequentialData out, final
Utf8Tools.encodeUtf8(value, out);
}

/**
* Write a bytes to data output, assuming the corresponding field is non-repeated, and field type
* is any delimited: bytes, string, or message.
*
* @param out The data output to write to
* @param field the descriptor for the field we are writing, the field must not be repeated
* @param value the bytes value to write
* @throws IOException If a I/O error occurs
*/
public static void writeBytes(final WritableSequentialData out, final FieldDefinition field,
final RandomAccessData value) throws IOException {
writeBytes(out, field, value, true);
}

/**
* Write a bytes to data output, assuming the corresponding field is non-repeated, and field type
* is any delimited: bytes, string, or message.
Expand Down Expand Up @@ -456,8 +529,8 @@ public static void writeOptionalInteger(WritableSequentialData out, FieldDefinit
if (value != null) {
writeTag(out, field, WIRE_TYPE_DELIMITED);
final var newField = field.type().optionalFieldDefinition;
out.writeVarInt(sizeOfInteger(newField, value, true), false);
writeInteger(out, newField, value, true);
out.writeVarInt(sizeOfInteger(newField, value), false);
writeInteger(out, newField, value);
}
}

Expand All @@ -472,8 +545,8 @@ public static void writeOptionalLong(WritableSequentialData out, FieldDefinition
if (value != null) {
writeTag(out, field, WIRE_TYPE_DELIMITED);
final var newField = field.type().optionalFieldDefinition;
out.writeVarInt(sizeOfLong(newField, value, true), false);
writeLong(out, newField, value, true);
out.writeVarInt(sizeOfLong(newField, value), false);
writeLong(out, newField, value);
}
}

Expand Down Expand Up @@ -520,8 +593,8 @@ public static void writeOptionalBoolean(WritableSequentialData out, FieldDefinit
if (value != null) {
writeTag(out, field, WIRE_TYPE_DELIMITED);
final var newField = field.type().optionalFieldDefinition;
out.writeVarInt(sizeOfBoolean(newField, value, true), false);
writeBoolean(out, newField, value, true);
out.writeVarInt(sizeOfBoolean(newField, value), false);
writeBoolean(out, newField, value);
}
}

Expand All @@ -537,8 +610,8 @@ public static void writeOptionalString(WritableSequentialData out, FieldDefiniti
if (value != null) {
writeTag(out, field, WIRE_TYPE_DELIMITED);
final var newField = field.type().optionalFieldDefinition;
out.writeVarInt(sizeOfString(newField, value, true), false);
writeString(out, newField, value, true);
out.writeVarInt(sizeOfString(newField, value), false);
writeString(out, newField, value);
}
}

Expand All @@ -554,10 +627,10 @@ public static void writeOptionalBytes(WritableSequentialData out, FieldDefinitio
if (value != null) {
writeTag(out, field, WIRE_TYPE_DELIMITED);
final var newField = field.type().optionalFieldDefinition;
final int size = sizeOfBytes(newField, value, true);
final int size = sizeOfBytes(newField, value);
out.writeVarInt(size, false);
if (size > 0) {
writeBytes(out,newField, value, true);
writeBytes(out,newField, value);
}
}
}
Expand Down Expand Up @@ -999,7 +1072,7 @@ public static int sizeOfTag(final FieldDefinition field, final ProtoConstants wi
public static int sizeOfOptionalInteger(FieldDefinition field, @Nullable Integer value) {
if (value != null) {
final int intValue = value;
int size = sizeOfInteger(field.type().optionalFieldDefinition, intValue, true);
int size = sizeOfInteger(field.type().optionalFieldDefinition, intValue);
return sizeOfTag(field, WIRE_TYPE_DELIMITED) + sizeOfUnsignedVarInt32(size) + size;
}
return 0;
Expand All @@ -1015,7 +1088,7 @@ public static int sizeOfOptionalInteger(FieldDefinition field, @Nullable Integer
public static int sizeOfOptionalLong(FieldDefinition field, @Nullable Long value) {
if (value != null) {
final long longValue = value;
final int size = sizeOfLong(field.type().optionalFieldDefinition, longValue, true);
final int size = sizeOfLong(field.type().optionalFieldDefinition, longValue);
return sizeOfTag(field, WIRE_TYPE_DELIMITED) + sizeOfUnsignedVarInt32(size) + size;
}
return 0;
Expand Down Expand Up @@ -1075,7 +1148,7 @@ public static int sizeOfOptionalBoolean(FieldDefinition field, @Nullable Boolean
*/
public static int sizeOfOptionalString(FieldDefinition field, @Nullable String value) {
if (value != null) {
final int size = sizeOfString(field.type().optionalFieldDefinition, value, true);
final int size = sizeOfString(field.type().optionalFieldDefinition, value);
return sizeOfTag(field, WIRE_TYPE_DELIMITED) + sizeOfUnsignedVarInt32(size) + size;
}
return 0;
Expand All @@ -1090,12 +1163,23 @@ public static int sizeOfOptionalString(FieldDefinition field, @Nullable String v
*/
public static int sizeOfOptionalBytes(FieldDefinition field, @Nullable RandomAccessData value) {
if (value != null) {
final int size = sizeOfBytes(field.type().optionalFieldDefinition, value, true);
final int size = sizeOfBytes(field.type().optionalFieldDefinition, value);
return sizeOfTag(field, WIRE_TYPE_DELIMITED) + sizeOfUnsignedVarInt32(size) + size;
}
return 0;
}

/**
* Get number of bytes that would be needed to encode an integer field
*
* @param field descriptor of field
* @param value integer value to get encoded size for
* @return the number of bytes for encoded value
*/
public static int sizeOfInteger(FieldDefinition field, int value) {
return sizeOfInteger(field, value, true);
}

/**
* Get number of bytes that would be needed to encode an integer field
*
Expand All @@ -1115,6 +1199,17 @@ public static int sizeOfInteger(FieldDefinition field, int value, boolean skipDe
};
}

/**
* Get number of bytes that would be needed to encode a long field
*
* @param field descriptor of field
* @param value long value to get encoded size for
* @return the number of bytes for encoded value
*/
public static int sizeOfLong(FieldDefinition field, long value) {
return sizeOfLong(field, value, true);
}

/**
* Get number of bytes that would be needed to encode a long field
*
Expand Down Expand Up @@ -1157,6 +1252,17 @@ public static int sizeOfDouble(FieldDefinition field, double value) {
return sizeOfTag(field, WIRE_TYPE_VARINT_OR_ZIGZAG) + FIXED64_SIZE;
}

/**
* Get number of bytes that would be needed to encode a boolean field
*
* @param field descriptor of field
* @param value boolean value to get encoded size for
* @return the number of bytes for encoded value
*/
public static int sizeOfBoolean(FieldDefinition field, boolean value) {
return sizeOfBoolean(field, value, true);
}

/**
* Get number of bytes that would be needed to encode a boolean field
*
Expand Down Expand Up @@ -1184,6 +1290,17 @@ public static int sizeOfEnum(FieldDefinition field, EnumWithProtoMetadata enumVa
return sizeOfTag(field, WIRE_TYPE_VARINT_OR_ZIGZAG) + sizeOfVarInt32(enumValue.protoOrdinal());
}

/**
* Get number of bytes that would be needed to encode a string field
*
* @param field descriptor of field
* @param value string value to get encoded size for
* @return the number of bytes for encoded value
*/
public static int sizeOfString(FieldDefinition field, String value) {
return sizeOfString(field, value, true);
}

/**
* Get number of bytes that would be needed to encode a string field
*
Expand Down Expand Up @@ -1218,6 +1335,17 @@ private static int sizeOfStringNoTag(String value) {
}
}

/**
* Get number of bytes that would be needed to encode a bytes field
*
* @param field descriptor of field
* @param value bytes value to get encoded size for
* @return the number of bytes for encoded value
*/
public static int sizeOfBytes(FieldDefinition field, RandomAccessData value) {
return sizeOfBytes(field, value, true);
}

/**
* Get number of bytes that would be needed to encode a bytes field
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ void testReadNextFieldNumber() throws IOException {
void testSkipField() throws IOException {
final String valToRead = randomVarSizeString();
final BufferedData data = BufferedData.allocate(1000);
writeLong(data, createFieldDefinition(FIXED64), rng.nextLong(), true);
writeInteger(data, createFieldDefinition(FIXED32), rng.nextInt(), true);
writeLong(data, createFieldDefinition(FIXED64), rng.nextLong());
writeInteger(data, createFieldDefinition(FIXED32), rng.nextInt());
int value = rng.nextInt(0, Integer.MAX_VALUE);
writeInteger(data, createFieldDefinition(INT32), value, true);
writeString(data, createFieldDefinition(STRING), randomVarSizeString(), true);
writeString(data, createFieldDefinition(STRING), valToRead, true);
writeInteger(data, createFieldDefinition(INT32), value);
writeString(data, createFieldDefinition(STRING), randomVarSizeString());
writeString(data, createFieldDefinition(STRING), valToRead);

data.flip();

Expand Down
Loading

0 comments on commit a0fc753

Please sign in to comment.