Support binary blobs - #9
Merged
Merged
Conversation
Looking up object converters by the Java class representing the object to convert should always be fine, because any LinkML class should only ever be represented by a single Java class (I cannot imagine a scenario where that would not be the case). However, looking up _type_ converters is another matter, because it may very well happen that different LinkML types are rendered by the same Java type. That is in fact already the case for example with the `linkml:String` and `linkml:Uriorcurie` types, which are both rendered as `java.lang.String`. So far, we have been dealing with that scenario by annotating Uriorcurie-typed slot with a `Converter` annotation allowing to directly specify the converter to use (so that those slots would be converted by the appropriate CurieConverter, instead of the StringConverter). But a more generic solution is needed. This commit implements such a solution. It (1) defines a new `TypeURI` annotation that can be used on a field to explicitly indicate the LinkML type of the slot (identified by its URI); (2) add a `getURI` method to the `IConverter` interface, by which a converter can explicitly indicate the LinkML type it is intended to convert; (3) amend the `ConverterContext` class to allow looking up slot converters by the URI of the slot (if available, i.e. if the slot has been annotated with the new `TypeURI` annotation) before looking up by Java type.
Now that we have a TypeURI annotation, the Java generator script needs to ensure that this annotation is added to all fields that require it. The way we do this is by introducing a concept of "custom type map", which is found in the same directory as the code templates. The map is a simple file where each line is of the form: <type uri> <java type> where <type uri> is the URI of a LinkML type and <java type> is the Java type to use to represent the LinkML type in Java code. This map serves two related purposes: (i) it allows to override the default type mappings set forth in LinkML-Py's code generator, and (ii) when a field has a type set by this map, this indicates the field should be annotated with a TypeURI annotation.
Add a `BinaryBlob` class to represent arbitrary binary blobs of data. The class is merely a thin wrapper around an array of bytes, providing support for encoding/decoding to/from Base64 and Base16 encodings and ensuring that binary blobs can be compared by values rather than by references. Add two converters to support (de)serialising BinaryBlob objects encoded as Base16- or Base64- encoded strings. The converters are automatically used whenever a slot has a range set to a type whose type URI is either `xsd:base64Binary` or `xsd:hexBinary` (provided the slot is correctly annotated with the type URI).
Update the custom type map to map xsd:base64Binary and xsd:hexBinary to the newly added BinaryBlob type. Also simplify the templates to import everything under `org.incenp.linkml.core.annotations.*`, instead of importing every annotation separately.
Repurpose the newly introduced TypeURI annotation so that it can also be used on a class, to mark that class as representing a custom LinkML type. This is mostly to avoid inadvertently creating a ClassInfo object for a class that does not in fact represent a LinkML class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR supersedes #8 using a new approach.
First, it represents binary blobs using a dedicated
BinaryBlobclass, instead of a raw array of bytes (byte[]). This allows for easier support of the expected logic ofequals()andhashCode().Second, it implements a new system for finding the appropriate
IConverterto use for a given slot, which can replace the@Converterannotation (which was notably used to deal withuriorcurie-typed slots).