Skip to content

Advanced typemap and full user control#650

Merged
JonasIsensee merged 4 commits intomasterfrom
bettertypemapping
Jun 26, 2025
Merged

Advanced typemap and full user control#650
JonasIsensee merged 4 commits intomasterfrom
bettertypemapping

Conversation

@JonasIsensee
Copy link
Copy Markdown
Collaborator

@JonasIsensee JonasIsensee commented Jun 24, 2025

This PR gives the user full control over type reconstruction.

Simply pass a function (f::JLDFile, typepath::String, params::Vector) -> type as keyword to load or jldopen.
This function is then called for every single type that will be decoded.
The default implementation is JLD2.default_typemap and all standard cases can simply be forwarded to it.

Example:
Here, for bogus reasons we want to load a Complex{Int} as a Complex{Float64} while also modifying the real part during loading.
if typepath == "Base.Complex" && params == [Int] checks that only Complex{Int} get changed and
wrapping the updated type in JLD2.Upgrade ensures that our custom implementation of JLD2.rconvert gets called during loading.

    fn = "advancedtypemap.jld2"
    z = 1 + 2im # Complex{Int}
    zf = 1f0 + 2f0*im # Complex{Float32}
    jldsave(fn; z, type = Complex{Int}, zf)

    typemap = function(f, typepath, params)
        @info "typepath: $typepath, params: $params"
        if typepath == "Base.Complex" && params == [Int]
            return JLD2.Upgrade(Complex{Float64})
        end
        JLD2.default_typemap(f, typepath, params)
    end

    JLD2.rconvert(::Type{Complex{Float64}}, nt::NamedTuple) =
        Complex{Float64}(nt.re + 1, nt.im)

    d = load(fn; typemap)
    @test d["z"] isa Complex{Float64}
    @test d["z"] == 2.0 + 2.0im
    @test d["type"] == Complex{Float64}
    @test d["zf"] isa Complex{Float32}
    @test d["zf"] == 1.0 + 2.0im

closes #630

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 24, 2025

Codecov Report

Attention: Patch coverage is 98.18182% with 1 line in your changes missing coverage. Please review.

Project coverage is 85.33%. Comparing base (0aae9f4) to head (21f0705).
Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/data/reconstructing_datatypes.jl 97.95% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #650      +/-   ##
==========================================
- Coverage   85.36%   85.33%   -0.04%     
==========================================
  Files          37       37              
  Lines        4407     4404       -3     
==========================================
- Hits         3762     3758       -4     
- Misses        645      646       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JonasIsensee JonasIsensee merged commit 760b565 into master Jun 26, 2025
15 checks passed
@JonasIsensee JonasIsensee deleted the bettertypemapping branch June 26, 2025 07:18
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.

Typemap yields Upgrade(type) objects on load

1 participant