Skip to content

Commit

Permalink
Add support for metadata with String keys (#6)
Browse files Browse the repository at this point in the history
* Support metadata with String keys
  • Loading branch information
Tokazama authored Jul 29, 2021
1 parent b183e9b commit 50cc0c5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Metadata"
uuid = "4fb893c1-3164-4f58-823a-cb4c64eabb4f"
authors = ["Zachary P. Christensen <[email protected]>"]
version = "0.2.6"
version = "0.2.7"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ true
# Creating New Metadata Types

This package creates a very minimal number of dedicated structures and creating new dedicated structures that use this interface is encouraged.
```julia
```
abstract type AbstractNoop end
struct Noop <: AbstractNoop end
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export
metadata_type,
share_metadata

const METADATA_TYPES = Union{<:AbstractDict{Symbol,Any},<:NamedTuple}
const METADATA_TYPES = Union{<:AbstractDict{String,Any},<:AbstractDict{Symbol,Any},<:NamedTuple}

# default dict
const MDict = Dict{Symbol,Any}
const MDict = Union{Dict{Symbol,Any},Dict{String,Any}}

include("utils.jl")
include("metastruct.jl")
Expand Down
17 changes: 10 additions & 7 deletions src/metaarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
end
end

function MetaArray{T,N,M,A}(a::AbstractArray; metadata=MDict(), kwargs...) where {T,N,M,A}
function MetaArray{T,N,M,A}(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N,M,A}
return MetaArray{T,N,M,A}(a, _construct_meta(metadata, values(kwargs)))
end

function MetaArray{T,N,M,A}(args...; metadata=MDict(), kwargs...) where {T,N,M,A}
function MetaArray{T,N,M,A}(args...; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N,M,A}
return MetaArray{T,N,M,A}(A(args...); metadata=metadata, kwargs...)
end

Expand All @@ -65,24 +65,24 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
###
MetaArray{T,N}(a::AbstractArray, m::M) where {T,N,M} = MetaArray{T,N,M}(a, m)

function MetaArray{T,N}(a::AbstractArray; metadata=MDict(), kwargs...) where {T,N}
function MetaArray{T,N}(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N}
return MetaArray{T,N}(a, _construct_meta(metadata, values(kwargs)))
end

function MetaArray{T,N}(args...; metadata=MDict(), kwargs...) where {T,N}
function MetaArray{T,N}(args...; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N}
return MetaArray{T,N}(Array{T,N}(args...); metadata=metadata, kwargs...)
end

###
### MetArray{T}
###
function MetaArray{T}(args...; metadata=MDict(), kwargs...) where {T}
function MetaArray{T}(args...; metadata=Dict{Symbol,Any}(), kwargs...) where {T}
return MetaArray{T}(Array{T}(args...); metadata=metadata, kwargs...)
end

MetaArray{T}(a::AbstractArray, m::M) where {T,M} = MetaArray{T,ndims(a)}(a, m)

function MetaArray{T}(a::AbstractArray; metadata=MDict(), kwargs...) where {T}
function MetaArray{T}(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...) where {T}
return MetaArray{T,ndims(a)}(a; metadata=metadata, kwargs...)
end

Expand All @@ -91,7 +91,7 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
###
MetaArray(v::AbstractArray{T,N}, m::M) where {T,N,M} = new{T,N,M,typeof(v)}(v, m)

function MetaArray(a::AbstractArray; metadata=MDict(), kwargs...)
function MetaArray(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...)
return MetaArray{eltype(a)}(a; metadata=metadata, kwargs...)
end
end
Expand Down Expand Up @@ -148,6 +148,9 @@ function Base.similar(

return Metadata.propagate_metadata(x, similar(parent(x), T, dims))
end
function Base.similar(x::MetaArray, ::Type{T}, dims::Tuple{Integer, Vararg{Integer}}) where {T}
return Metadata.propagate_metadata(x, similar(parent(x), T, dims))
end

Base.summary(io::IO, x::MetaArray) = Base.showarg(io, x, true)
function Base.showarg(io::IO, x::MetaArray, toplevel)
Expand Down
12 changes: 6 additions & 6 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct GlobalMetadata <: AbstractDict{UInt,MDict}

function GlobalMetadata(m::Module)
if !isdefined(m, GLOBAL_METADATA)
Core.eval(m, :(const $GLOBAL_METADATA = $(new(IdDict{UInt,MDict}()))))
Core.eval(m, :(const $GLOBAL_METADATA = $(new(IdDict{UInt,Dict{Symbol,Any}}()))))
push!(modules, m)
end
return metadata(m)
Expand Down Expand Up @@ -214,16 +214,16 @@ has_metadata(x::AbstractArray, k; dim=nothing) = haskey(metadata(x; dim=dim), k)
Generic method for attaching metadata to `x`.
"""
attach_metadata(x, m=MDict()) = MetaStruct(x, m)
attach_metadata(x::AbstractArray, m=MDict()) = MetaArray(x, m)
function attach_metadata(x::AbstractRange, m=MDict())
attach_metadata(x, m=Dict{Symbol,Any}()) = MetaStruct(x, m)
attach_metadata(x::AbstractArray, m=Dict{Symbol,Any}()) = MetaArray(x, m)
function attach_metadata(x::AbstractRange, m=Dict{Symbol,Any}())
if known_step(x) === oneunit(eltype(x))
return MetaUnitRange(x, m)
else
return MetaRange(x, m)
end
end
attach_metadata(x::IO, m=MDict()) = MetaIO(x, m)
attach_metadata(x::IO, m=Dict{Symbol,Any}()) = MetaIO(x, m)
attach_metadata(m::METADATA_TYPES) = Base.Fix2(attach_metadata, m)

"""
Expand Down Expand Up @@ -396,7 +396,7 @@ function attach_global_metadata(x, meta::MDict, m::Module)
end

function attach_global_metadata(x, meta, m::Module)
gm = MDict()
gm = Dict{Symbol,Any}()
for (k,v) in pairs(meta)
gm[k] = v
end
Expand Down

0 comments on commit 50cc0c5

Please sign in to comment.