From ab6b2707a132405e07fe89b72a9f70ee927dec56 Mon Sep 17 00:00:00 2001 From: Zaq? Wiedmann Date: Tue, 17 May 2022 19:30:09 -0700 Subject: [PATCH] fix: gate transformers on valid non-nil destinations This builds on #203 which attempted to provide a more flexible gating to running transformers. However upon testing #203 in my own environment, I ran into the first panic listed below. There are a variety of errors that can happen when trying to run reflection on zero values: 2 just from my testing of this PR ``` panic: reflect: call of reflect.Value.Type on zero Value panic: reflect: call of reflect.Value.FieldByName on zero Value ``` The panic specifically calls out zero values, but it's actual a more specific set of values which is covered by `reflect.IsValid`. I attempted to replace the check with `reflect.IsZero`, which ends up being too restrictive and breaks existing tests. I also attempted to solely use `reflect.IsZero` which is not restrictive enough and cause the later panic above in the tests. Thus I arrived on the combination in this PR which seems to strike the "right" balance. --- merge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge.go b/merge.go index 8c2a8fc..8b4e2f4 100644 --- a/merge.go +++ b/merge.go @@ -79,7 +79,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co visited[h] = &visit{addr, typ, seen} } - if config.Transformers != nil && !isEmptyValue(dst) { + if config.Transformers != nil && !isReflectNil(dst) && dst.IsValid() { if fn := config.Transformers.Transformer(dst.Type()); fn != nil { err = fn(dst, src) return