Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
feat(errors): convenience for parameter get+check
Browse files Browse the repository at this point in the history
  • Loading branch information
tecosaur committed Jul 3, 2023
1 parent 0104154 commit a0aa954
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DataToolkitBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export IdentifierException, UnresolveableIdentifier, AmbiguousIdentifier,
PackageException, UnregisteredPackage, MissingPackage,
DataOperationException, CollectionVersionMismatch, EmptyStackError,
ReadonlyCollection, TransformerError, UnsatisfyableTransformer,
OrphanDataSet
OrphanDataSet, InvalidParameterType
export STACK, DATA_CONFIG_RESERVED_ATTRIBUTES
export @import, @addpkg, @dataplugin, @advise
export @import, @addpkg, @dataplugin, @advise, @getparam

# For plugin packages
export PLUGINS, PLUGINS_DOCUMENTATION, DEFAULT_PLUGINS, Plugin,
Expand Down
36 changes: 35 additions & 1 deletion src/model/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,43 @@ struct InvalidParameterType{T <: Union{<:AbstractDataTransformer, DataSet, DataC
type::Type
end

function Base.showerror(io::IO, err::InvalidParameterType{<:AbstractDataTransformer})
print(io, "InvalidParameterType: '", err.parameter, "' parameter of ",
err.thing.dataset.name, "'s ", nameof(typeof(err.thing)),
"{:", string(first(typeof(err.thing).parameters)), "} must be a ",
string(err.type), " not a ",
string(typeof(get(err.thing, err.parameter))), ".")
end

function Base.showerror(io::IO, err::InvalidParameterType)
print(io, "InvalidParameterType: '", err.parameter, "' parameter of ",
string(err.thing), " must be a ", string(err.type), " not a ",
string(typeof(get(err.thing, err.parameter))), ".")
# More info about `err.transformer` / parent dataset?
end

macro getparam(expr::Expr, default=nothing)
thing, type = if Meta.isexpr(expr, :(::)) expr.args else (expr, :Any) end
Meta.isexpr(thing, :.) || error("Malformed expression passed to @getparam")
root, param = thing.args
if isnothing(default)
typename = if type isa Symbol type
elseif Meta.isexpr(type, :curly) first(type.args)
else :Any end
default = if typename (:Vector, :Dict, :SmallDict)
:($type())
else :nothing end
end
if type == :Any
:(get($(esc(root)), $(esc(param)), $(esc(default))))
else
quote
let value = get($(esc(root)), $(esc(param)), $(esc(default)))
if !(value isa $(esc(type)))
throw(InvalidParameterType(
$(esc(root)), $(esc(param)), $(esc(type))))
end
value
end
end
end
end

0 comments on commit a0aa954

Please sign in to comment.