Search in sources :

Example 1 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project kogito-apps by kiegroup.

the class ProtoIndexParser method create.

@Override
public EntityIndexDescriptor create(Descriptor annotatedDescriptor, AnnotationElement.Annotation annotation) {
    String name = annotatedDescriptor.getFullName();
    List<IndexDescriptor> indexes = new ArrayList<>(annotatedDescriptor.getFields().size());
    List<AttributeDescriptor> fields = new ArrayList<>(annotatedDescriptor.getFields().size());
    for (FieldDescriptor fd : annotatedDescriptor.getFields()) {
        AnnotationElement.Annotation fieldAnnotation = fd.getAnnotations().get(FIELD_ANNOTATION);
        if (fieldAnnotation != null) {
            String fieldName = Optional.ofNullable((String) fieldAnnotation.getAttributeValue(FIELD_NAME_ATTRIBUTE).getValue()).filter(a -> !a.isEmpty()).orElseGet(fd::getName);
            boolean isIndexed = INDEX_YES.equals(fieldAnnotation.getAttributeValue(FIELD_INDEX_ATTRIBUTE).getValue());
            fields.add(createAttributeDescriptor(fd, fieldName));
            if (isIndexed) {
                indexes.add(new IndexDescriptor(fieldName, List.of(fieldName)));
            }
        }
    }
    return new EntityIndexDescriptor(name, indexes, fields);
}
Also used : FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) AnnotationMetadataCreator(org.infinispan.protostream.AnnotationMetadataCreator) Set(java.util.Set) Descriptor(org.infinispan.protostream.descriptors.Descriptor) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Map(java.util.Map) Optional(java.util.Optional) Configuration(org.infinispan.protostream.config.Configuration) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) Collectors.toSet(java.util.stream.Collectors.toSet) ArrayList(java.util.ArrayList) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 2 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project kogito-apps by kiegroup.

the class ProtoIndexParserTest method testCreateAttributeDescriptor.

@Test
void testCreateAttributeDescriptor() {
    FieldDescriptor roomField = createFileDescriptor().getMessageTypes().stream().filter(descriptor -> "org.acme.travels.travels.Hotel".equals(descriptor.getFullName())).findAny().get().findFieldByName("room");
    AttributeDescriptor attributeDescriptor = createAttributeDescriptor(roomField, null);
    assertEquals(new AttributeDescriptor("room", "string", true), attributeDescriptor);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) ProtoIndexParser.createAttributeDescriptor(org.kie.kogito.persistence.protobuf.ProtoIndexParser.createAttributeDescriptor) ProtoIndexParser.createEntityIndexDescriptors(org.kie.kogito.persistence.protobuf.ProtoIndexParser.createEntityIndexDescriptors) TestUtils.getTestFileContent(org.kie.kogito.persistence.protobuf.TestUtils.getTestFileContent) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) Function(java.util.function.Function) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Collectors.toMap(java.util.stream.Collectors.toMap) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) TestUtils.getValidEntityIndexDescriptors(org.kie.kogito.persistence.protobuf.TestUtils.getValidEntityIndexDescriptors) INDEXED_ANNOTATION(org.kie.kogito.persistence.protobuf.ProtoIndexParser.INDEXED_ANNOTATION) Map(java.util.Map) ProtoIndexParser.configureBuilder(org.kie.kogito.persistence.protobuf.ProtoIndexParser.configureBuilder) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SerializationContext(org.infinispan.protostream.SerializationContext) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) ProtoIndexParser.createAttributeDescriptor(org.kie.kogito.persistence.protobuf.ProtoIndexParser.createAttributeDescriptor) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.jupiter.api.Test)

Example 3 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project infinispan by infinispan.

the class ProtobufMatcherEvalContext method processMissingFields.

private void processMissingFields() {
    for (FieldDescriptor fd : messageContext.getMessageDescriptor().getFields()) {
        AttributeNode<FieldDescriptor, Integer> attributeNode = currentNode.getChild(fd.getNumber());
        boolean fieldSeen = messageContext.isFieldMarked(fd.getNumber());
        if (attributeNode != null && (fd.isRepeated() || !fieldSeen)) {
            if (fd.isRepeated()) {
                // Repeated fields can't have default values but we need to at least take care of IS [NOT] NULL predicates
                if (fieldSeen) {
                    // Here we use a dummy value since it would not matter anyway for IS [NOT] NULL
                    attributeNode.processValue(AttributeNode.DUMMY_VALUE, this);
                } else {
                    processNullAttribute(attributeNode);
                }
            } else {
                if (fd.getJavaType() == JavaType.MESSAGE) {
                    processNullAttribute(attributeNode);
                } else {
                    Object defaultValue = fd.hasDefaultValue() ? fd.getDefaultValue() : null;
                    attributeNode.processValue(defaultValue, this);
                }
            }
        }
    }
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 4 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project infinispan by infinispan.

the class ProtobufPropertyHelper method mapPropertyNamePathToFieldIdPath.

@Override
public List<?> mapPropertyNamePathToFieldIdPath(Descriptor messageDescriptor, String[] propertyPath) {
    List<Integer> translatedPath = new ArrayList<>(propertyPath.length);
    Descriptor md = messageDescriptor;
    for (String prop : propertyPath) {
        FieldDescriptor fd = md.findFieldByName(prop);
        translatedPath.add(fd.getNumber());
        if (fd.getJavaType() == JavaType.MESSAGE) {
            md = fd.getMessageType();
        } else {
            // iteration is expected to stop here
            md = null;
        }
    }
    return translatedPath;
}
Also used : ArrayList(java.util.ArrayList) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 5 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project infinispan by infinispan.

the class ProtobufPropertyHelper method getField.

/**
 * @param entityType
 * @param propertyPath
 * @return the field descriptor or null if not found
 */
private FieldDescriptor getField(Descriptor entityType, String[] propertyPath) {
    Descriptor messageDescriptor = entityType;
    int i = 0;
    for (String p : propertyPath) {
        FieldDescriptor field = messageDescriptor.findFieldByName(p);
        if (field == null || ++i == propertyPath.length) {
            return field;
        }
        if (field.getJavaType() == JavaType.MESSAGE) {
            messageDescriptor = field.getMessageType();
        } else {
            break;
        }
    }
    return null;
}
Also used : EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) 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