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();
}
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;
}
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]);
}
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;
}
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);
}
Aggregations