use of org.infinispan.protostream.descriptors.FieldDescriptor in project infinispan by infinispan.
the class IndexingTagHandler method indexMissingFields.
/**
* All fields that were not seen until the end of this message are missing and will be indexed with their default
* value or null if none was declared. The null value is replaced with a special null token placeholder because
* Lucene cannot index nulls.
*/
private void indexMissingFields() {
if (messageContext.getDocument() == null) {
return;
}
for (FieldDescriptor fieldDescriptor : messageContext.getMessageDescriptor().getFields()) {
if (!messageContext.isFieldMarked(fieldDescriptor.getNumber())) {
Object defaultValue = fieldDescriptor.hasDefaultValue() ? fieldDescriptor.getDefaultValue() : null;
addFieldToDocument(fieldDescriptor, defaultValue);
}
}
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class ProtobufParser method parseMessage.
private void parseMessage(TagHandler tagHandler, Descriptor messageDescriptor, TagReader in) throws IOException {
int tag;
while ((tag = in.readTag()) != 0) {
final int fieldNumber = WireType.getTagFieldNumber(tag);
final WireType wireType = WireType.fromTag(tag);
final FieldDescriptor fd = messageDescriptor != null ? messageDescriptor.findFieldByNumber(fieldNumber) : null;
switch(wireType) {
case LENGTH_DELIMITED:
{
if (fd == null) {
byte[] value = in.readByteArray();
tagHandler.onTag(fieldNumber, null, value);
} else if (fd.getType() == Type.STRING) {
String value = in.readString();
tagHandler.onTag(fieldNumber, fd, value);
} else if (fd.getType() == Type.BYTES) {
byte[] value = in.readByteArray();
tagHandler.onTag(fieldNumber, fd, value);
} else if (fd.getType() == Type.MESSAGE) {
int length = in.readUInt32();
int oldLimit = in.pushLimit(length);
tagHandler.onStartNested(fieldNumber, fd);
parseMessage(tagHandler, fd.getMessageType(), in);
tagHandler.onEndNested(fieldNumber, fd);
in.checkLastTagWas(0);
in.popLimit(oldLimit);
}
break;
}
case START_GROUP:
{
if (fd != null) {
tagHandler.onStartNested(fieldNumber, null);
parseMessage(tagHandler, null, in);
in.checkLastTagWas(WireType.makeTag(fieldNumber, WireType.WIRETYPE_END_GROUP));
tagHandler.onEndNested(fieldNumber, null);
} else {
tagHandler.onStartNested(fieldNumber, fd);
parseMessage(tagHandler, fd.getMessageType(), in);
in.checkLastTagWas(WireType.makeTag(fieldNumber, WireType.WIRETYPE_END_GROUP));
tagHandler.onEndNested(fieldNumber, fd);
}
break;
}
case FIXED32:
case FIXED64:
case VARINT:
{
if (fd == null) {
if (wireType == WireType.FIXED32) {
tagHandler.onTag(fieldNumber, null, in.readFixed32());
} else if (wireType == WireType.FIXED64) {
tagHandler.onTag(fieldNumber, null, in.readFixed64());
} else {
tagHandler.onTag(fieldNumber, null, in.readUInt64());
}
} else {
Object value;
switch(fd.getType()) {
case DOUBLE:
value = in.readDouble();
break;
case FLOAT:
value = in.readFloat();
break;
case BOOL:
value = in.readBool();
break;
case INT32:
value = in.readInt32();
break;
case SFIXED32:
value = in.readSFixed32();
break;
case FIXED32:
value = in.readFixed32();
break;
case UINT32:
value = in.readUInt32();
break;
case SINT32:
value = in.readSInt32();
break;
case INT64:
value = in.readInt64();
break;
case UINT64:
value = in.readUInt64();
break;
case FIXED64:
value = in.readFixed64();
break;
case SFIXED64:
value = in.readSFixed64();
break;
case SINT64:
value = in.readSInt64();
break;
case ENUM:
value = in.readEnum();
break;
default:
throw new IOException("Unexpected field type " + fd.getType() + " for field " + fieldNumber);
}
tagHandler.onTag(fieldNumber, fd, value);
}
break;
}
default:
throw new IOException("Found tag with invalid wire type : tag=" + tag + ", wireType=" + wireType);
}
}
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class DescriptorsTest method testDocComment.
@Test
public void testDocComment() {
String file1 = "package test1;\n" + "/** \n" + " * some doc text \n" + " * some more doc text \n" + " **/\n\n" + "message X {\n" + " /**\n" + " * field doc text \n\n" + " */\n" + " optional int32 field1 = 1;\n" + "}\n";
FileDescriptorSource fileDescriptorSource = new FileDescriptorSource();
fileDescriptorSource.addProtoFile("file1.proto", file1);
Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource);
assertEquals(1, descriptors.size());
assertTrue(descriptors.containsKey("file1.proto"));
Map<String, GenericDescriptor> types = descriptors.get("file1.proto").getTypes();
Descriptor typeX = (Descriptor) types.get("test1.X");
assertNotNull(typeX);
assertEquals(1, typeX.getFields().size());
FieldDescriptor field1 = typeX.getFields().get(0);
assertEquals("some doc text \n some more doc text", typeX.getDocumentation());
assertEquals("field doc text", field1.getDocumentation());
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class DescriptorsTest method assertSearchRequestFields.
private void assertSearchRequestFields(List<FieldDescriptor> fields) {
FieldDescriptor queryField = fields.get(0);
assertThat(queryField.getLabel()).isEqualTo(Label.REQUIRED);
assertThat(queryField.getType()).isEqualTo(Type.STRING);
assertThat(queryField.getJavaType()).isEqualTo(JavaType.STRING);
assertThat(queryField.getFullName()).isEqualTo("org.infinispan.protostream.test.SearchRequest.query");
assertThat(queryField.getName()).isEqualTo("query");
assertThat(queryField.getNumber()).isEqualTo(1);
assertThat(queryField.getMessageType()).isNull();
assertThat(queryField.getContainingMessage().getName()).isEqualTo("SearchRequest");
FieldDescriptor pageNumberField = fields.get(1);
assertThat(pageNumberField.getLabel()).isEqualTo(Label.OPTIONAL);
assertThat(pageNumberField.getType()).isEqualTo(Type.INT32);
assertThat(pageNumberField.getJavaType()).isEqualTo(JavaType.INT);
assertThat(pageNumberField.getName()).isEqualTo("page_number");
assertThat(pageNumberField.getNumber()).isEqualTo(2);
assertThat(pageNumberField.hasDefaultValue()).isTrue();
assertThat(pageNumberField.getDefaultValue()).isEqualTo(10);
assertThat(pageNumberField.getMessageType()).isNull();
FieldDescriptor flagField = fields.get(3);
assertThat(flagField.getLabel()).isEqualTo(Label.REPEATED);
assertThat(flagField.getType()).isEqualTo(Type.INT32);
assertThat(flagField.getJavaType()).isEqualTo(JavaType.INT);
assertThat(flagField.getOptionByName("packed")).isEqualTo("true");
FieldDescriptor dntField = fields.get(4);
assertThat(dntField.getLabel()).isEqualTo(Label.OPTIONAL);
assertThat(dntField.getType()).isEqualTo(Type.ENUM);
assertThat(dntField.getEnumType().findValueByName("DONT_CARE").getNumber()).isEqualTo(2);
assertThat(dntField.getEnumType().findValueByNumber(1).getName()).isEqualTo("TRACK_FOR_SURE");
assertThat(dntField.getEnumType().getFileDescriptor()).isNotNull();
assertThat(dntField.getFileDescriptor()).isNotNull();
assertThat(dntField.getJavaType()).isEqualTo(JavaType.ENUM);
FieldDescriptor reqEnumField = fields.get(5);
assertThat(reqEnumField.getLabel()).isEqualTo(Label.REQUIRED);
assertThat(reqEnumField.getType()).isEqualTo(Type.ENUM);
assertThat(reqEnumField.getJavaType()).isEqualTo(JavaType.ENUM);
assertThat(reqEnumField.hasDefaultValue()).isFalse();
assertThat(reqEnumField.getEnumType()).isNotNull();
assertThat(reqEnumField.getEnumType().findValueByNumber(0).getFileDescriptor()).isNotNull();
assertThat(reqEnumField.getEnumType().findValueByNumber(0).getContainingEnum()).isEqualTo(reqEnumField.getEnumType());
assertThat(reqEnumField.getOptionByName("deprecated")).isEqualTo("true");
FieldDescriptor labelField = fields.get(6);
assertThat(labelField.hasDefaultValue()).isTrue();
assertThat(labelField.getDefaultValue()).isEqualTo("whatever");
FieldDescriptor typedField = fields.get(7);
Descriptor messageType = typedField.getMessageType();
assertThat(messageType.getFullName()).isEqualTo("org.infinispan.protostream.test.SearchRequest.Inner");
assertThat(messageType.getContainingType().getName()).isEqualTo("SearchRequest");
assertThat(typedField.getType()).isEqualTo(Type.MESSAGE);
assertThat(typedField.getFullName()).isEqualTo("org.infinispan.protostream.test.SearchRequest.typed");
}
use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.
the class DescriptorsTest method assertExtensions.
private void assertExtensions(List<ExtendDescriptor> extensions) {
assertThat(extensions).hasSize(1);
ExtendDescriptor resultExtension = extensions.get(0);
assertThat(resultExtension.getExtendedMessage().getName()).isEqualTo("MoreInner");
assertThat(resultExtension.getFileDescriptor()).isNotNull();
FieldDescriptor barField = resultExtension.getFields().get(0);
assertThat(barField.getLabel()).isEqualTo(Label.OPTIONAL);
assertThat(barField.getJavaType()).isEqualTo(JavaType.FLOAT);
assertThat(barField.getNumber()).isEqualTo(101);
assertThat(barField.isPacked()).isTrue();
}
Aggregations