akito0107 / fm5

factory method generator for golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

factory method generator for golang

License:Apache License 2.0


Languages

Language:Go 96.0%Language:Makefile 4.0%