Skip to content

Commit

Permalink
Fixed: panic on nullable value with multiple foreign key usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shtrih committed Feb 14, 2024
1 parent 8fb9a31 commit f29185b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nil> 3]"},
}
for _, c := range cases {
if key := ToStringKey(c.values...); key != c.key {
Expand Down

0 comments on commit f29185b

Please sign in to comment.