-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial port from LazyJSON.jl * Added Travis CI configuration
- Loading branch information
1 parent
e18616d
commit 72976a5
Showing
5 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
72976a5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
72976a5
There was a problem hiding this comment.
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: