use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.
the class ProtobufSchemaParser method parseSchema.
/**
* @see io.apicurio.registry.serde.SchemaParser#parseSchema(byte[])
*/
@Override
public ProtobufSchema parseSchema(byte[] rawSchema) {
try {
// textual .proto file
ProtoFileElement fileElem = ProtoParser.Companion.parse(FileDescriptorUtils.DEFAULT_LOCATION, IoUtil.toString(rawSchema));
FileDescriptor fileDescriptor = FileDescriptorUtils.protoFileToFileDescriptor(fileElem);
return new ProtobufSchema(fileDescriptor, fileElem);
} catch (DescriptorValidationException pe) {
throw new SerializationException("Error parsing protobuf schema ", pe);
}
}
use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.
the class ProtobufSchemaParser method getSchemaFromData.
/**
* @see io.apicurio.registry.resolver.SchemaParser#getSchemaFromData(java.lang.Object)
*/
@Override
public ParsedSchema<ProtobufSchema> getSchemaFromData(Record<U> data) {
FileDescriptor schemaFileDescriptor = data.payload().getDescriptorForType().getFile();
ProtoFileElement protoFileElement = toProtoFileElement(schemaFileDescriptor);
ProtobufSchema protobufSchema = new ProtobufSchema(schemaFileDescriptor, protoFileElement);
byte[] rawSchema = IoUtil.toBytes(protoFileElement.toSchema());
return new ParsedSchemaImpl<ProtobufSchema>().setParsedSchema(protobufSchema).setReferenceName(protobufSchema.getFileDescriptor().getName()).setSchemaReferences(handleDependencies(schemaFileDescriptor)).setRawSchema(rawSchema);
}
use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.
the class ProtobufSchemaParser method parseSchema.
/**
* @see io.apicurio.registry.serde.SchemaParser#parseSchema(byte[])
*/
@Override
public ProtobufSchema parseSchema(byte[] rawSchema, Map<String, ParsedSchema<ProtobufSchema>> resolvedReferences) {
try {
// textual .proto file
ProtoFileElement fileElem = ProtoParser.Companion.parse(FileDescriptorUtils.DEFAULT_LOCATION, IoUtil.toString(rawSchema));
Map<String, ProtoFileElement> dependencies = new HashMap<>();
resolvedReferences.forEach((key, value) -> {
dependencies.put(key, value.getParsedSchema().getProtoFileElement());
if (value.hasReferences()) {
addReferencesToDependencies(value.getSchemaReferences(), dependencies);
}
});
MessageElement firstMessage = FileDescriptorUtils.firstMessage(fileElem);
if (firstMessage != null) {
try {
final Descriptors.Descriptor fileDescriptor = FileDescriptorUtils.toDescriptor(firstMessage.getName(), fileElem, dependencies);
return new ProtobufSchema(fileDescriptor.getFile(), fileElem);
} catch (IllegalStateException ise) {
// If we fail to init the dynamic schema, try to get the descriptor from the proto element
return getFileDescriptorFromElement(fileElem);
}
} else {
return getFileDescriptorFromElement(fileElem);
}
} catch (DescriptorValidationException pe) {
throw new SerializationException("Error parsing protobuf schema ", pe);
}
}
use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.
the class ProtobufSchemaParser method handleDependencies.
private List<ParsedSchema<ProtobufSchema>> handleDependencies(FileDescriptor fileDescriptor) {
List<ParsedSchema<ProtobufSchema>> schemaReferences = new ArrayList<>();
fileDescriptor.getDependencies().forEach(referenceFileDescriptor -> {
ProtoFileElement referenceProtoFileElement = toProtoFileElement(referenceFileDescriptor);
ProtobufSchema referenceProtobufSchema = new ProtobufSchema(referenceFileDescriptor, referenceProtoFileElement);
byte[] rawSchema = IoUtil.toBytes(referenceProtoFileElement.toSchema());
ParsedSchema<ProtobufSchema> referencedSchema = new ParsedSchemaImpl<ProtobufSchema>().setParsedSchema(referenceProtobufSchema).setReferenceName(referenceProtobufSchema.getFileDescriptor().getName()).setSchemaReferences(handleDependencies(referenceFileDescriptor)).setRawSchema(rawSchema);
schemaReferences.add(referencedSchema);
});
return schemaReferences;
}
Aggregations