1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-07 21:47:02 +09:00

Fix nil interface issue

This commit is contained in:
Mikhail Iudin 2025-02-25 13:50:20 +01:00
parent b04a48144d
commit fbf22655fe
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
3 changed files with 33 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/anyproto/any-sync/util/reflection"
"sync"
"time"
@ -168,7 +169,7 @@ func (c *oCache) load(ctx context.Context, id string, e *entry) {
c.mu.Lock()
defer c.mu.Unlock()
if value == nil && err == nil {
if reflection.IsNilish(value) && err == nil {
err = fmt.Errorf("loaded value is nil, id: %s", id)
}
if err != nil {

View file

@ -67,6 +67,7 @@ func TestOCache_Get(t *testing.T) {
assert.Equal(t, 1, c.Len())
assert.NoError(t, c.Close())
})
t.Run("error", func(t *testing.T) {
tErr := errors.New("err")
c := New(func(ctx context.Context, id string) (value Object, err error) {
@ -283,6 +284,17 @@ func TestOCache_GC(t *testing.T) {
}
func Test_OCache_Remove(t *testing.T) {
t.Run("remove puzzler", func(t *testing.T) {
c := New(func(ctx context.Context, id string) (value Object, err error) {
var p *testObject
return p, err
})
val, err := c.Get(context.TODO(), "test")
require.Error(t, err, "loaded value is nil, id: test")
require.Nil(t, val)
require.Equal(t, 0, c.Len())
require.NoError(t, c.Close())
})
t.Run("remove simple", func(t *testing.T) {
closeCh := make(chan struct{})
getCh := make(chan struct{})

View file

@ -0,0 +1,19 @@
package reflection
import "reflect"
func IsNilish(val any) bool {
if val == nil {
return true
}
v := reflect.ValueOf(val)
k := v.Kind()
switch k {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer,
reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return v.IsNil()
}
return false
}