Search in sources :

Example 1 with GenericDescriptor

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

the class AbstractSchemaJdbcStore method verifyParametersPresentForMessage.

void verifyParametersPresentForMessage(ImmutableSerializationContext ctx, String fullTypeName, Map<String, Parameter> parameterMap, boolean key) {
    GenericDescriptor genericDescriptor;
    try {
        genericDescriptor = ctx.getDescriptorByName(fullTypeName);
    } catch (IllegalArgumentException t) {
        throw log.schemaNotFound(fullTypeName);
    }
    Set<String> seenNames = new HashSet<>();
    if (genericDescriptor instanceof Descriptor) {
        recursiveUpdateParameters((Descriptor) genericDescriptor, parameterMap, null, seenNames, key);
    } else if (genericDescriptor instanceof EnumDescriptor) {
        if (!key && config.getSchemaJdbcConfiguration().embeddedKey()) {
            throw log.keyCannotEmbedWithEnum(fullTypeName);
        }
        String name = genericDescriptor.getName();
        // treat an enum as just a string
        Parameter enumParam = parameterMap.get(name.toUpperCase());
        if (enumParam != null) {
            assert enumParam.getType() == ProtostreamFieldType.STRING;
            updateUnwrap(enumParam, key, json -> json.at("_value"));
            enumParam.jsonUpdateConsumer = (json, o) -> {
                json.set("_type", fullTypeName);
                json.set("_value", o);
            };
        }
    } else {
        throw new UnsupportedOperationException("Unsupported descriptor found " + genericDescriptor);
    }
}
Also used : Type(org.infinispan.protostream.descriptors.Type) Arrays(java.util.Arrays) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) TableOperations(org.infinispan.persistence.jdbc.common.TableOperations) AbstractSchemaJdbcConfiguration(org.infinispan.persistence.sql.configuration.AbstractSchemaJdbcConfiguration) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) HashMap(java.util.HashMap) ComponentRegistry(org.infinispan.factories.ComponentRegistry) Descriptor(org.infinispan.protostream.descriptors.Descriptor) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) SQLException(java.sql.SQLException) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache) ResultSet(java.sql.ResultSet) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) BaseJdbcStore(org.infinispan.persistence.jdbc.common.impl.BaseJdbcStore) BaseTableOperations(org.infinispan.persistence.jdbc.common.sql.BaseTableOperations) Predicate(java.util.function.Predicate) CacheConfigurationException(org.infinispan.commons.CacheConfigurationException) Timestamp(java.sql.Timestamp) Set(java.util.Set) Json(org.infinispan.commons.dataconversion.internal.Json) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) SchemaJdbcConfiguration(org.infinispan.persistence.sql.configuration.SchemaJdbcConfiguration) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Objects(java.util.Objects) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Base64(java.util.Base64) List(java.util.List) ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) InitializationContext(org.infinispan.persistence.spi.InitializationContext) DataConversion(org.infinispan.encoding.DataConversion) Types(java.sql.Types) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) HashSet(java.util.HashSet)

Example 2 with GenericDescriptor

use of org.infinispan.protostream.descriptors.GenericDescriptor 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 3 with GenericDescriptor

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

the class CompileTimeProtoSchemaGenerator method importProtoTypeMetadata.

@Override
protected ProtoTypeMetadata importProtoTypeMetadata(XClass javaType) {
    if (javaType == typeFactory.fromClass(WrappedMessage.class)) {
        GenericDescriptor descriptor = serializationContext.getDescriptorByName(WrappedMessage.PROTOBUF_TYPE_NAME);
        BaseMarshaller<WrappedMessage> marshaller = serializationContext.getMarshaller(WrappedMessage.PROTOBUF_TYPE_NAME);
        return new ImportedProtoTypeMetadata(descriptor, marshaller, javaType);
    }
    String fileName = dependencies.get(javaType);
    if (fileName != null) {
        String packageName = serializationContext.getFileDescriptors().get(fileName).getPackage();
        return new CompileTimeImportedProtoTypeMetadata(makeTypeMetadata(javaType), packageName, fileName);
    }
    return null;
}
Also used : WrappedMessage(org.infinispan.protostream.WrappedMessage) ImportedProtoTypeMetadata(org.infinispan.protostream.annotations.impl.ImportedProtoTypeMetadata) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor)

Example 4 with GenericDescriptor

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

the class ProtobufParserTest method testTagHandler.

