impl

package module
v0.0.0-...-c8bff90 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2020 License: MIT Imports: 13 Imported by: 0

README

impl

A library and a command line that intelligently implements Go interfaces for any given type.

Features:
  • Go Modules aware
  • Only adds the missing methods
  • Reports an error if a method with a conflicting type signature already exists
  • Adds "import" declarations to the file if any of the interface methods require it
  • Recursively implement methods in embedded interfaces
  • Adjusts the method function signature based on imports, such as replacing *models.Person with *Person if the target is in the "models" package already.
  • Understands "." imports as well as "_" named imports
Install

As a command line tool:

go get marwan.io/impl/cmd/impl

As a library:

go get marwan.io/impl

Usage (command line)

Given a type MyType struct {} definition in github.com/my/pkg, you can run the following command:

impl -iface=io.Writer -impl=github.com/my/pkg.MyType

And whichever file MyType is defined in will have func (*MyType) Write(p []byte) (int, error) { panic("unimplemented) }

Similar to gofmt, results will be printed to stdout by default. If you'd like to persist the file instead, then pass the -w flag.

For other options such as json output for tooling, see impl --help.

Usage (library)
import "marwan.io/impl"

resp, err := impl.Implement("io", "Writer", "github.com/my/pkg", "MyType")
// resp.File is the file where MyType was defined
// resp.FileContent is the new content of the entire file that has the Writer method
// resp.Methods is the Go code of all the newly add methods
// resp.AddedImports denotes all new import statements to the concrete type file
// resp.AllImports denotes all import paths of that same file.
// err must be handled
List Available Interfaces

The library can list available interfaces given any import path

impl list -json
["mypkg.MyInterface", "io.Writer", "io.Closer"] # etc

impl list io -json
# ...

Documentation

Overview

Package impl can generate method stubs to implement a given interface For usage and installation, see README file

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListInterfaces

func ListInterfaces(path string) ([]string, error)

ListInterfaces ...

Types

type AddedImport

type AddedImport struct {
	Name, Path string
}

AddedImport represents a newly added import statement to the concrete type. If name is not empty, then that import is required to have that name.

type Implementation

type Implementation struct {
	File         string         // path to the Go file of the implementing type
	FileContent  []byte         // the Go file plus the method implementations at the bottom of the file
	Methods      []byte         // only the method implementations, helpful if you want to insert the methods elsewhere in the file
	AddedImports []*AddedImport // all the required imports for the methods, it does not filter out imports already imported by the file
	AllImports   []*AddedImport // convenience to get a list of all the imports of the concrete type file
	Error        error          // any error encountered during the process
}

Implementation defines the results of the implement method

func Implement

func Implement(ifacePath, iface, implPath, impl string) (*Implementation, error)

Implement an interface and return the path to as well as the content of the file where the concrete type was defined updated with all of the missing methods

Directories

Path Synopsis
cmd
test_data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL