GithubHelp home page GithubHelp logo

impl's Introduction

impl generates method stubs for implementing an interface.

go install github.com/josharian/impl@latest

Sample usage:

$ impl 'f *File' io.ReadWriteCloser
func (f *File) Read(p []byte) (n int, err error) {
	panic("not implemented")
}

func (f *File) Write(p []byte) (n int, err error) {
	panic("not implemented")
}

func (f *File) Close() error {
	panic("not implemented")
}

# You can also provide a full name by specifying the package path.
# This helps in cases where the interface can't be guessed
# just from the package name and interface name.
$ impl 's *Source' golang.org/x/oauth2.TokenSource
func (s *Source) Token() (*oauth2.Token, error) {
    panic("not implemented")
}

You can use impl from Vim with vim-go or vim-go-impl

impl's People

Contributors

arp242 avatar artandreev avatar bhcleek avatar boz avatar danilvpetrov avatar dhowett avatar dmitshur avatar guseggert avatar jmoiron avatar josharian avatar kangkona avatar korylprince avatar paddycarver avatar philippgille avatar rhysd avatar sevki avatar tttoad avatar ysmolski avatar yuheitomi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

impl's Issues

Project Alive? I'm curious if it's possible to use this with go:generate and redirect to a file.go

I have other things being generated with go:generate and running go generate on the command line but when I try any of these they fail to redirect to a file.

When running these it still just prints to stdout and nothing is in the file.go

Just redirect to file.go
go:generate impl "l *TransferService" updater.TransferService > file.go
Redirect out/err to file.go
go:generate impl "l *TransferService" updater.TransferService 2> file.go 1>&2

Add TODO comment to each panic line

Some editor extensions highlight lines that contain TODO: and it's also great for searching code you need to work on (but didn't create an issue yet, or maybe have an issue but also want others who browse the code in their IDE and don't know about the ticket see that there's something to do).

So I think it would be creat to not just generate panic("not implemented") but:

panic("not implemented") // TODO: Implement

And in that regard, maybe even change "not implemented" to "not implemented yet", because usually when generating a struct for an interface, you're going to implement it.

Support imported interface parameters in the same package as the object

Example code:

package repo

type BB struct {
}

type IRepo interface {
	StramCall(r string) IRepo
}

type repo struct {
}

use "r *repo" IRepo will generate

func (r *repo) StramCall(r string) repo.IRepo {
	panic("not implemented") // TODO: Implement
}

Expect to get

func (r *repo) StramCall(rR string) IRepo { // The parameter name can be rR or rr 
	panic("not implemented") // TODO: Implement
}

If you agree with this, I can provide a PR for this feature.

Continuous generation via markup

I had an idea for how this could be integrated seamlessly into a workflow.

I would love to be able to put some kind of comment, markup, etc in a .go file and have impl generate the implementation from that.

For example, at the top of the file, I could put // impl 'f *File' io.ReadWriteCloser or similar and impl would generate the appropriate output for me.

As a valuable addition to that workflow, it would be fantastic if impl could examine the existing implementation and determine if it matches the interface. If not, it would generate any missing methods. This would allow me to, on save, call impl and have the appropriate methods generated for me, and if I have changed the interface since the last run, the new methods would be created.

The last value-add would be the ability for impl to update the arguments/return value of a method that exists but has been changed in the interface.

If all those things were in place, a simple on save run of impl against the current file would work all kinds of magic.

Note: I was going to break this into multiple issues for each piece of functionality, but I decided it would be better to open this as a discussion/question first. I'd love to hear your thoughts!

Thanks!

Add missing functions when running impl twice

It'd be awesome if impl could add the missing methods of an interface to a struct when executed twice. What it currently does is just duplicating all the methods.

If this makes sense I can try to craft a PR.

Unexported embedded interfaces break impl

⮀ impl 'msg diffRezMessage' logging.LogMessage
unrecognized interface: eachFielder

Where logging.LogMessage is like:

	// A LogMessage has structured data to report to a log (c.f. Deliver)
	LogMessage interface {
		DefaultLevel() Level
		Message() string
		eachFielder
	}

	eachFielder interface {
		EachField(FieldReportFn)
	}

Retain comments on interface

For easing making nice godocs, it would be nice if the comment above an interface type method specification was copied and placed above the matching function to make the stubs also for the godoc.

I will add this feature myself and make a PR shortly.

Generate default zero-value return values

First of all thanks for the tool! When I saw this I've immediately created a task to integrate it into vim-go: fatih/vim-go#122

Could it be possible to put the zero-value return values into the stub like:

func (t T) Write(p []byte) (n int, err error) {
    return 0, nil
}

This would allow the package or command to be compiled without any problem. Otherwise after generating the methods someone needs to go and fill the methods with appropriate return values.

