From acba201ed6a1ec6e3dc806c76b964ce0594fe8eb Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Thu, 6 Aug 2020 19:42:07 +0200 Subject: [PATCH 1/2] ducktyping... --- src/ProgressLogging.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ProgressLogging.jl b/src/ProgressLogging.jl index 821023c..86ab4ec 100644 --- a/src/ProgressLogging.jl +++ b/src/ProgressLogging.jl @@ -164,9 +164,15 @@ function asprogress( progress = undef, # `undef` is an arbitrary unsupported value kwargs..., ) + if hasfield(typeof(name), :progress) + return Progress((getfield(name.progress, f) for f in fieldnames(Progress))...) + end if progress isa Union{Nothing,Real,AbstractString} return _asprogress(name, id; progress = progress, kwargs...) else + if fieldnames(typeof(progress)) == fieldnames(Progress) + return Progress((getfield(progress, f) for f in fieldnames(Progress))...) + end return nothing end end From 70ccc2bdf8eb246fc498727612c003743aa766de Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Mon, 14 Sep 2020 17:34:09 +0200 Subject: [PATCH 2/2] better ducks --- src/ProgressLogging.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ProgressLogging.jl b/src/ProgressLogging.jl index 86ab4ec..7d8c9dd 100644 --- a/src/ProgressLogging.jl +++ b/src/ProgressLogging.jl @@ -164,19 +164,23 @@ function asprogress( progress = undef, # `undef` is an arbitrary unsupported value kwargs..., ) - if hasfield(typeof(name), :progress) - return Progress((getfield(name.progress, f) for f in fieldnames(Progress))...) + if hasfield(typeof(name), :progress) && is_progresslike(name.progress) + return _asprogress(name.progress) end if progress isa Union{Nothing,Real,AbstractString} return _asprogress(name, id; progress = progress, kwargs...) else - if fieldnames(typeof(progress)) == fieldnames(Progress) - return Progress((getfield(progress, f) for f in fieldnames(Progress))...) + if is_progresslike(progress) + return _asprogress(progress) end return nothing end end +is_progresslike(_::T) where {T} = all(in.(fieldnames(Progress), Ref(fieldnames(T)))) + +_asprogress(progress) = Progress((getfield(progress, f) for f in fieldnames(Progress))...) + # `parentid` is used from `@logprogress`. function _asprogress(name, id, parentid = ROOTID; progress, _...) if progress isa Union{Nothing,Real}