Skip to content

Commit 7b18630

Browse files
authored
Merge pull request #629 from JuliaIO/ib/fix_union_upgrade
Fix unions for upgrades on mutable structs
2 parents b638d89 + fb669c3 commit 7b18630

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.5.11
2+
- Make `Upgrade` work for types that sit inside `Union{..}`
3+
14
## 0.5.10
25
- fix regression for `UInt32`
36
- **Deprecation**: Do not rely on JLD2 to load a compression library. This feature will be removed in a future release. Instead explicitly add `using` statements into your scripts.

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "JLD2"
22
uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
3-
version = "0.5.10"
3+
version = "0.5.11"
44

55
[deps]
66
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"

src/data/reconstructing_datatypes.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ function types_from_refs(f::JLDFile, ptr::Ptr)
333333
nulldt = CommittedDatatype(UNDEFINED_ADDRESS, 0)
334334
cdt = get(f.datatype_locations, ref, nulldt)
335335
res = cdt !== nulldt ? julia_repr(jltype(f, cdt)) : load_dataset(f, ref)
336+
res isa Upgrade && (res = res.target)
336337
unknown_params |= isunknowntype(res) || isreconstructed(res)
337338
res
338339
end for ref in refs]
@@ -353,8 +354,6 @@ function jlconvert(rr::MappedRepr{<:Type,DataTypeODR},
353354
params = map(params) do p
354355
if p isa Union{Int64,Int32}
355356
Int(p)
356-
elseif p isa Upgrade
357-
p.target
358357
else
359358
p
360359
end

test/loadsave.jl

+23
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ module DummyModule
726726
end
727727
JLD2.rconvert(::Type{CC}, nt::NamedTuple) = CC()
728728
JLD2.rconvert(::Type{BB}, nt::NamedTuple) = BB()
729+
mutable struct DD end
730+
JLD2.rconvert(::Type{DD}, nt::NamedTuple) = DD()
729731
end
730732
@testset "Upgrading a struct that was formerly singleton" begin
731733
cd(mktempdir()) do
@@ -736,6 +738,27 @@ end
736738
end
737739
end
738740

741+
@testset "Issue #628 Upgrading structs that contains unions" begin
742+
cd(mktempdir()) do
743+
@testset "immutable" begin
744+
a1 = Dict("a" => [nothing DummyModule.AA()])
745+
save("a.jld2", a1)
746+
a2 = load("a.jld2"; typemap = Dict("Main.DummyModule.AA" => JLD2.Upgrade(DummyModule.AA)))
747+
@test only(keys(a1)) == "a"
748+
@test keys(a1) == keys(a2)
749+
@test a1["a"] == a2["a"]
750+
end
751+
@testset "mutable" begin
752+
a1 = Dict("a" => [nothing DummyModule.DD()])
753+
save("a.jld2", a1)
754+
a2 = load("a.jld2"; typemap = Dict("Main.DummyModule.DD" => JLD2.Upgrade(DummyModule.DD)))
755+
@test only(keys(a1)) == "a"
756+
@test keys(a1) == keys(a2)
757+
@test typeof(a1["a"]) == typeof(a2["a"])
758+
end
759+
end
760+
end
761+
739762
@testset "Issue #571 Loading Vararg NTuples" begin
740763
tst = [("a",), ("a", "b")]
741764
cd(mktempdir()) do

0 commit comments

Comments
 (0)