Skip to content

Commit

Permalink
module reference serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Isensee committed Dec 19, 2023
1 parent 037a9d3 commit 3e81a43
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/data/specialcased_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ odr(::Type{String}) = fieldodr(String, true)
objodr(x::String) = FixedLengthString{String}(jlsizeof(x))

struct NullTerminated end
struct SpacePadded end
struct SpacePadded end
struct AsciiString{TERM}
length::Int
end
Expand Down Expand Up @@ -86,7 +86,7 @@ function jltype(f::JLDFile, dt::BasicDatatype)
end

function jltype(f::JLDFile, dt::VariableLengthDatatype)
if dt == H5TYPE_VLEN_UTF8
if dt == H5TYPE_VLEN_UTF8
# this is the fully supported JLD2 string
return ReadRepresentation{String,Vlen{String}}()
elseif dt.bitfield1 & 0x1 == 0x1
Expand Down Expand Up @@ -280,4 +280,29 @@ function writeas(NT::Type{NTuple{N,T}}) where {N,T}
end

wconvert(::Type{Vector{T}}, x::NTuple{N,T}) where {N,T} = collect(x)
rconvert(::Type{NTuple{N,T}}, x::Vector{T}) where {N,T} = NTuple{N,T}(x)
rconvert(::Type{NTuple{N,T}}, x::Vector{T}) where {N,T} = NTuple{N,T}(x)

## Modules
# Modules are stored by name
writeas(::Type{Module}) = String
wconvert(::Type{String}, x::Module) = string(x)
function rconvert(::Type{Module}, x::String)
pkg = Symbol(x)
# Try to find the module
# Start with the method used to find compression libraries
m =_findmod(pkg)
isnothing(m) || return Base.loaded_modules[m]
@info "Encountered reference to module $x, but it is not currently loaded."
return try
topimport(pkg)

Check warning on line 297 in src/data/specialcased_types.jl

View check run for this annotation

Codecov / codecov/patch

src/data/specialcased_types.jl#L294-L297

Added lines #L294 - L297 were not covered by tests
Base.loaded_modules[_findmod(pkg)]
catch
@warn "Could not load module $x. Returning a dummy module"
Module(Symbol(x*"_dummy"))
end
end

function jlconvert(::ReadRepresentation{Module,CustomSerialization{String,Vlen{String}}},

Check warning on line 305 in src/data/specialcased_types.jl

View check run for this annotation

Codecov / codecov/patch

src/data/specialcased_types.jl#L300-L305

Added lines #L300 - L305 were not covered by tests
f::JLDFile, ptr::Ptr, header_offset::RelOffset)
rconvert(Module, jlconvert(ReadRepresentation{String, Vlen{String}}(), f, ptr, header_offset))
end

Check warning on line 308 in src/data/specialcased_types.jl

View check run for this annotation

Codecov / codecov/patch

src/data/specialcased_types.jl#L307-L308

Added lines #L307 - L308 were not covered by tests

0 comments on commit 3e81a43

Please sign in to comment.