- Notifications
You must be signed in to change notification settings - Fork4
Accept Interfaces, Return Concrete Types
License
butuzov/ireturn
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Accept Interfaces, Return Concrete Types
You can getireturn withgo install command. Go1.23+ required.
go install github.com/butuzov/ireturn/cmd/ireturn@latest
Or you can download the suitable binary from thereleases section.
ireturn work with two arguments (but allow to use of only one of them in the same moment):
accept- accept-list of the comma-separated interfaces.reject- reject-list of the comma-separated interfaces.
By default,ireturn will accept all errors (error), empty interfaces (interfaces{}), anonymous interfaces declarations (interface { methodName() } ) and interfaces from standard library as a valid ones.
Interfaces in the list can be provided as regexps or keywords ("error" for "error", "empty" forinterface{},anon for anonymous interfaces):
# allow usage of empty interfaces, errors and Doer interface from any package.ireturn --accept="\\.Doer,error,empty" ./...# reject standard library interfaces and plinko.Payload as valid onesireturn --reject="std,github.com/shipt/plinko.Payload" ./...# default settings allows errors, empty interfaces, anonymous declarations and standard libraryireturn ./...# checkfor non idiomatic interface namesireturn -allow="error,generic,anon,stdlib,.*(or|er)$" ./...
You can use shorthand for some types of interfaces:
emptyforinterface{}typeanonfor anonymous declarationsinterface{ someMethod() }errorforerrortypestdlibfor all interfaces from standard library.genericfor generic interfaces (added in go1.18)
golangci-lint compliant disable directive//nolint:ireturn can be used withireturn
- uses: butuzov/ireturn-linter@main with: allow: "error,empty"// Bad.typeDoerinterface {Do() }typeIDoerstruct{}funcNew()Doer {returnnew(IDoer)}func (d*IDoer)Do() {/*...*/}// Good.typeDoerinterface {Do() }typeIDoerstruct{}funcNew()*IDoer {returnnew(IDoer)}func (d*IDoer)Do() {/*...*/}// Very Good (Verify Interface Compliance in compile time)var_Doer= (*IDoer)(nil)typeDoerinterface {Do() }typeIDoerstruct{}funcNew()*IDoer {returnnew(IDoer)}func (d*IDoer)Do() {/*...*/}
About
Accept Interfaces, Return Concrete Types
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.