Skip to content

Support binary blobs - #9

Merged
gouttegd merged 6 commits into
mainfrom
support-binary-blob-2
Sep 19, 2026
Merged

gouttegd merged 6 commits into
mainfrom
support-binary-blob-2

Conversation

@gouttegd

Copy link
Copy Markdown
Owner

This PR supersedes #8 using a new approach.

First, it represents binary blobs using a dedicated BinaryBlob class, instead of a raw array of bytes (byte[]). This allows for easier support of the expected logic of equals() and hashCode().

Second, it implements a new system for finding the appropriate IConverter to use for a given slot, which can replace the @Converter annotation (which was notably used to deal with uriorcurie-typed slots).

gouttegd and others added 5 commits September 19, 2026 09:50
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.
@gouttegd
gouttegd merged commit e390c4c into main Sep 19, 2026
2 checks passed
@gouttegd
gouttegd deleted the support-binary-blob-2 branch September 19, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant