Skip to content

Commit

Permalink
Initial port from LazyJSON.jl (#1)
Browse files Browse the repository at this point in the history
* Initial port from LazyJSON.jl

* Added Travis CI configuration
  • Loading branch information
mattBrzezinski authored Sep 19, 2020
1 parent e18616d commit 72976a5
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: julia
os:
- linux
- osx
- windows
julia:
- 1.0
- 1
- nightly
matrix:
allow_failures:
- julia: nightly
notifications:
email: false
14 changes: 14 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name = "PropertyDicts"
uuid = "f8a19df8-e894-5f55-a973-672c1158cbca"
license = "MIT"
version = "0.1.0"

[compat]
julia = "1"

[extras]
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["OrderedCollections", "Test"]
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# PropertyDicts.jl
# PropertyDicts.jl

Wrap an `AbstractDict` to add `getproperty` support for `Symbol` and `AbstractString` keys.

```julia
d = PropertyDict(Dict("foo"=>1, :bar=>2))

d.foo, d.bar, d."foo"
> (1, 2, 1)

d."bar"
> ERROR: KeyError: key "bar" not found
```
45 changes: 45 additions & 0 deletions src/PropertyDicts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module PropertyDicts

export PropertyDict

struct PropertyDict{K, V, T <: AbstractDict{K, V}} <: AbstractDict{K, V}
d::T
PropertyDict(d::T) where {T <: AbstractDict} =
new{keytype(d), valtype(d), T}(d)
end

function Base.getproperty(d::PropertyDict, n::Symbol)
v = get(d, n, Base.secret_table_token)

if v != Base.secret_table_token
return v
end

return getindex(d, String(n))
end

Base.convert(::Type{Any}, d::PropertyDict) = d
Base.convert(::Type{PropertyDict{K,V,T}}, d::PropertyDict{K,V,T}) where {K,V,T<:AbstractDict{K,V}} = d
Base.convert(::Type{T}, d::PropertyDict) where T <: AbstractDict = T === AbstractDict ? d : convert(T, PropertyDicts.unwrap(d))
Base.convert(::Type{T}, d::PropertyDict) where T = convert(T, PropertyDicts.unwrap(d))

Base.get(d::PropertyDict, k, default) = get(unwrap(d), k, default)

Base.getindex(d::PropertyDict, i) = getindex(unwrap(d), i)

Base.getproperty(d::PropertyDict, n) = getindex(d, n)
Base.getproperty(d::PropertyDict{AbstractString}, n::Symbol) = getindex(d, String(n))

Base.iterate(d::PropertyDict) = iterate(unwrap(d))
Base.iterate(d::PropertyDict, i) = iterate(unwrap(d), i)

Base.IteratorSize(::Type{PropertyDict{K,V,T}}) where {K,V,T} = Base.IteratorSize(T)
Base.IteratorEltype(::Type{PropertyDict{K,V,T}}) where {K,V,T} = Base.IteratorEltype(T)

Base.length(d::PropertyDict) = length(unwrap(d))

Base.string(d::PropertyDict) = string(unwrap(d))

unwrap(d::PropertyDict) = getfield(d, :d)

end # module PropertyDicts
63 changes: 63 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using OrderedCollections: OrderedDict
using PropertyDicts
using Test

@testset "PropertyDicts" begin
d = Dict("foo"=>1, :bar=>2)
_keys = collect(keys(d))
pd = PropertyDict(d)

@testset "convert" begin
expected = OrderedDict
result = convert(expected, pd)

@test result isa expected
end

@testset "get" begin
@testset "default value" begin
default = "baz"

@test get(pd, "DNE", default) == default
end

@testset "$(typeof(key))" for key in _keys
@test get(pd, key, "DNE") == d[key]
end
end

@testset "getindex - $key" for key in _keys
@test getindex(pd, key) == getindex(d, key)
end

@testset "getproperty" begin
@test pd.foo == 1
@test pd.bar == 2
end

@testset "iterate" begin
@test iterate(pd) == iterate(d)
@test iterate(pd, 1) == iterate(d, 1)
@test iterate(pd, 2) == iterate(d, 2)
end

@testset "iteratorsize" begin
@test Base.IteratorSize(pd) == Base.IteratorSize(d)
end

@testset "iteratoreltype" begin
@test Base.IteratorEltype(pd) == Base.IteratorEltype(d)
end

@testset "length" begin
@test length(pd) == length(d)
end

@testset "string" begin
@test string(pd) == string(d)
end

@testset "unwrap" begin
@test PropertyDicts.unwrap(pd) == d
end
end

2 comments on commit 72976a5

@DilumAluthge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/21641

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 72976a56bbdef540321e262d1d1bc9e9afaf6f76
git push origin v0.1.0

Please sign in to comment.