Skip to content

Commit

Permalink
Support Base.get
Browse files Browse the repository at this point in the history
  • Loading branch information
oschulz committed Dec 1, 2023
1 parent 5438a45 commit 0968803
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/propdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/test_propdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0968803

Please sign in to comment.