From d81ae6f701e09214ac550a3308428f8434174f5f Mon Sep 17 00:00:00 2001 From: M Dmitry Date: Mon, 19 Feb 2024 03:42:25 +0000 Subject: [PATCH] Fixed: panic on nullable value with multiple foreign key usage (#6839) See: https://github.com/go-gorm/playground/pull/537 --- utils/utils.go | 6 +++++- utils/utils_test.go | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index a4d8ac25..347a331f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -74,7 +74,11 @@ func ToStringKey(values ...interface{}) string { case uint: results[idx] = strconv.FormatUint(uint64(v), 10) default: - results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface()) + results[idx] = "nil" + vv := reflect.ValueOf(v) + if vv.IsValid() && !vv.IsZero() { + results[idx] = fmt.Sprint(reflect.Indirect(vv).Interface()) + } } } diff --git a/utils/utils_test.go b/utils/utils_test.go index d0486822..8ff42af8 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -48,8 +48,10 @@ func TestToStringKey(t *testing.T) { }{ {[]interface{}{"a"}, "a"}, {[]interface{}{1, 2, 3}, "1_2_3"}, + {[]interface{}{1, nil, 3}, "1_nil_3"}, {[]interface{}{[]interface{}{1, 2, 3}}, "[1 2 3]"}, {[]interface{}{[]interface{}{"1", "2", "3"}}, "[1 2 3]"}, + {[]interface{}{[]interface{}{"1", nil, "3"}}, "[1 3]"}, } for _, c := range cases { if key := ToStringKey(c.values...); key != c.key {