Search in sources :

Example 11 with FieldDescriptor

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);
        }
    }
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 12 with FieldDescriptor

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);
        }
    }
}
Also used : WireType(org.infinispan.protostream.descriptors.WireType) IOException(java.io.IOException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 13 with FieldDescriptor

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());
}
Also used : GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test)

Example 14 with FieldDescriptor

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");
}
Also used : Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 15 with FieldDescriptor

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();
}
Also used : ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)51 Descriptor (org.infinispan.protostream.descriptors.Descriptor)16 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)12 TagWriter (org.infinispan.protostream.TagWriter)9 IOException (java.io.IOException)7 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)7 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)6 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)4 Configuration (org.infinispan.protostream.config.Configuration)4 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)3 Set (java.util.Set)2 Function (java.util.function.Function)2