Skip to content

Commit 18c59c9

Browse files
try finding module from loaded modules (#120)
* try finding module from loaded modules * use for-loop instead of findfirst * replace isnothing for julia 1.0 compat * add test * bump version
1 parent f11c936 commit 18c59c9

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BSON"
22
uuid = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
3-
version = "0.3.6"
3+
version = "0.3.7"
44

55
[compat]
66
julia = "0.7, 1"

src/extensions.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,22 @@ tags[:svec] = d -> Core.svec(d[:data]...)
1818

1919
ref(path::Symbol...) = BSONDict(:tag => "ref", :path => Base.string.([path...]))
2020

21-
resolve(fs, init) = reduce((m, f) -> getfield(m, Symbol(f)), fs; init = init)
21+
function _find_module(x)
22+
for (k, v) in Base.loaded_modules
23+
k.name == x && return v
24+
end
25+
return nothing
26+
end
27+
28+
function resolve(fs, init)
29+
ff = first(fs)
30+
m = _find_module(ff)
31+
if m !== nothing
32+
init = m
33+
fs = @view fs[2:end]
34+
end
35+
return reduce((m, f) -> getfield(m, Symbol(f)), fs; init = init)
36+
end
2237

2338
tags[:ref] = (d, init) -> resolve(d[:path], init)
2439

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ end
3131
module A
3232
using DataFrames, BSON
3333
d = DataFrame(a = 1:10, b = rand(10))
34+
a = DataFrames.PooledArrays.PooledArray(["a" "b"; "c" "d"])
3435
bson("test_25_dataframe.bson", Dict(:d=>d))
36+
bson("test_26_module_in_module.bson", Dict(:a=>a))
3537
end
3638

3739
struct NoInit
@@ -156,4 +158,9 @@ end
156158
rm("test_25_dataframe.bson")
157159
end
158160

161+
@testset "Module with module import" begin
162+
@test BSON.load(joinpath(@__DIR__, "test_26_module_in_module.bson"))[:a] == A.a
163+
rm("test_26_module_in_module.bson")
164+
end
165+
159166
end

0 commit comments

Comments
 (0)