Skip to content

Commit e508a12

Browse files
authored
Merge pull request #82 from ecordell/fix-typed-indexer
2 parents 36e5882 + ddc1c42 commit e508a12

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

typed/typedindexer.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,37 @@ func (t Indexer[K]) ListKeys() []string {
4040
}
4141

4242
func (t Indexer[K]) Get(obj K) (item K, exists bool, err error) {
43-
var typedObj *K
43+
var zero K
4444
gotItem, gotExists, gotErr := t.indexer.Get(obj)
45-
if err != nil || !gotExists {
46-
return *typedObj, gotExists, gotErr
45+
if gotErr != nil || !gotExists {
46+
return zero, gotExists, gotErr
4747
}
4848
gotRObj, ok := gotItem.(runtime.Object)
4949
if !ok {
50-
return *typedObj, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
50+
return zero, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
5151
}
5252

5353
gotTypedObj, err := UnstructuredObjToTypedObj[K](gotRObj)
5454
if err != nil {
55-
return *typedObj, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, *typedObj)
55+
return zero, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, zero)
5656
}
5757
return gotTypedObj, gotExists, gotErr
5858
}
5959

6060
func (t Indexer[K]) GetByKey(key string) (item K, exists bool, err error) {
61-
var typedObj *K
61+
var zero K
6262
gotItem, gotExists, gotErr := t.indexer.GetByKey(key)
63-
if err != nil || !gotExists {
64-
return *typedObj, gotExists, gotErr
63+
if gotErr != nil || !gotExists {
64+
return zero, gotExists, gotErr
6565
}
6666
gotRObj, ok := gotItem.(runtime.Object)
6767
if !ok {
68-
return *typedObj, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
68+
return zero, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
6969
}
7070

7171
gotTypedObj, err := UnstructuredObjToTypedObj[K](gotRObj)
7272
if err != nil {
73-
return *typedObj, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, *typedObj)
73+
return zero, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, zero)
7474
}
7575
return gotTypedObj, gotExists, gotErr
7676
}

typed/typedindexer_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package typed
33
import (
44
"context"
55
"fmt"
6+
"testing"
67

78
corev1 "k8s.io/api/core/v1"
89
"k8s.io/apimachinery/pkg/runtime"
@@ -25,3 +26,26 @@ func ExampleIndexer() {
2526
fmt.Printf("%T", secrets)
2627
// Output: []*v1.Secret
2728
}
29+
30+
func TestGetByKeyErrorHandling(t *testing.T) {
31+
ctx, cancel := context.WithCancel(context.Background())
32+
defer cancel()
33+
34+
client := fake.NewSimpleDynamicClient(runtime.NewScheme())
35+
informerFactory := dynamicinformer.NewDynamicSharedInformerFactory(client, 0)
36+
informerFactory.Start(ctx.Done())
37+
informerFactory.WaitForCacheSync(ctx.Done())
38+
39+
indexer := NewIndexer[*corev1.Secret](informerFactory.ForResource(corev1.SchemeGroupVersion.WithResource("secrets")).Informer().GetIndexer())
40+
41+
secret, exists, err := indexer.GetByKey("nonexistent/key")
42+
if secret != nil {
43+
t.Errorf("Expected nil secret for non-existent key, got %v", secret)
44+
}
45+
if exists {
46+
t.Errorf("Expected exists to be false for non-existent key")
47+
}
48+
if err != nil {
49+
t.Errorf("Expected no error for non-existent key, got %v", err)
50+
}
51+
}

0 commit comments

Comments
 (0)