Skip to content

Commit 72976a5

Browse files
Initial port from LazyJSON.jl (#1)
* Initial port from LazyJSON.jl * Added Travis CI configuration
1 parent e18616d commit 72976a5

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: julia
2+
os:
3+
- linux
4+
- osx
5+
- windows
6+
julia:
7+
- 1.0
8+
- 1
9+
- nightly
10+
matrix:
11+
allow_failures:
12+
- julia: nightly
13+
notifications:
14+
email: false

Project.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name = "PropertyDicts"
2+
uuid = "f8a19df8-e894-5f55-a973-672c1158cbca"
3+
license = "MIT"
4+
version = "0.1.0"
5+
6+
[compat]
7+
julia = "1"
8+
9+
[extras]
10+
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
11+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
12+
13+
[targets]
14+
test = ["OrderedCollections", "Test"]

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
# PropertyDicts.jl
1+
# PropertyDicts.jl
2+
3+
Wrap an `AbstractDict` to add `getproperty` support for `Symbol` and `AbstractString` keys.
4+
5+
```julia
6+
d = PropertyDict(Dict("foo"=>1, :bar=>2))
7+
8+
d.foo, d.bar, d."foo"
9+
> (1, 2, 1)
10+
11+
d."bar"
12+
> ERROR: KeyError: key "bar" not found
13+
```

src/PropertyDicts.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module PropertyDicts
2+
3+
export PropertyDict
4+
5+
struct PropertyDict{K, V, T <: AbstractDict{K, V}} <: AbstractDict{K, V}
6+
d::T
7+
PropertyDict(d::T) where {T <: AbstractDict} =
8+
new{keytype(d), valtype(d), T}(d)
9+
end
10+
11+
function Base.getproperty(d::PropertyDict, n::Symbol)
12+
v = get(d, n, Base.secret_table_token)
13+
14+
if v != Base.secret_table_token
15+
return v
16+
end
17+
18+
return getindex(d, String(n))
19+
end
20+
21+
Base.convert(::Type{Any}, d::PropertyDict) = d
22+
Base.convert(::Type{PropertyDict{K,V,T}}, d::PropertyDict{K,V,T}) where {K,V,T<:AbstractDict{K,V}} = d
23+
Base.convert(::Type{T}, d::PropertyDict) where T <: AbstractDict = T === AbstractDict ? d : convert(T, PropertyDicts.unwrap(d))
24+
Base.convert(::Type{T}, d::PropertyDict) where T = convert(T, PropertyDicts.unwrap(d))
25+
26+
Base.get(d::PropertyDict, k, default) = get(unwrap(d), k, default)
27+
28+
Base.getindex(d::PropertyDict, i) = getindex(unwrap(d), i)
29+
30+
Base.getproperty(d::PropertyDict, n) = getindex(d, n)
31+
Base.getproperty(d::PropertyDict{AbstractString}, n::Symbol) = getindex(d, String(n))
32+
33+
Base.iterate(d::PropertyDict) = iterate(unwrap(d))
34+
Base.iterate(d::PropertyDict, i) = iterate(unwrap(d), i)
35+
36+
Base.IteratorSize(::Type{PropertyDict{K,V,T}}) where {K,V,T} = Base.IteratorSize(T)
37+
Base.IteratorEltype(::Type{PropertyDict{K,V,T}}) where {K,V,T} = Base.IteratorEltype(T)
38+
39+
Base.length(d::PropertyDict) = length(unwrap(d))
40+
41+
Base.string(d::PropertyDict) = string(unwrap(d))
42+
43+
unwrap(d::PropertyDict) = getfield(d, :d)
44+
45+
end # module PropertyDicts

test/runtests.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using OrderedCollections: OrderedDict
2+
using PropertyDicts
3+
using Test
4+
5+
@testset "PropertyDicts" begin
6+
d = Dict("foo"=>1, :bar=>2)
7+
_keys = collect(keys(d))
8+
pd = PropertyDict(d)
9+
10+
@testset "convert" begin
11+
expected = OrderedDict
12+
result = convert(expected, pd)
13+
14+
@test result isa expected
15+
end
16+
17+
@testset "get" begin
18+
@testset "default value" begin
19+
default = "baz"
20+
21+
@test get(pd, "DNE", default) == default
22+
end
23+
24+
@testset "$(typeof(key))" for key in _keys
25+
@test get(pd, key, "DNE") == d[key]
26+
end
27+
end
28+
29+
@testset "getindex - $key" for key in _keys
30+
@test getindex(pd, key) == getindex(d, key)
31+
end
32+
33+
@testset "getproperty" begin
34+
@test pd.foo == 1
35+
@test pd.bar == 2
36+
end
37+
38+
@testset "iterate" begin
39+
@test iterate(pd) == iterate(d)
40+
@test iterate(pd, 1) == iterate(d, 1)
41+
@test iterate(pd, 2) == iterate(d, 2)
42+
end
43+
44+
@testset "iteratorsize" begin
45+
@test Base.IteratorSize(pd) == Base.IteratorSize(d)
46+
end
47+
48+
@testset "iteratoreltype" begin
49+
@test Base.IteratorEltype(pd) == Base.IteratorEltype(d)
50+
end
51+
52+
@testset "length" begin
53+
@test length(pd) == length(d)
54+
end
55+
56+
@testset "string" begin
57+
@test string(pd) == string(d)
58+
end
59+
60+
@testset "unwrap" begin
61+
@test PropertyDicts.unwrap(pd) == d
62+
end
63+
end

0 commit comments

Comments
 (0)