Apollo3zehn / PureHDF

A pure .NET library that makes reading and writing of HDF5 files (groups, datasets, attributes, ...) very easy.
MIT License
47 stars 16 forks source link

Nullable Property in compound type leads to exception #89

Closed Blackclaws closed 1 month ago

Blackclaws commented 1 month ago

When using nullable value types for Compound types an exception gets thrown:

public record Compound(double A, uint? Channel);
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.Exception: The compound data type needs at least one member

Expected result was that it works. But it might not be able to work with null, the exception is not helpful though.

Apollo3zehn commented 1 month ago

Probably I forgot the create a unit test for this case. Nullable value types are special because they become reference types. I`ll check that today evening.

Apollo3zehn commented 1 month ago

I won't make it today but on Monday I`ll find the time for it :-)

Apollo3zehn commented 1 month ago

v1.0.0-beta.19 adds write support for nullable value types (released in a few minutes). I will work on read support the next days.

Since the value can now be null, it will be encoded as variable length sequence (H5T_VLEN) data with a single element. This is the closest match to a nullable value type in the HDF5 spec.

Here you can see how public record Compound(double A, uint? Channel); is being encoded to the H5 file (and then dumped via h5dump):

image

In case you have no record encapsulating the uint? Channel field but the bare uint? variable, you would need to write such kind of data using the generic H5Attribute<T> like so:

uint? data = null;
file.Attributes["nullable"] = new H5Attribute<uint?>(data);

If you do not use the generic attribute you will get a compiler warning. It is necessary to use the generic version in combination with null data because null looses all runtime type information and then PureHDF does not know how to encode it. The generic H5Attribute<T> keeps that type information.