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