schnittstelle

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: MIT Imports: 12 Imported by: 0

README

schnittstelle

schnittstelle (german for "interface") is a CLI tool to generate an interface from the methods implemented for a struct in a source tree.

Installation

Simply compile and install the tool on your system using go install.

GOBIN=/usr/local/bin go install github.com/zekrotja/schnittstelle/v3/cmd/schnittstelle@latest

Alternatively, you can simply download latest binaries from the releases page or artifacts from latest CI builds.

Usage

Usage: schnittstelle --struct STRUCT [--root ROOT] [--interface INTERFACE] [--package PACKAGE] 
  [--out OUT] [--inject INJECT] [--import IMPORT] [--pool POOL] [--format]

Options:
  --struct STRUCT, -s STRUCT
                         Name of the struct
  --root ROOT, -r ROOT   Root directory [default: .]
  --interface INTERFACE, -i INTERFACE
                         The name of the result interface (name of struct when not specified)
  --package PACKAGE, -p PACKAGE
                         Package name ingested in output
  --out OUT, -o OUT      Output file (if not specified, output will be piped to Stdout)
  --inject INJECT        Inject code lines into the output code.
  --import IMPORT        Add import lines to the output.
  --pool POOL            Number of files which can be searched through simultaneously [default: 5]
  --format, -f           Format output using Gofmt.
  --help, -h             display this help and exit

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPathIsFile = errors.New("root path is not a directory")
)

Functions

func Assemble

func Assemble(
	interfaceName string,
	packageName string,
	inject []string,
	signatures []string,
	w io.Writer,
) (err error)

Assemble takes an array of method signatures and builds an interface with the defined interfaceName and writes it to w. If a packageName is specified, the package header with the given package name is added to the output. When inject is specified, the contets will be added to the output in between the package statement and the interface injection.

func Extract

func Extract(structName string, codePath string, poolSize int) ([]string, error)

Extract walks through all files recursively starting from codePath creating an index of Go source files which are no unit test files.

Then, it looks through all files line-by-line to find methods which have the given structName as receiver. This applies either to both receivers by value or by reference. Only methods which are exported are included in the result set.

The poolSize defines the number of files which can be opened and searched through simultaneously.

The resulting method signatures are then returned as an array of strings. The result set is sorted alphabetically.

func FindMethodsInFile

func FindMethodsInFile(filePath string, structName string) ([]string, error)

FindMethodsInFile reads the given file line by line to look for methods having the given structName as receiver and returns the list of results in an array.

Types

type Job

type Job func(workerId int, params ...interface{}) interface{}

Job defines a job function which will be executed in a worker getting passed the worker ID and parameters specified when pushing the job.

type MultiError

type MultiError []error

MultiError is an array of errors which can be handled and one error object.

func (MultiError) Error

func (t MultiError) Error() string

Error assembles a single error message out of all errors in the array.

If the array is empty, an empty string is returned.

func (MultiError) ToError

func (t MultiError) ToError() error

ToError returns nil if the length of the error array is 0 and returns the array if not.

This should be called before returning the MultiError in a function to ensure that subsequent nil checks perform as expected.

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

WorkerPool provides a simple "thread" pool implementation based on goroutines.

func NewWorkerpool

func NewWorkerpool(size int) *WorkerPool

NewWorkerpool creates a new instance of WorkerPool and spawns the defined number (size) of workers available waiting for jobs.

func (*WorkerPool) Close

func (w *WorkerPool) Close()

Close closes the Jobs channel so that the workers stop after executing all enqueued jobs. This is nessecary to be executed before WaitBlocking is called.

func (*WorkerPool) Push

func (w *WorkerPool) Push(job Job, params ...interface{})

Push enqueues a job with specified parameters which will be passed on executing the job.

func (*WorkerPool) Results

func (w *WorkerPool) Results() <-chan interface{}

Results returns the read-only results channel where executed job results are pushed in.

func (*WorkerPool) WaitBlocking

func (w *WorkerPool) WaitBlocking()

WaitBlocking blocks until all jobs are finished.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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