Skip to content

Commit

Permalink
version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama committed Apr 6, 2022
1 parent c8156a9 commit d6a8e0e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 58 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.3.2"
version = "0.3.3"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
1 change: 1 addition & 0 deletions src/Metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const MDict = Union{Dict{Symbol,Any},Dict{String,Any}}

include("NoMetadata.jl")
include("interface.jl")
include("deprecations.jl")
include("GlobalMetadata.jl")
include("MetaStruct.jl")
include("MetaDict.jl")
Expand Down
47 changes: 47 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@


"""
metadata(x, k)
Returns the value associated with key `k` of `x`'s metadata.
"""
function metadata(x, k)
out = getmeta(x, k, no_metadata)
out === no_metadata && throw(KeyError(k))
return out
end

"""
metadata!(x::AbstractArray, k, val)
Set the value associated with key `k` of `x`'s metadata to `val`.
"""
@inline metadata!(x, k, val) = metadata!(metadata(x), k, val)
metadata!(x::AbstractDict, k, val) = setindex!(x, val, k)
metadata!(m::AbstractDict{String}, k::Symbol, val) = setindex!(m, val, String(k))
metadata!(m::AbstractDict{Symbol}, k::String, val) = setindex!(m, val, Symbol(k))

"""
metadata!(x::AbstractArray, k, val; dim)
Set the value associated with key `k` of the metadata at dimension `dim` of `x` to `val`.
"""
metadata!(x::AbstractArray, k, val; dim=nothing) = metadata!(metadata(x; dim=dim), k, val)

"""
metadata(x::AbstractArray, k; dim)
Returns the value associated with key `k` of `x`'s metadata.
"""
@inline function metadata(x::AbstractArray, k; dim=nothing)
if dim === nothing
if has_metadata(x)
return metadata(metadata(x), k)
else
return no_metadata
end
else
return metadata(metadata(x; dim=dim), k)
end
end

68 changes: 11 additions & 57 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,49 +40,14 @@ metadata_type(::Type{T}) where {T<:AbstractDict} = T
metadata_type(::Type{T}) where {T<:NamedTuple} = T

"""
metadata(x, k)
Returns the value associated with key `k` of `x`'s metadata.
"""
function metadata(x, k)
out = getmeta(x, k, no_metadata)
out === no_metadata && throw(KeyError(k))
return out
end

"""
metadata(x::AbstractArray, k; dim)
Returns the value associated with key `k` of `x`'s metadata.
"""
@inline function metadata(x::AbstractArray, k; dim=nothing)
if dim === nothing
if has_metadata(x)
return metadata(metadata(x), k)
else
return no_metadata
end
else
return metadata(metadata(x; dim=dim), k)
end
end

"""
metadata!(x::AbstractArray, k, val)
Set the value associated with key `k` of `x`'s metadata to `val`.
"""
@inline metadata!(x, k, val) = metadata!(metadata(x), k, val)
metadata!(x::AbstractDict, k, val) = setindex!(x, val, k)
metadata!(m::AbstractDict{String}, k::Symbol, val) = setindex!(m, val, String(k))
metadata!(m::AbstractDict{Symbol}, k::String, val) = setindex!(m, val, Symbol(k))

"""
metadata!(x::AbstractArray, k, val; dim)
has_metadata(x)::Bool
Set the value associated with key `k` of the metadata at dimension `dim` of `x` to `val`.
Returns `true` if `x` has metadata.
"""
metadata!(x::AbstractArray, k, val; dim=nothing) = metadata!(metadata(x; dim=dim), k, val)
has_metadata(x) = has_metadata(typeof(x))
has_metadata(::Type{T}) where {T} = _has_metadata(metadata_type(T))
_has_metadata(::Type{NoMetadata}) = false
_has_metadata(::Type{T}) where {T} = true

"""
metadata_type(::Type{<:AbstractArray}; dim)::Type
Expand Down Expand Up @@ -170,16 +135,6 @@ that `getmeta!` passes `x` to `f` as an argument (as opposed to `f()`).
return out
end

"""
has_metadata(x)::Bool
Returns `true` if `x` has metadata.
"""
has_metadata(x) = has_metadata(typeof(x))
has_metadata(::Type{T}) where {T} = _has_metadata(metadata_type(T))
_has_metadata(::Type{NoMetadata}) = false
_has_metadata(::Type{T}) where {T} = true

"""
has_metadata(x::AbstractArray; dim)::Bool
Expand Down Expand Up @@ -287,7 +242,7 @@ macro defproperties(T)
end
end

Base.getproperty(x::$T, k::String) = Metadata.metadata(x, k)
Base.getproperty(x::$T, k::String) = getproperty(x, Symbol(k))
@inline function Base.getproperty(x::$T, k::Symbol)
if hasproperty(parent(x), k)
return getproperty(parent(x), k)
Expand All @@ -296,15 +251,14 @@ macro defproperties(T)
end
end

Base.setproperty!(x::$T, k::String, v) = Metadata.metadata!(x, k, v)
@inline function Base.setproperty!(x::$T, k::Symbol, val)
Base.setproperty!(x::$T, k::String, v) = setproperty!(x, Symbol(k), v)
@inline function Base.setproperty!(x::$T, k::Symbol, v)
if hasproperty(parent(x), k)
return setproperty!(parent(x), k, val)
return setproperty!(parent(x), k, v)
else
return Metadata.metadata!(x, k, val)
return Metadata.metadata!(x, k, v)
end
end

@inline Base.propertynames(x::$T) = Metadata.metadata_keys(x)
end)
end
Expand Down

0 comments on commit d6a8e0e

Please sign in to comment.