Skip to content

Commit

Permalink
Merge pull request #1 from bakito/main
Browse files Browse the repository at this point in the history
use generics

Signed-off-by: Cyrill Näf <cyrill.naef@gmail.com>
  • Loading branch information
BigGold1310 committed Jul 22, 2024
2 parents 6bc8a6a + c8511b6 commit deaef79
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 24 deletions.
8 changes: 2 additions & 6 deletions resource/composed/composed.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ func New() *Unstructured {
}

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

// Get known GVKs for the runtime object type
knownGVKs, _, err := Scheme.ObjectKinds(rt)
knownGVKs, _, err := Scheme.ObjectKinds(obj)
if err != nil {
return errors.Errorf("could not retrieve GVKs for the provided object: %v", err)
}
Expand Down
19 changes: 1 addition & 18 deletions resource/composed/composed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestTo(t *testing.T) {
v1beta1.AddToScheme(Scheme)
type args struct {
un *Unstructured
obj interface{}
obj runtime.Object
}
type want struct {
obj interface{}
Expand Down Expand Up @@ -316,23 +316,6 @@ func TestTo(t *testing.T) {
err: errors.New("GVK /test.example.io, Kind=Unknown is not known by the scheme for the provided object type"),
},
},
"NoRuntimeObject": {
reason: "Should only convert to a object if the object is a runtime.Object",
args: args{
un: &Unstructured{Unstructured: unstructured.Unstructured{Object: map[string]any{
"apiVersion": v1beta1.CRDGroupVersion.String(),
"kind": v1beta1.Bucket_Kind,
"metadata": map[string]any{
"name": "cool-bucket",
},
}}},
obj: "not-a-runtime-object",
},
want: want{
obj: string("not-a-runtime-object"),
err: errors.New("object is not a compatible runtime.Object"),
},
},
}

for name, tc := range cases {
Expand Down

0 comments on commit deaef79

Please sign in to comment.