fm5

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

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

Go to latest
Published: Nov 25, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

README

fm5

CircleCI Maintainability Test Coverage

generate factory method utilities for golang.

Getting Started

Prerequisites
  • Go 1.11
  • make
Installing
$ go get -u github.com/akito0107/fm5/cmd/fm5
How to use
  1. You declare struct which has some member variables.
type Structure struct {
	id   int
	name string
}
  1. Generate factory methods with passing the type name.
$ fm5 -t Structure
  1. Then, you can get file which named Structure_fm.go. This file contains the function which identifies whether given error is the one that we are defined before.
// Code generated by "fm5"; DO NOT EDIT.
package e2e

func NewStructure(id int, name string) *Structure {
	return &Structure{id: id, name: name}
}
  1. You can also generate functional options pattern with -fo option.
$ fm5 -t Structure -fo
// Code generated by "fm5"; DO NOT EDIT.
package e2e

func NewStructure(id int, name string) *Structure {
	return &Structure{id: id, name: name}
}

type StructureOption func(*Structure)

func NewStructureOptions(opts ...StructureOption) *Structure {
	i := &Structure{}
	for _, o := range opts {
		o(i)
	}
	return i
}
func WithId(id int) StructureOption {
	return func(i *Structure) {
		i.id = id
	}
}
func WithName(name string) StructureOption {
	return func(i *Structure) {
		i.name = name
	}
}
  1. You can also customize return type with -r option. This feature convenient for returning interface type.
$ fm -t Structure -fo -r Interface
// Code generated by "fm5"; DO NOT EDIT.
package e2e

func NewStructure(id int, name string) Interface {
	return &Structure{id: id, name: name}
}

type StructureOption func(*Structure)

func NewStructureOptions(opts ...StructureOption) Interface {
	i := &Structure{}
	for _, o := range opts {
		o(i)
	}
	return i
}
func WithId(id int) StructureOption {
	return func(i *Structure) {
		i.id = id
	}
}
func WithName(name string) StructureOption {
	return func(i *Structure) {
		i.name = name
	}
}

Options

$ fm5 --help
NAME:
   fm5 - factory method generator

USAGE:
   fm5 [OPTIONS]

VERSION:
   0.0.0

COMMANDS:
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --type value, -t value                       struct name (required)
   --dryrun                                     dryrun (default=false)
   --factory-method, --fm                       generate default factory method(default=true)
   --factory-method-name value, --fmn value     factory method name(default=New   $typename)
   --functional-option, --fo                    generate functional option patterns methods(default=false)
   --functional-option-name value, --fon value  functional option method name(New   $typename   Options)
   --return-typename value, -r value            return typename (if present, applied this type, otherwise, using pointer type of given type)
   --help, -h                                   show help
   --version, -v                                print the version

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

Documentation

Overview

Code generated by "generr"; DO NOT EDIT.

Code generated by "generr"; DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotStructType

func IsNotStructType(err error) (bool, string)

func IsTypeNotFoundWithGivenName

func IsTypeNotFoundWithGivenName(err error) (bool, string)

func Parse

func Parse(r io.Reader, typename string) (string, *ast.TypeSpec, error)

Types

type Generator

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

func NewGenerator

func NewGenerator(pkgName string, typeName string, ts *ast.TypeSpec) *Generator

func (*Generator) AppendDefaultFactory

func (g *Generator) AppendDefaultFactory(methodname, outputtypename string) error

func (*Generator) AppendFunctionalOptionType

func (g *Generator) AppendFunctionalOptionType(methodname, outputtypename string) error

func (*Generator) AppendFunctionalOptions

func (g *Generator) AppendFunctionalOptions() error

func (*Generator) AppendPackage

func (g *Generator) AppendPackage()

func (*Generator) Generate

func (g *Generator) Generate() error

func (*Generator) Out

func (g *Generator) Out(w io.Writer) error

type NotStructType

type NotStructType struct {
	Typename string
}

func (*NotStructType) Error

func (e *NotStructType) Error() string

func (*NotStructType) NotStructType

func (e *NotStructType) NotStructType() string

type TypeNotFoundWithGivenName

type TypeNotFoundWithGivenName struct {
	Name string
}

func (*TypeNotFoundWithGivenName) Error

func (e *TypeNotFoundWithGivenName) Error() string

func (*TypeNotFoundWithGivenName) TypeNotFoundWithGivenName

func (e *TypeNotFoundWithGivenName) TypeNotFoundWithGivenName() string

Directories

Path Synopsis
cmd
fm5
Code generated by "fmgo"; DO NOT EDIT.
Code generated by "fmgo"; DO NOT EDIT.

Jump to

Keyboard shortcuts

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