I am having a question about how to implement interfaces correctly in Go when it comes to third-party packages that use chained methods. I have compiled an example project below for you so that you can understand the problem.
As you can see, the package has a chained method
Logical().Write() . Since I want to create tests for
PublicFunctionIWantToTest, I want to pass down all the functionality as interface so that I can use
https://vektra.github.io/mockery/ to create mocks for it.
Unfortunately, I am hitting an issue with my
MyAPIClient and the
MyAPILogical interface. Since I can see in the package's documentation (
https://pkg.go.dev/github.com/hashicorp/vault/a...@v1.8.1#Logical.Write) that the
Logical() method returns a
Logical instance, I want to make it so that interface method returns the other interface
MyAPILogical (line 15). This does not work though, there is an error on line 47 in the
GetClient() method saying that I would not implement the interface correctly. How could I do that?
cannot use myClient (variable of type *api.Client) as MyAPIClient value in return statement: *api.Client does not implement MyAPIClient (wrong type for method Logical)
have Logical() *api.Logical
want Logical() MyAPILogicalcompilerInvalidIfaceAssignThank you kindly