Accept quoted package + interface input

Similar to gorename, so that people and tool authors can move comfortably from one tool to the next. We currently accept net/http.ResponseWriter; we should also accept "net/http".ResponseWriter.

Moved from the discussion in #12.

cc @shurcooL

Mishandles arguments that reuse types

See Seal and Open argument lists.

$ impl T cipher.AEAD
func (T) NonceSize() int {
    panic("not implemented")
}

func (T) Overhead() int {
    panic("not implemented")
}

func (T) Seal(dst []byte) []byte {
    panic("not implemented")
}

func (T) Open(dst []byte) ([]byte, error) {
    panic("not implemented")
}

$ godoc crypto/cipher AEAD
type AEAD interface {
    // NonceSize returns the size of the nonce that must be passed to Seal
    // and Open.
    NonceSize() int

    // Overhead returns the maximum difference between the lengths of a
    // plaintext and ciphertext.
    Overhead() int

    // Seal encrypts and authenticates plaintext, authenticates the
    // additional data and appends the result to dst, returning the updated
    // slice. The nonce must be NonceSize() bytes long and unique for all
    // time, for a given key.
    //
    // The plaintext and dst may alias exactly or not at all.
    Seal(dst, nonce, plaintext, data []byte) []byte

    // Open decrypts and authenticates ciphertext, authenticates the
    // additional data and, if successful, appends the resulting plaintext
    // to dst, returning the updated slice. The nonce must be NonceSize()
    // bytes long and both it and the additional data must match the
    // value passed to Seal.
    //
    // The ciphertext and dst may alias exactly or not at all.
    Open(dst, nonce, ciphertext, data []byte) ([]byte, error)
}
    AEAD is a cipher mode providing authenticated encryption with associated
    data.

func NewGCM(cipher Block) (AEAD, error)
    NewGCM returns the given 128-bit, block cipher wrapped in Galois Counter
    Mode.

Generate parameter names

When an interface definition leaves its method parameters anonymous, it would be nice if impl would generate a name for the variable.

This is granted non-trivial, since there are scoping problems - named results, the receiver, and other parameters might all collide.

Add support for generics

With the release of Go 1.18, interface types can now have type constraints. It would be neat to be able to specify these type constraints when generating the implementation.

Interfaces that embed imported interfaces break `impl`

I am attempting to generate the stubs for the Node interface but I get a not an interface: blocks.Block:

import (
	blocks "github.com/ipfs/go-block-format"
)

type Node interface {
	blocks.Block
	Resolver
// snipped for brevity
}

From the blocks package:

// Block provides abstraction for blocks implementations.
type Block interface {
	RawData() []byte
	Cid() *cid.Cid
	String() string
	Loggable() map[string]interface{}
}

Cannot find interface in other packages

Attempting to use impl from within the go-vim vim plugin. Command seems to fail when trying to find an interface from another package. Is this supported?

Example:

  /root
    main.go
    interface.go 
    subpackage/ 
         implementer.go 

When I'm in implementer.go I have my mouse over a struct that I want to stub out. I specify the fully qualified name of the root package including the $GOPATH/src path. Tool says unable to find interface in "root.Inteface"

v1.1.0 release

Hi there,

Would you please create a v1.1.0 release (or v1.0.1) to support fixes/features added since December 2020 (afab942)

Thanks!

Comment at end of file causes panic

You can reproduce this issue with the following main.go file:

package main

type T struct{}

type Interface interface {
	// Func comment
	Func()
}

// end comment

Then run:

$ impl 't T' Interface
panic: flattenCommentMap expects comment map of length 1

goroutine 1 [running]:
main.flattenCommentMap(0xc000015fb0?)
	/home/administrator/code/go/src/github.com/josharian/impl/impl.go:410 +0x44b
main.Pkg.funcsig({0xc000045000?, 0xc00007efc0?, {0xc00001e9c0?, 0xc00007eb80?}}, 0xc00007f0c0, 0xc00017beb8?, 0x1)
	/home/administrator/code/go/src/github.com/josharian/impl/impl.go:277 +0x1ab
main.funcs({0x7ffd5e6b88fa, 0x9}, {0xc00001e034, 0x9}, {0xc00001e9c0, 0x4}, 0x28?)
	/home/administrator/code/go/src/github.com/josharian/impl/impl.go:331 +0x3cb
main.main()
	/home/administrator/code/go/src/github.com/josharian/impl/impl.go:485 +0x2ad

Removing the last comment or putting any non-comment code after it causes the panic not to happen.

Make it easier to generate methods for package-local interfaces

It would be nice to be able to implement interfaces from the same package you are editing.

Say I have interface Client with a bunch of methods, and I want a struct to implement that interface. Right now I would have to write out the whole path, and also will have to edit the package name out of the parameters.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.