Code Monkey home page Code Monkey logo

lgen's Introduction

lgen

MIT License GolangCI

Description

Generate boilerplates for layered architecture by your templates.

At first, we prepare layered structure and templates.

$ tree templates
templates
├── repositories
│   └── repository.tmpl
├── controllers
│   └── controller.tmpl
└── usercases
    └── usecase.tmpl

For instance, the usecase template is below. templates are written with text/template.

// templates/usercase/usecase.tmpl
package usecase

type {{ .Action | title}}{{ .Model | title }}Input struct{}

type {{ .Action | title}}{{ .Model | title }}Result struct{}

type {{ .Action | title}}{{ .Model | title }}Usecase interface {
  Run({{ .Action | title}}{{ .Model | title }}Input) ({{ .Action | title}}{{ .Model | title }}Result, error)
}

func New{{ .Action | title}}{{ .Model | title }}Usecase() {{ .Action | title}}{{ .Model | title }}Usecase {
  return &{{ .Action }}{{ .Model | title }}Usecase{
  }
}

type {{ .Action }}{{ .Model | title }}Usecase struct {}

func (u *{{ .Action }}{{ .Model | title }}Usecase) Run(
    in {{ .Action | title}}{{ .Model | title }}Input,
  ) ({{ .Action | title}}{{ .Model | title }}Result, error){
  // Need to implement usercase logic
  return {{ .Action | title}}{{ .Model | title }}Result{
    // Need to build result
  }
}

Execute lgen with Action and Model strings.

$ lgen -action Get -model User -template ./testdata -dist myproduct

The generated directories and files are below.

$ tree myproduct
myproduct
├── repositories
│   └── get_user_repository.go
├── controllers
│   └── get_user_controller.go
└── usercases
    └── get_user_usecase.go

2 directories, 2 files

The get_user_usercase.go is below. We are enabled to write miltiple files by a command.

package usecase

type GetUserInput struct{}

type GetUserResult struct{}

type GetUserUsecase interface {
        Run(GetUserInput) (GetUserResult, error)
}

func NewGetUserUsecase() GetUserUsecase {
        return &getUserUsecase{}
}

type getUserUsecase struct{}

func (u *getUserUsecase) Run(
        in GetUserInput,
) (GetUserResult, error) {
        // Need to implement usercase logic
        return GetUserResult{
                // Need to build result
        }
}

Synopsis

$ lgen -a get -m user -template /your/templates/directory -dist /your/project/root/directory

Options

$ lgen -h
Usage of /var/folders/sy/ls4cfp216x774g54brzl67yw0000gn/T/go-build403725816/b001/exe/lgen:
  -a string
        action name
  -action string
        action name
  -d string
        output directory (default "./")
  -dist string
        output directory
  -m string
        model name
  -model string
        model name
  -t string
        templates directory (default "./templates")
  -template string
        templates directory
  -v    print version information and quit.
  -version
        print version information and quit.

Installation

$ go get github.com/budougumi0617/lgen

Built binaries are available on gihub releases. https://github.com/budougumi0617/lgen/releases

MacOS

If you want to install on MacOS, you can use Homebrew.

brew install budougumi0617/tap/lgen

Template rules

The template must be created according to the following rules.

  1. Base on text/template package.
  2. template file extension is only .tmpl.
  3. Varialbes are Action, and Model only.
  4. Functions are supported below:
    1. basic functions https://golang.org/pkg/text/template/#hdr-Functions
    2. title - strings.ToTitle

Contribution

  1. Fork (https://github.com/budougumi0617/lgen/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

License

MIT

Author

budougumi0617

lgen's People

Contributors

budougumi0617 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.