Code Monkey home page Code Monkey logo

goimpl's Introduction

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.

goimpl's People

Contributors

sasha-s avatar

Watchers

 avatar

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.