From b8f52db7530aaa6b51684fda0d31202cafefd144 Mon Sep 17 00:00:00 2001 From: John Omotani Date: Wed, 13 Nov 2024 09:38:28 +0000 Subject: [PATCH] Fix recursive_merge() to return same type as its first argument `mergewith()` always returns a `Dict`, ignoring the types of its arguments. We need `recursive_merge()` to return an `OrderedDict`, so re-implement `recursive_merge()` 'by hand' without using `mergewith()`. --- moment_kinetics/src/utils.jl | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/moment_kinetics/src/utils.jl b/moment_kinetics/src/utils.jl index f38954a294..74b8a3bbb3 100644 --- a/moment_kinetics/src/utils.jl +++ b/moment_kinetics/src/utils.jl @@ -312,16 +312,20 @@ Merge two AbstractDicts `a` and `b`. Any elements that are AbstractDicts are als """ function recursive_merge end function recursive_merge(a::AbstractDict, b::AbstractDict) - return mergewith(recursive_merge, a, b) -end -function recursive_merge(a::AbstractDict, b) - error("Cannot merge a Dict with a non-Dict, got $a and $b") -end -function recursive_merge(a, b::AbstractDict) - error("Cannot merge a Dict with a non-Dict, got $a and $b") -end -function recursive_merge(a, b) - return b + result = deepcopy(a) + a_keys = collect(keys(a)) + for (k,v) ∈ pairs(b) + if k ∉ a_keys + result[k] = v + elseif isa(result[k], AbstractDict) && isa(v, AbstractDict) + result[k] = recursive_merge(result[k], v) + elseif isa(result[k], AbstractDict) || isa(v, AbstractDict) + error("Cannot merge a Dict with a non-Dict, got $(result[k]) and $v") + else + result[k] = v + end + end + return result end """