gen-options

command module
v0.0.0-...-5db323b Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2016 License: Apache-2.0 Imports: 17 Imported by: 0

README

Overview

gen-options is a small go generate tool. The project is inspired by gen-mocks.

Suppose you have a struct looks like

type Foo struct {
     fstField int
     sndField string
}

and if you want to initialize the struct using dependency injection like this

func NewFoo(options ...func(*Foo)) {
     foo := &Foo{}
     for _, o := range options {
     	 o(foo)
     }
}

then you need to define one function per field

func OptionFirstField(int x) func(*Foo) {
     return func(f *Foo) {
     	    f.fstField = x
     }
}

gen-options can help generate these functions for you.

Usage

install: go get github.com/showbufire/gen-options

help: gen-options -h

common usage: gen-options -s Foo -w

By default, for each field named bar, it will generate a function OptionBar. You can tweak the default behavior, by

  • feed a -f=Prefix option. The generated function will be PrefixBar.
  • add an options field tag to opt in for function generation. For example, if bar string `options:"Baz"` , then the generated will be OptionBaz. If it's options:"", then the generated will be OptionBar.
  • no function will be generated if options field tag is missing.
  • comments will be copied from the field to the generated function with the function putting in front.

By default, it will generate a type alias type Option func(*YourStruct), you can turn it off or change to another name by using the -t option: -t="" or -t="SomeOption".

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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