|
1 | 1 | using Base: tail |
2 | 2 |
|
3 | | -KeyT = Union{Symbol, AbstractString, Integer} |
| 3 | +KeyT = Union{Symbol, AbstractString, Integer, CartesianIndex} |
4 | 4 |
|
5 | 5 | """ |
6 | 6 | KeyPath(keys...) |
7 | 7 |
|
8 | 8 | A type for representing a path of keys to a value in a nested structure. |
9 | 9 | Can be constructed with a sequence of keys, or by concatenating other `KeyPath`s. |
10 | | -Keys can be of type `Symbol`, `String`, or `Int`. |
| 10 | +Keys can be of type `Symbol`, `String`, `Int`, or `CartesianIndex`. |
11 | 11 |
|
12 | 12 | For custom types, access through symbol keys is assumed to be done with `getproperty`. |
13 | 13 | For consistency, the method `Base.propertynames` is used to get the viable property names. |
14 | 14 |
|
15 | | -For string and integer keys instead, the access is done with `getindex`. |
| 15 | +For string, integer, and cartesian index keys, the access is done with `getindex` instead. |
16 | 16 |
|
17 | 17 | See also [`getkeypath`](@ref), [`haskeypath`](@ref). |
18 | 18 |
|
|
85 | 85 | keypathstr(kp::KeyPath) = join(kp.keys, ".") |
86 | 86 |
|
87 | 87 | _getkey(x, k::Integer) = x[k] |
| 88 | +_getkey(x::AbstractArray, k::CartesianIndex) = x[k] |
88 | 89 | _getkey(x, k::Symbol) = getproperty(x, k) |
89 | 90 | _getkey(x::AbstractDict, k::Symbol) = x[k] |
90 | 91 | _getkey(x, k::AbstractString) = x[k] |
91 | 92 |
|
92 | 93 | _setkey!(x, k::Integer, v) = (x[k] = v) |
| 94 | +_setkey!(x::AbstractArray, k::CartesianIndex, v) = (x[k] = v) |
93 | 95 | _setkey!(x, k::Symbol, v) = setproperty!(x, k, v) |
94 | 96 | _setkey!(x::AbstractDict, k::Symbol, v) = (x[k] = v) |
95 | 97 | _setkey!(x, k::AbstractString, v) = (x[k] = v) |
96 | 98 |
|
97 | 99 | _haskey(x, k::Integer) = haskey(x, k) |
98 | 100 | _haskey(x::Tuple, k::Integer) = 1 <= k <= length(x) |
99 | 101 | _haskey(x::AbstractArray, k::Integer) = 1 <= k <= length(x) # TODO: extend to generic indexing |
| 102 | +_haskey(x::AbstractArray, k::CartesianIndex) = checkbounds(Bool, x, k) |
100 | 103 | _haskey(x, k::Symbol) = k in propertynames(x) |
101 | 104 | _haskey(x::AbstractDict, k::Symbol) = haskey(x, k) |
102 | 105 | _haskey(x, k::AbstractString) = haskey(x, k) |
|
0 commit comments