Search in sources :

Example 31 with FieldDescriptor

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

the class ProtostreamProtobufAdapterTypeProvider method buildMessage.

private DescriptorProto buildMessage(Descriptor descriptor) {
    DescriptorProto.Builder messageBuilder = DescriptorProto.newBuilder();
    messageBuilder.setName(descriptor.getName());
    for (FieldDescriptor fieldDescriptor : descriptor.getFields()) {
        messageBuilder.addField(buildFieldDescriptor(fieldDescriptor));
    }
    return messageBuilder.build();
}
Also used : FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 32 with FieldDescriptor

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

the class ProtobufPropertyHelper method hasProperty.

@Override
public boolean hasProperty(Descriptor entityType, String[] propertyPath) {
    Descriptor messageDescriptor = entityType;
    int i = 0;
    for (String p : propertyPath) {
        i++;
        FieldDescriptor field = messageDescriptor.findFieldByName(p);
        if (field == null) {
            return false;
        }
        if (field.getJavaType() == JavaType.MESSAGE) {
            messageDescriptor = field.getMessageType();
        } else {
            break;
        }
    }
    return i == propertyPath.length;
}
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)

Example 33 with FieldDescriptor

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

the class ProtobufUtilTest method testWrappedMessageTypeIdMapper.

@Test
public void testWrappedMessageTypeIdMapper() throws Exception {
    WrappedMessageTypeIdMapper mapper = new WrappedMessageTypeIdMapper() {

        @Override
        public int mapTypeIdOut(int typeId, ImmutableSerializationContext ctx) {
            if (typeId == 100042) {
                // change typeId ouf User
                return 100021;
            }
            return typeId;
        }
    };
    Configuration cfg = Configuration.builder().wrappingConfig().wrappedMessageTypeIdMapper(mapper).build();
    ImmutableSerializationContext ctx = createContext(cfg);
    // this has TypeId 100042
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    byte[] bytes = ProtobufUtil.toWrappedByteArray(ctx, user);
    int[] seenTypeId = new int[] { -1 };
    TagHandler tagHandler = new TagHandler() {

        @Override
        public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
            if (fieldNumber == WrappedMessage.WRAPPED_TYPE_ID) {
                seenTypeId[0] = (Integer) tagValue;
            }
        }
    };
    Descriptor wrappedMessageDescriptor = ctx.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME);
    ProtobufParser.INSTANCE.parse(tagHandler, wrappedMessageDescriptor, bytes);
    assertEquals(100021, seenTypeId[0]);
}
Also used : User(org.infinispan.protostream.domain.User) Configuration(org.infinispan.protostream.config.Configuration) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 34 with FieldDescriptor

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

the class ProtoStreamReaderImpl method readPrimitive.

private Object readPrimitive(String fieldName, JavaType javaType) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    final Type type = fd.getType();
    if (type == Type.ENUM || type == Type.GROUP || type == Type.MESSAGE) {
        throw new IllegalArgumentException("Declared field type is not a primitive : " + fd.getFullName());
    }
    if (fd.getJavaType() != javaType) {
        throw new IllegalArgumentException("Declared field type is not of the expected type : " + fd.getFullName());
    }
    checkFieldRead(fd, false);
    final int expectedTag = fd.getWireTag();
    Object o = messageContext.unknownFieldSet.consumeTag(expectedTag);
    if (o != null) {
        return convertWireTypeToJavaType(type, o);
    }
    TagReader in = messageContext.in;
    while (true) {
        int tag = in.readTag();
        if (tag == 0) {
            break;
        }
        if (tag == expectedTag) {
            switch(type) {
                case DOUBLE:
                    return in.readDouble();
                case FLOAT:
                    return in.readFloat();
                case BOOL:
                    return in.readBool();
                case STRING:
                    return in.readString();
                case BYTES:
                    return in.readByteArray();
                case INT32:
                    return in.readInt32();
                case SFIXED32:
                    return in.readSFixed32();
                case FIXED32:
                    return in.readFixed32();
                case UINT32:
                    return in.readUInt32();
                case SINT32:
                    return in.readSInt32();
                case INT64:
                    return in.readInt64();
                case UINT64:
                    return in.readUInt64();
                case FIXED64:
                    return in.readFixed64();
                case SFIXED64:
                    return in.readSFixed64();
                case SINT64:
                    return in.readSInt64();
                default:
                    throw new IOException("Unexpected field type : " + type);
            }
        }
        messageContext.unknownFieldSet.readSingleField(tag, in);
    }
    if (fd.hasDefaultValue()) {
        return fd.getDefaultValue();
    }
    if (fd.isRequired()) {
        throw new IOException("Field " + fd.getFullName() + " is required but is not present in the stream");
    }
    return null;
}
Also used : Type(org.infinispan.protostream.descriptors.Type) WireType(org.infinispan.protostream.descriptors.WireType) JavaType(org.infinispan.protostream.descriptors.JavaType) IOException(java.io.IOException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) TagReader(org.infinispan.protostream.TagReader)

Example 35 with FieldDescriptor

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

the class ProtoStreamWriterImpl method writeFloat.

@Override
public void writeFloat(String fieldName, float value) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    if (fd.getType() != Type.FLOAT) {
        throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
    }
    checkFieldWrite(fd);
    messageContext.out.writeFloat(fd.getNumber(), value);
}
Also used : 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