Skip to content

Commit 92b9428

Browse files
bakitoBigGold1310
authored andcommitted
use generics
Signed-off-by: bakito <[email protected]>
1 parent 6bc8a6a commit 92b9428

File tree

2 files changed

+3
-24
lines changed

2 files changed

+3
-24
lines changed

resource/composed/composed.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,10 @@ func New() *Unstructured {
4343
}
4444

4545
// To converts a unstructured composed resource to the provided object.
46-
func To(un *Unstructured, obj interface{}) error {
47-
rt, ok := obj.(runtime.Object)
48-
if !ok {
49-
return errors.New("object is not a compatible runtime.Object")
50-
}
46+
func To[T runtime.Object](un *Unstructured, obj T) error {
5147

5248
// Get known GVKs for the runtime object type
53-
knownGVKs, _, err := Scheme.ObjectKinds(rt)
49+
knownGVKs, _, err := Scheme.ObjectKinds(obj)
5450
if err != nil {
5551
return errors.Errorf("could not retrieve GVKs for the provided object: %v", err)
5652
}

resource/composed/composed_test.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func TestTo(t *testing.T) {
241241
v1beta1.AddToScheme(Scheme)
242242
type args struct {
243243
un *Unstructured
244-
obj interface{}
244+
obj runtime.Object
245245
}
246246
type want struct {
247247
obj interface{}
@@ -316,23 +316,6 @@ func TestTo(t *testing.T) {
316316
err: errors.New("GVK /test.example.io, Kind=Unknown is not known by the scheme for the provided object type"),
317317
},
318318
},
319-
"NoRuntimeObject": {
320-
reason: "Should only convert to a object if the object is a runtime.Object",
321-
args: args{
322-
un: &Unstructured{Unstructured: unstructured.Unstructured{Object: map[string]any{
323-
"apiVersion": v1beta1.CRDGroupVersion.String(),
324-
"kind": v1beta1.Bucket_Kind,
325-
"metadata": map[string]any{
326-
"name": "cool-bucket",
327-
},
328-
}}},
329-
obj: "not-a-runtime-object",
330-
},
331-
want: want{
332-
obj: string("not-a-runtime-object"),
333-
err: errors.New("object is not a compatible runtime.Object"),
334-
},
335-
},
336319
}
337320

338321
for name, tc := range cases {

0 commit comments

Comments
 (0)