@Test
public void testTagHandler() throws Exception {
    ImmutableSerializationContext ctx = createContext();
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
    user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12), new Address("Bond Street", "W23", 2)));
    byte[] userBytes = ProtobufUtil.toWrappedByteArray(ctx, user);
    Descriptor wrapperDescriptor = ctx.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME);
    TagHandler messageHandler = new TagHandler() {

        @Override
        public void onStart(GenericDescriptor descriptor) {
            log.debugf("\tonStart %s", descriptor);
        }

        @Override
        public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
            log.debugf("\tonTag %d %s %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null, tagValue);
        }

        @Override
        public void onStartNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
            log.debugf("\tonStartNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
        }

        @Override
        public void onEndNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
            log.debugf("\tonEndNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
        }

        @Override
        public void onEnd() {
            log.debug("\tonEnd");
        }
    };
    TagHandler wrapperHandler = new TagHandler() {

        private Integer typeId;

        private String typeName;

        private byte[] wrappedMessage;

        private Integer wrappedEnum;

        private GenericDescriptor getDescriptor() {
            return typeId != null ? ctx.getDescriptorByTypeId(typeId) : ctx.getDescriptorByName(typeName);
        }

        @Override
        public void onStart(GenericDescriptor descriptor) {
            log.debugf("onStart %s", descriptor);
        }

        @Override
        public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
            log.debugf("onTag %d %s %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null, tagValue);
            if (fieldDescriptor == null) {
                // ignore unknown fields
                return;
            }
            switch(fieldNumber) {
                case WrappedMessage.WRAPPED_TYPE_ID:
                    typeId = (Integer) tagValue;
                    break;
                case WrappedMessage.WRAPPED_TYPE_NAME:
                    typeName = (String) tagValue;
                    break;
                case WrappedMessage.WRAPPED_MESSAGE:
                    wrappedMessage = (byte[]) tagValue;
                    break;
                case WrappedMessage.WRAPPED_ENUM:
                    wrappedEnum = (Integer) tagValue;
                    break;
                case WrappedMessage.WRAPPED_DOUBLE:
                case WrappedMessage.WRAPPED_FLOAT:
                case WrappedMessage.WRAPPED_INT64:
                case WrappedMessage.WRAPPED_UINT64:
                case WrappedMessage.WRAPPED_INT32:
                case WrappedMessage.WRAPPED_FIXED64:
                case WrappedMessage.WRAPPED_FIXED32:
                case WrappedMessage.WRAPPED_BOOL:
                case WrappedMessage.WRAPPED_STRING:
                case WrappedMessage.WRAPPED_BYTES:
                case WrappedMessage.WRAPPED_UINT32:
                case WrappedMessage.WRAPPED_SFIXED32:
                case WrappedMessage.WRAPPED_SFIXED64:
                case WrappedMessage.WRAPPED_SINT32:
                case WrappedMessage.WRAPPED_SINT64:
                    messageHandler.onStart(null);
                    messageHandler.onTag(fieldNumber, fieldDescriptor, tagValue);
                    messageHandler.onEnd();
                    break;
            }
        }

        @Override
        public void onStartNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
            log.debugf("onStartNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
        }

        @Override
        public void onEndNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
            log.debugf("onEndNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
        }

        @Override
        public void onEnd() {
            if (wrappedEnum != null) {
                EnumDescriptor enumDescriptor = (EnumDescriptor) getDescriptor();
                String enumConstantName = enumDescriptor.findValueByNumber(wrappedEnum).getName();
                FieldDescriptor fd = wrapperDescriptor.findFieldByNumber(WrappedMessage.WRAPPED_ENUM);
                messageHandler.onStart(enumDescriptor);
                messageHandler.onTag(WrappedMessage.WRAPPED_ENUM, fd, enumConstantName);
                messageHandler.onEnd();
            } else if (wrappedMessage != null) {
                try {
                    Descriptor messageDescriptor = (Descriptor) getDescriptor();
                    ProtobufParser.INSTANCE.parse(messageHandler, messageDescriptor, wrappedMessage);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            log.debug("onEnd");
        }
    };
    ProtobufParser.INSTANCE.parse(wrapperHandler, wrapperDescriptor, userBytes);
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) IOException(java.io.IOException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 5 with GenericDescriptor

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

the class SerializationContextImpl method unregisterFileDescriptorTypes.

@GuardedBy("descriptorLock")
private void unregisterFileDescriptorTypes(FileDescriptor fileDescriptor) {
    if (fileDescriptor.isResolved()) {
        for (GenericDescriptor d : fileDescriptor.getTypes().values()) {
            Integer typeId = d.getTypeId();
            if (typeId != null) {
                typeIds.remove(typeId);
            }
            if (d instanceof EnumDescriptor) {
                for (EnumValueDescriptor ev : ((EnumDescriptor) d).getValues()) {
                    enumValueDescriptors.remove(ev.getScopedName());
                }
            }
        }
        genericDescriptors.keySet().removeAll(fileDescriptor.getTypes().keySet());
        fileDescriptor.markUnresolved();
    }
    for (FileDescriptor fd : fileDescriptor.getDependants().values()) {
        unregisterFileDescriptorTypes(fd);
    }
}
Also used : GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) GuardedBy(net.jcip.annotations.GuardedBy)

Aggregations

GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)9 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)6 Descriptor (org.infinispan.protostream.descriptors.Descriptor)5 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)5 Test (org.junit.Test)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)2 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)2 File (java.io.File)1 URL (java.net.URL)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Types (java.sql.Types)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1