Giter VIP home page Giter VIP logo

fm5's Introduction

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

fm5's People

Contributors

akito0107 avatar orisano avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

orisano

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.