< Summary

Information
Class: src/pointerdict.jl
Assembly: Default
File(s): src/pointerdict.jl
Tag: 7_9662997695
Line coverage
73%
Covered lines: 34
Uncovered lines: 12
Coverable lines: 46
Total lines: 115
Line coverage: 73.9%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Coverage History

File(s)

src/pointerdict.jl

#LineLine coverage
 1"""
 2    PointerDict
 3
 4`PointerDict` is wrapper around `AbstractDict` that allows to use `JSONPointer` as keys.
 5
 6## Constructors
 7
 8PointerDict("a" => 1, "b" => 2)
 9PointerDict(j"/a" => 1, j"/b" => 2)
 10PointerDict(Dict("a" => 1, "b" => 2)
 11"""
 12struct PointerDict{K<:Union{String,Symbol}, V} <: AbstractDict{K, V}
 13    d::AbstractDict
 14
 115    PointerDict(@nospecialize pd::PointerDict) = pd
 2316    PointerDict(d::AbstractDict{String,V}) where {V} = new{String,V}(d)
 117    PointerDict(d::AbstractDict{Symbol,V}) where {V} = new{Symbol,V}(d)
 18
 19    function PointerDict(d::T) where T <: AbstractDict
 120        dict = dicttype(T){String,valtype(d)}()
 221        for (k,v) in d
 322            dict[string(k)] = v
 23        end
 124        PointerDict(dict)
 25    end
 26    function PointerDict(args...)
 1427        d = OrderedDict{String, Any}()
 1428        for (k, v) in args
 3829            if isa(k, Pointer)
 3130                _setindex!(d, v, k)
 31            else
 5132                setindex!(d, v, k)
 33            end
 34        end
 1435        PointerDict(d)
 36    end
 37    # PointerDict(; kwargs...) = PointerDict(values(kwargs))
 38end
 39
 140dicttype(::Type{T}) where T <: AbstractDict = eval(T.name.wrapper)
 141dicttype(x::T) where T <: AbstractDict = eval(T.name.wrapper)
 42
 043Base.IteratorSize(@nospecialize T::Type{<:PointerDict}) = Base.IteratorSize(fieldtype(T, :d))
 044Base.IteratorEltype(@nospecialize T::Type{<:PointerDict}) = Base.IteratorEltype(eltype(T))
 45
 146Base.keytype(@nospecialize T::Type{<:PointerDict{String}}) = String
 047Base.keytype(@nospecialize T::Type{<:PointerDict{Symbol}}) = Symbol
 48
 49function Base.empty(pd::PointerDict, ::Type{K}=keytype(pd), ::Type{V}=valtype(pd)) where {K,V}
 250    PointerDict(empty(getfield(pd, :d), K, V))
 51end
 52
 53# Simply delegated dictionary functions to the wrapped PointerDdicct object
 54# NOTE: push! is not included below, because the fallback version just
 55#       calls setindex!
 56@delegate_onefield(PointerDict, d, [ Base.getindex, Base.get, Base.get!, Base.haskey,
 57Base.getkey, Base.pop!, Base.iterate,
 58Base.isempty, Base.length, Base.delete!, Base.setindex!])
 59# Base.copy(pd::PointerDict) = PointerDict(copy(getfield(pd, :d)))
 60
 61# empty! returns the wrapped dictionary if simply delegated
 62function Base.empty!(pd::PointerDict)
 163    empty!(getfield(pd, :d))
 164    return pd
 65end
 66
 67
 68function Base.get(pd::PointerDict, jk::Pointer, d)
 369    _get(getfield(pd, :d), jk, d)
 70end
 071function Base.get(f::Base.Callable, pd::PointerDict, jp::Pointer)
 072    _get(f, getfield(pd, :d), jp)
 73end
 74
 75function Base.get!(pd::PointerDict, jp::Pointer, d)
 376    _get!(getfield(pd, :d), jp, d)
 77end
 078function Base.get!(f::Base.Callable, pd::PointerDict, jp::Pointer)
 079    _get!(f, getfield(pd, :d), jp)
 80end
 81
 82function Base.getindex(pd::PointerDict, jp::Pointer)
 5683    _getindex(getfield(pd, :d), jp)
 84end
 85
 86function Base.setindex!(pd::PointerDict, v, jp::Pointer)
 887    _setindex!(getfield(pd, :d), v, jp)
 88end
 2089Base.haskey(pd::PointerDict, jp::Pointer) = _haskey(getfield(pd, :d), jp)
 090Base.getkey(pd::PointerDict, jp::Pointer, d) = getkey(getfield(pd, :d), jp, d)
 91
 92## merge and mergewith
 093Base.merge(pd::PointerDict) = copy(pd)
 94
 95function Base.merge(pd::PointerDict, pds::PointerDict...)
 296    K = _promote_keytypes((pd, pds...))
 297    V = _promote_valtypes(valtype(pd), pds...)
 298    out = PointerDict(Dict{K,V}())
 299    for (k,v) in pd
 6100        out[k] = v
 101    end
 2102    merge!(recursive_merge, out, pds...)
 103end
 104
 3105recursive_merge(x::AbstractDict...) = merge(recursive_merge, x...)
 2106recursive_merge(x::AbstractVector...) = cat(x...; dims=1)
 0107recursive_merge(x...) = x[end]
 108
 109# fall back to String if we don't clearly have Symbol
 0110_promote_keytypes(@nospecialize(pds::Tuple{Vararg{PointerDict{Symbol, T}}})) where T = Symbol
 2111_promote_keytypes(@nospecialize(pds::Tuple{Vararg{PointerDict}})) = String
 0112_promote_valtypes(V) = V
 113function _promote_valtypes(V, d, ds...)  # give up if promoted to any
 2114    V === Any ? Any : _promote_valtypes(promote_type(V, valtype(d)), ds...)
 115end

Methods/Properties