Skip to content

Commit a11129f

Browse files
committed
Fix deepmerge, add Dict conversion and empty support
1 parent da1f4bf commit a11129f

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/dictmerge.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ function deepmerge!(d::AbstractDict, others::AbstractDict...)
66
for (k, v) in other
77
if haskey(d, k)
88
v_curr = d[k]
9-
if isa(v_curr, AbstractDict)
10-
deepmerge!(v_curr, v)
11-
else
9+
if isnothing(v_curr) || isnothing(v)
1210
d[k] = v
11+
else
12+
if isa(v_curr, AbstractDict)
13+
deepmerge!(v_curr, v)
14+
else
15+
d[k] = v
16+
end
1317
end
1418
else
1519
d[k] = v
@@ -23,8 +27,8 @@ end
2327
function deepmerge(d::AbstractDict, others::AbstractDict...)
2428
K = Base.promoteK(keytype(d), others...)
2529
V = Base.promoteV(valtype(d), others...)
26-
result = Dict{K,V}(d)
27-
deepmerge!(result, others...)
30+
result = empty(d, K, V)
31+
deepmerge!(result, d, others...)
2832
end
2933

3034

src/propdict.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ _dict(p::PropDict) = getfield(p, :_internal_dict)
7070

7171
Base.parent(p::PropDict) = _dict(p)
7272

73+
Dict(p::PropDict) = _dict(p)
74+
Dict{Union{Symbol,Int},Any}(p::PropDict) = _dict(p)
75+
7376

7477
function Functors.functor(::Type{<:PropDict}, p)
7578
content, f_rec = Functors.functor(Dict, _dict(p))
@@ -165,6 +168,7 @@ end
165168
end
166169
end
167170

171+
Base.empty(::PropDict, ::Type{Union{Symbol,Int}}, ::Type{Any}) = PropDict()
168172

169173
Base.length(p::PropDict) = length(_dict(p))
170174

@@ -193,14 +197,9 @@ Base.iterate(p::PropDict) = iterate(_dict(p))
193197
Base.iterate(p::PropDict, i) = iterate(_dict(p), i)
194198

195199

196-
function Base.merge!(p::PropDict, others::PropDict...)
197-
PropDict(deepmerge!(_dict(p), map(_dict, (others))...))
198-
p
199-
end
200-
200+
Base.merge!(p::PropDict, others::PropDict...) = deepmerge!(p, others...)
201201

202-
Base.merge(p::PropDict, others::PropDict...) =
203-
PropDict(deepmerge(_dict(p), map(_dict, (others))...))
202+
Base.merge(p::PropDict, others::PropDict...) = deepmerge(p, others...)
204203

205204

206205
const integer_expr = r"^[+-]?[0-9]+$"

test/test_propdict.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using PropDicts
44
using Test
55

6+
using PropDicts: deepmerge, deepmerge!
7+
68
using Functors
79

810
@testset "propdict" begin
@@ -12,6 +14,14 @@ using Functors
1214
pa = PropDict(da)
1315
@test parent(pa) === PropDicts._dict(pa)
1416

17+
@test @inferred(Dict(pa)) === parent(pa)
18+
@test @inferred(Dict{Union{Symbol,Int},Any}(pa)) === parent(pa)
19+
@test @inferred(convert(Dict, pa)) === parent(pa)
20+
@test @inferred(convert(Dict{Union{Symbol,Int},Any}, pa)) === parent(pa)
21+
22+
@test @inferred(empty(pa)) isa PropDict
23+
@test @inferred(isempty(empty(pa)))
24+
1525
@test convert(Dict, pa) === PropDicts._dict(pa)
1626
@test convert(Dict{Union{Symbol,Int}}, pa) === PropDicts._dict(pa)
1727
@test convert(Dict{Union{Symbol,Int},Any}, pa) === PropDicts._dict(pa)
@@ -36,6 +46,12 @@ using Functors
3646
@test pc.bar == PropDict(:baz => raw"$somevar")
3747
@test pc[44] == "abc"
3848

49+
px = PropDict("foo" => 11, "bar" => PropDict("baz" => PropDict("a" => 7, "b" => 9)), "44" => "abc")
50+
py = PropDict("bar" => PropDict("baz" => nothing, "baz2" => 5))
51+
pz = @inferred(merge!(px, py))
52+
@test pz === px
53+
@test pz == Dict(:foo => 11, :bar => Dict(:baz => nothing, :baz2 => 5), 44 => "abc")
54+
3955
@test PropDicts.contains_vars(raw"fo\\$o") == true
4056
@test PropDicts.substitute_vars(raw"foo $bar ${baz} y", ignore_missing = true) == raw"foo $bar ${baz} y"
4157
PropDicts.substitute_vars(raw"foo $bar baz", Dict("bar" => "xyz"))
@@ -61,7 +77,5 @@ using Functors
6177
@test pd.c isa PropDicts.MissingProperty
6278
@test pd.c.d isa PropDicts.MissingProperty
6379
@test get(pd.c, :d, 5) == 5
64-
@test pd.c isa PropDicts.MissingProperty
65-
66-
80+
@test pd.c isa PropDicts.MissingProperty
6781
end

0 commit comments

Comments
 (0)