Understanding interface inside interface(Embedded Interface)
Asked Answered
G

1

9

I was trying to understand the Interface embedding with the following code.

I have the following:

type MyprojectV1alpha1Interface interface {
    RESTClient() rest.Interface
    SamplesGetter
}

// SamplesGetter has a method to return a SampleInterface.
// A group's client should implement this interface.
type SamplesGetter interface {
    Samples(namespace string) SampleInterface
}

// SampleInterface has methods to work with Sample resources.
type SampleInterface interface {
    Create(*v1alpha1.Sample) (*v1alpha1.Sample, error)
    Update(*v1alpha1.Sample) (*v1alpha1.Sample, error)
    Delete(name string, options *v1.DeleteOptions) error
    DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error
    Get(name string, options v1.GetOptions) (*v1alpha1.Sample, error)
    List(opts v1.ListOptions) (*v1alpha1.SampleList, error)
    Watch(opts v1.ListOptions) (watch.Interface, error)
    Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1alpha1.Sample, err error)
    SampleExpansion
}

Now if I have the follwoing:

func returninterface() MyprojectV1alpha1Interface {
//does something and returns me MyprojectV1alpha1Interface
}
temp := returninterface()

Now, from the MyprojectV1alpha1Interface if I want to call the

Create function of SampleInterface

what I need to do?

Also, please explain me how this interfaces work in Golang.

Gustave answered 1/7, 2018 at 3:1 Comment(0)
M
13

In this definition:

type MyprojectV1alpha1Interface interface {
    RESTClient() rest.Interface
    SamplesGetter
}

Your MyprojectV1alpha1Interface embeds the SamplesGetter interface.

Embedding an interface inside another interface means all of the methods of the embedded interface (SamplesGetter) can be invoked over the embedding interface (MyprojectV1alpha1Interface).

That means you can invoke any of the SamplesGetter methods on any object that implements MyprojectV1alpha1Interface.

So once you get a MyprojectV1alpha1Interface object in your temp variable, you can call the Samples method (with suitable namespace, which I cannot guess from the code you posted):

sampleInt := temp.Samples("namespace here")

sampleInt will then have a SampleInterface object, so you can then invoke the Create function using your sampleInt variable:

sample, err := sampleInt.Create(<you should use a *v1alpha1.Sample here>)

For more details about how interfaces work, I'd suggest you go to the official specs and examples:

https://golang.org/ref/spec#Interface_types

https://gobyexample.com/interfaces

Mesial answered 1/7, 2018 at 3:20 Comment(3)
Awesome explanation, thanks a lot. I had a understanding of interfaces. But, got confused with interface embedding. But, now its crystal clear. ThanksGustave
This is very good explanation @eugenioy. But does it mean embedding interfaces into another could pose the risks?Braunschweig
not sure what you mean by risks @jack_t, embedding is a way to simplify composition in GoMesial

© 2022 - 2024 — McMap. All rights reserved.