goimpl

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

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

Go to latest
Published: Aug 11, 2017 License: MIT Imports: 13 Imported by: 0

README

goimpl Build Status

A tool to generate stub implementation of an interface.

The output is printed to stdout, errors (if any) — to stderr. ##Installation

go get github.com/sasha-s/goimpl/cmd/goimpl

In a nutshell

goimpl io.ReadWriteCloser "*pkg.impl"

Would print

package pkg

type impl struct{}

func (i *impl) Close() (err error) {
    return
}

func (i *impl) Read(u []uint8) (i1 int, err error) {
    return
}

func (i *impl) Write(u []uint8) (i1 int, err error) {
    return
}

Let's say you already have this (the Writer is almost http.ResponseWriter, but not quite):

package w12

import "net/http"

type Writer struct{}

func (w Writer) Write(float64) (error, *int) {
    return nil, nil
}

func (Writer) Header() http.Header {
    return nil
}

Running

goimpl -existing http.ResponseWriter "w12.Writer"

Gives

package w12

type Writer struct{}

// inputs[0]: had `float64` want `[]uint8`; outputs[0]: had `error` want `int`; outputs[1]: had `*int` want `error`
func (w Writer) Write(u []uint8) (i int, err error) {
	return
}

func (w Writer) WriteHeader(i int) {
	return
}

Here only the missing methods and the methods with wrong signature are generates.

Usage

Usage: goimpl [flags] [import1] [import2...] package.interfaceTypeName [(*|&)][package2.]typeName
This would generate empty implementation of the interfaceTypeName.
  -existing=false: Would trigger generation of missing method for the existing type(struct). Note, that if you want to use a pointer receiver prefix the type with '&'.
  -goimports=true: Run goimports on the generated code. The generated code might not compile if this is not set.
  -named=true: Generate named return values. The genrated code might not compile if this is not set.
Alternative(s)

impl

  • impl is parsing AST, goimpl is using reflection.
  • impl is much faster and has better error reporting.
  • goimpl generates complete code that (usually) compiles (package/ imports).
  • goimpl can generate a minimum update for the exising implementation.
  • goimpl can work with ambiguous package names.

Documentation

Overview

A tool to generate stub implementation of an interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(opts *GenOpts, out io.Writer) error

Generate an empty implementation of the interface as specified in opts and write the result to out.

Types

type Arg

type Arg struct {
	reflect.Type
	ArgName string // Name for a variable for this arg.
	Sep     string // Separator - empty if it the last arg in a list, comma otherwise.
}

Arg describes an argument of a method: either in or out.

type GenOpts

type GenOpts struct {
	PkgName             string              // target package.
	ImplName            string              // type (struct) that would implement the interface.
	Inter               reflect.Type        // Interface to implement.
	Existing            interface{}         // Existing type that we want to implement the interface.
	NoNamedReturnValues bool                // Do not generate named return values. The generated code might not compiple if this is set.
	MethodBlacklist     map[string]struct{} // Would not generate the code for those methods.
	Comments            map[string]string   // Add comments to those methods in generated code.
	NoGoImports         bool                // No goimports if set. Faster. The generated code might not compile.
	Extra               []string            // Extra imports.
}

GenOpts specifies code generation options.

func (*GenOpts) Clean

func (opts *GenOpts) Clean(s string) string

Clean keeps only letters.

func (GenOpts) First

func (GenOpts) First(s string) string

First returns a first letter of s in lowercase.

func (*GenOpts) GetName

func (opts *GenOpts) GetName(t reflect.Type) string

GetName of a type.

func (*GenOpts) Method

func (opts *GenOpts) Method(recName string, ft reflect.Method) Method

Method populates the Method struct. recName is a name of the receiver in the generated code.

func (*GenOpts) Methods

func (opts *GenOpts) Methods(it reflect.Type) []Method

Methods populates a list of methods for a given reflect type (which is supposed to be an interface).

func (*GenOpts) Short

func (opts *GenOpts) Short(t reflect.Type, cur map[string]struct{}) string

Short returns a unique (in the current scope) name for the argument of type t.

type Method

type Method struct {
	reflect.Method
	Inputs  []Arg
	Outputs []Arg
	Comment string
}

Method.

func (Method) Diff

func (m Method) Diff(other Method) string

Diff returns an empty string if the methods have same signature. Returns a description of the diff oftherwise.

Directories

Path Synopsis
cmd
goimpl
A command line tool to generate stub implementation of an interface.
A command line tool to generate stub implementation of an interface.

Jump to

Keyboard shortcuts

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