Skip to content

Commit

Permalink
goor's first design commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ttakuya50 committed May 23, 2020
1 parent fa190af commit f6286b7
Show file tree
Hide file tree
Showing 15 changed files with 939 additions and 1 deletion.
54 changes: 54 additions & 0 deletions .github/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# https://github.com/golangci/golangci-lint#config-file

linters-settings: # 各linterの詳細設定
govet:
check-shadowing: true
golint:
min-confidence: 0.8 # 数値が低いほど厳しいlint
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
errcheck:
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false

# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: false

linters:
disable-all: true
enable:
- deadcode # 未使用のコードを検出
- errcheck # チェックされていない error の検出
- goconst # 定数に置き換えられるコードがないかチェックする
- gocyclo # コードの循環的複雑度をチェックする
- gofmt # 公式
- goimports # 公式
- golint # 公式
- gosimple # より簡潔に書けるコードがないかチェックする
- govet # 公式
- ineffassign # 値が未使用なまま上書きされている変数がないかチェックする
- misspell # 英単語のスペルに誤りがないかチェックする
- nakedret # Named Result Parametersによるreturnをしていないかチェックする
- staticcheck # 誤った動作をするコードがないかチェックする(https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck)
- structcheck # 未使用の構造体フィールドを検出
- unconvert # 不必要なキャストがないかチェックする
- unparam # 未使用の引数がないかチェックする
- unused # 未使用の変数、型、フィールドがないかチェックする
- varcheck # 未使用のグローバル変数と定数を検出

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
issues:
exclude-use-default: false
max-same-issues: 0

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true
26 changes: 26 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: goreleaser

on:
push:
tags:
- '*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20 changes: 20 additions & 0 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: reviewdog

on:
pull_request:
types: [opened,synchronize]

jobs:
golangci-lint-github-pr-review:
name: runner / golangci-lint (github-pr-review)
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: golangci-lint w/ github-pr-review
uses: reviewdog/action-golangci-lint@v1
with:
github_token: ${{ secrets.github_token }}
golangci_lint_flags: "--config=.github/.golangci.yml"
tool_name: "golangci-lint-github-pr-review"
reporter: "github-pr-review"
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# CLI name
goor
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

build:
go build -v -o goor

lint:
docker run --rm -v $(MAKEFILE_DIR):/app -w /app golangci/golangci-lint:v1.27.0 golangci-lint run --config=.github/.golangci.yml
260 changes: 259 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,259 @@
# goor
# goor
This is a CLI for creating constructors, getters and setters for structures, and is only for Golang.

# Installation

```shell script
$ go get -u github.com/ttakuya50/goor
```
If you want the direct binary data, please click [here](https://github.com/ttakuya50/goor/releases).

# Usage

```shell script
Usage of goor:
goor [flags] -type=[struct type name]
For more information, see:
https://github.com/ttakuya50/goor
Flags:
-getter
[option] when you create a constructor, you also create a getter.
-output string
[option] output file name (default "srcdir/<type>_constructor_gen.go").
-pointer
[option] set the return value to a pointer when creating the constructor. (default true)
-setter
[option] when you create a constructor, you also create a setter.
-type string
[required] a struct type name.
-version
outputs the current version.
```

Frequently used settings are listed below.
## 1. Create constructor.

```go
//go:generate goor -type=User
type User struct {
id int
name string
age int
}
```

```shell script
$ go generate ./...
```

A file named user_constructor_gen.go will be created.
Constructor is generated.
```go
func NewUser(
id int,
name string,
age age,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}
```

## 2. Create constructor and getter.

```go
//go:generate goor -type=User -getter
type User struct {
id int
name string
age int
}
```

```shell script
$ go generate ./...
```

A file named user_constructor_gen.go will be created.
Constructor and getter are generated.
```go
func NewUser(
id int,
name string,
age int,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}

func (g *User) Id() int {
return g.id
}

func (g *User) Name() string {
return g.name
}

func (g *User) Age() int {
return g.age
}
```

## 3. Create constructor and getter and setter.

```go
//go:generate goor -type=User -getter -setter
type User struct {
id int
name string
age int
}
```

```shell script
$ go generate ./...
```

A file named user_constructor_gen.go will be created.
Constructor and getter and setter are generated.
```go
func NewUser(
id int,
name string,
age int,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}

func (g *User) Id() int {
return g.id
}

func (g *User) Name() string {
return g.name
}

func (g *User) Age() int {
return g.age
}

func (s *User) SetId(id int) {
s.id = id
}

func (s *User) SetName(name string) {
s.name = name
}

func (s *User) SetAge(age int) {
s.age = age
}
```

## 4. It is also possible to exclude certain structure variables.

```go
//go:generate goor -type=User
type User struct {
id int `goor:"constructor:-"`
name string
age int
}
```

```shell script
$ go generate ./...
```

It is also possible to exclude certain structure variables.
```go
func NewUser(
name string,
age int,
) *User {
return &User{
name: name,
age: age,
}
}
```

## 5. It is also possible to exclude the constructor, getter and setter of a specific structure variable.

```go
//go:generate goor -type=User -getter -setter
type User struct {
id int `goor:"getter:-"`
name string `goor:"constructor:-;setter:-"`
age int `goor:"constructor:-;getter:-;setter:-"`
}
```

```shell script
$ go generate ./...
```

The following code will be generated on its own.
```go
func NewUser(
id int,
) *User {
return &User{
id: id,
}
}

func (g *User) Name() string {
return g.name
}

func (s *User) SetId(id int) {
s.id = id
}
```

## 5. Change the name of the output file.

```go
//go:generate goor -type=User -output=user_gen.go
type User struct {
id int
name string
age int
}
```

```shell script
$ go generate ./...
```

The following code will be generated in the file named user_gen.go
```go
func NewUser(
id int,
name string,
age int,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}
```

# Examples
Check out the examples of how to generate code using goor in the examples directory.

# Author
[t_takuya50](https://twitter.com/t_takuya50)

0 comments on commit f6286b7

Please sign in to comment.