*datatypes.JSON in model causes panic on tx.Statement.Changed (#6611)

* do not panic on nil

* more explanation in comments

* get things compact
This commit is contained in:
Mathias Zeller 2023-10-10 08:46:32 +02:00 committed by GitHub
parent 9d8a5bb208
commit 12ba285a52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 12 deletions

View File

@ -89,19 +89,28 @@ func Contains(elems []string, elem string) bool {
return false
}
func AssertEqual(src, dst interface{}) bool {
if !reflect.DeepEqual(src, dst) {
if valuer, ok := src.(driver.Valuer); ok {
src, _ = valuer.Value()
}
if valuer, ok := dst.(driver.Valuer); ok {
dst, _ = valuer.Value()
}
return reflect.DeepEqual(src, dst)
}
func AssertEqual(x, y interface{}) bool {
if reflect.DeepEqual(x, y) {
return true
}
if x == nil || y == nil {
return false
}
xval := reflect.ValueOf(x)
yval := reflect.ValueOf(y)
if xval.Kind() == reflect.Ptr && xval.IsNil() ||
yval.Kind() == reflect.Ptr && yval.IsNil() {
return false
}
if valuer, ok := x.(driver.Valuer); ok {
x, _ = valuer.Value()
}
if valuer, ok := y.(driver.Valuer); ok {
y, _ = valuer.Value()
}
return reflect.DeepEqual(x, y)
}
func ToString(value interface{}) string {

View File

@ -98,6 +98,7 @@ func TestAssertEqual(t *testing.T) {
{"error not equal", errors.New("1"), errors.New("2"), false},
{"driver.Valuer equal", ModifyAt{Time: now, Valid: true}, ModifyAt{Time: now, Valid: true}, true},
{"driver.Valuer not equal", ModifyAt{Time: now, Valid: true}, ModifyAt{Time: now.Add(time.Second), Valid: true}, false},
{"driver.Valuer equal (ptr to nil ptr)", (*ModifyAt)(nil), &ModifyAt{}, false},
}
for _, test := range assertEqualTests {
t.Run(test.name, func(t *testing.T) {