JuliaIO / JLD2.jl

HDF5-compatible file format in pure Julia
Other
547 stars 85 forks source link

Unpack for JLD2File #454

Closed lukasgrunwald closed 1 year ago

lukasgrunwald commented 1 year ago

I would be nice to be able to use @pack! and @unpack from the UnPack.jl-package to quickly store and load values from JLD2File instances. This can be achieved by adding the methods

using UnPack
@inline UnPack.unpack(x::JLDFile{MmapIO}, ::Val{f}) where {f} =  x[string(f)]

@inline function UnPack.pack!(file::JLDFile{MmapIO}, ::Val{f}, val) where {f}
    file[string(f)] = val
end

After defining above extensions, these methods can be used as

using JLD2

x = rand(10)
path = "temp.jld2"
file = jldopen(path, "w")
@pack! file = x # Creates entry with name x
close(file)

file = jldopen("temp.jld2") # contains fields :x
@unpack x = file # Extracts x from the struct
close(file)

Maybe this could be added with UnPack.jl as on optional dependency; not sure how that works though? Happy to try and make a PR if you are open to doing this.

JonasIsensee commented 1 year ago

Hi @lukasgrunwald , this is a neat idea.

I would recommend making the type signatures flexible enough to also accept JLD2.Groups. You're welcome to give it a try.

Building a small extension like that isn't too hard. I'm not sure how much documentation there exists around it but here's a link to some code where I tested out extensions for a different project https://gitlab.gwdg.de/eDLS/InPartS.jl/-/commit/a8982109c774ebc54933aaa95e4f9dcda1cdd1d4