diff --git a/src/propdict.jl b/src/propdict.jl index 344e6fd..94f6fbd 100644 --- a/src/propdict.jl +++ b/src/propdict.jl @@ -171,6 +171,8 @@ function Base.getindex(p::PropDict, key) end end +Base.get(p::PropDict, key, default) = get(_dict(p), key, default) + Base.get!(p::PropDict, key, default) = get!(_dict(p), key, default) Base.setindex!(p::PropDict, value, key) = setindex!(_dict(p), _convert_value(value), key) @@ -310,6 +312,8 @@ MissingProperty(m::MissingProperty) = MissingProperty(_internal_parent(m), _inte Base.getindex(@nospecialize(m::MissingProperty), @nospecialize(key)) = MissingProperty(m, key) +Base.get(@nospecialize(m::MissingProperty), @nospecialize(key), default) = default + @inline function Base.getproperty(@nospecialize(m::MissingProperty), s::Symbol) if s == :_internal_parent getfield(m, :_internal_parent) diff --git a/test/test_propdict.jl b/test/test_propdict.jl index 6befb42..fa47c1e 100644 --- a/test/test_propdict.jl +++ b/test/test_propdict.jl @@ -44,4 +44,18 @@ using Test @test xa.a.b isa PropDicts.MissingProperty @test (xa.a.b[33].c = 42) == 42 @test xa.a.b[33].c == 42 + + pd = PropDict(:a => 42) + @test get(pd, :a, 7) == 42 + @test pd.b isa PropDicts.MissingProperty + @test get(pd, :b, 7) == 7 + @test pd.b isa PropDicts.MissingProperty + @test get!(pd, :b, 9) == 9 + @test !(pd.b isa PropDicts.MissingProperty) + @test pd.b == 9 + + @test pd.c isa PropDicts.MissingProperty + @test pd.c.d isa PropDicts.MissingProperty + @test get(pd.c, :d, 5) == 5 + @test pd.c isa PropDicts.MissingProperty end