-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
200 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,13 +49,13 @@ See [more examples here](https://github.com/Bin-Huang/newc/tree/master/test) | |
Without manual installation, just add this comment line to the struct. Go will automatically install this tool if missing. | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].1 | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
``` | ||
|
||
For example: | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].1 | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
type UserService struct { | ||
baseService | ||
userRepository *repositories.UserRepository | ||
|
@@ -65,6 +65,30 @@ type UserService struct { | |
|
||
This is very useful, especially in teamwork. **It can run without manual installation. It doesn't break the work of other people who don't have installed this tool in collaboration.** | ||
|
||
## Return value instead reference | ||
|
||
Add `--value` parameter | ||
|
||
```go | ||
//go:generate newc --value | ||
type Config struct { | ||
debug bool | ||
} | ||
``` | ||
|
||
Generated code: | ||
|
||
```go | ||
// constructor_gen.go | ||
|
||
// NewConfig Create a new Config | ||
func NewConfig(debug bool) Config { | ||
return Config{ | ||
debug: debug, | ||
} | ||
} | ||
``` | ||
|
||
## Call an initializer | ||
|
||
1. Add `--init` parameter | ||
|
@@ -123,7 +147,7 @@ Don't worry about the imports, variable naming, and code style in the generated | |
It doesn't break the work of other people who don't have installed this tool in collaboration. Go will automatically install this tool if missing. | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].1 | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
``` | ||
|
||
## Sponsoring | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,13 +49,13 @@ func NewUserService(baseService baseService, userRepository *repositories.UserRe | |
无需手动安装,只需要给结构体添加下面这行注释就行。Go 会在缺失时自动下载这个工具。 | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].1 | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
``` | ||
|
||
比如这样: | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].1 | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
type UserService struct { | ||
baseService | ||
userRepository *repositories.UserRepository | ||
|
@@ -65,6 +65,30 @@ type UserService struct { | |
|
||
这个方式非常有用,尤其在团队开发中。**就算其他同事没有安装这个工具,这么做也能正常运行,不会影响到其他人的工作**。 | ||
|
||
## 返回结构体的值,而不是引用 | ||
|
||
使用 `--value` 参数 | ||
|
||
```go | ||
//go:generate newc --value | ||
type Config struct { | ||
debug bool | ||
} | ||
``` | ||
|
||
生成代码: | ||
|
||
```go | ||
// constructor_gen.go | ||
|
||
// NewConfig Create a new Config | ||
func NewConfig(debug bool) Config { | ||
return Config{ | ||
debug: debug, | ||
} | ||
} | ||
``` | ||
|
||
## 想在构造时做些初始化? | ||
|
||
1. 加上 `--init` 参数 | ||
|
@@ -123,7 +147,7 @@ func NewController(logger *zap.Logger, debug bool) *Controller { | |
就算其他同事没有安装这个工具,这么做也不会影响到他们的工作。因为 Go 会在必要时自动安装这个工具。 | ||
|
||
```go | ||
//go:generate go run github.com/Bin-Huang/[email protected].1 | ||
//go:generate go run github.com/Bin-Huang/[email protected].2 | ||
``` | ||
|
||
## 赞赏 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
package config | ||
|
||
// Config ... | ||
//go:generate go run ../../../newc --value | ||
type Config struct { | ||
Debug bool | ||
} | ||
|
||
// DebugConfig ... | ||
type DebugConfig struct { | ||
Debug bool | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package unittest | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRefMode(t *testing.T) { | ||
value := NewStructRef(false) | ||
typeName := reflect.TypeOf(value).String() | ||
if typeName != "*unittest.StructRef" { | ||
t.Errorf("expected *unittest.StructRef, but got %v", typeName) | ||
} | ||
} | ||
|
||
func TestValueMode(t *testing.T) { | ||
value := NewStructValue(false) | ||
typeName := reflect.TypeOf(value).String() | ||
if typeName != "unittest.StructValue" { | ||
t.Errorf("expected unittest.StructValue, but got %v", typeName) | ||
} | ||
} | ||
|
||
func TestInitMode(t *testing.T) { | ||
value := NewStructWithInit(false) | ||
if value.Debug != true { | ||
t.Errorf("NewStructWithInit should calling init method") | ||
} | ||
} | ||
|
||
func TestValueInitMode(t *testing.T) { | ||
value := NewStructValueWithInit(false) | ||
if value.Debug != true { | ||
t.Errorf("NewStructValueWithInit should calling init method") | ||
} | ||
typeName := reflect.TypeOf(value).String() | ||
if typeName != "unittest.StructValueWithInit" { | ||
t.Errorf("expected unittest.StructValueWithInit, but got %v", typeName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package unittest | ||
|
||
// StructRef ... | ||
//go:generate go run ../../../newc | ||
type StructRef struct { | ||
Debug bool | ||
} | ||
|
||
// StructValue ... | ||
//go:generate go run ../../../newc --value | ||
type StructValue struct { | ||
Debug bool | ||
} | ||
|
||
// StructWithInit ... | ||
//go:generate go run ../../../newc --init | ||
type StructWithInit struct { | ||
Debug bool | ||
} | ||
|
||
func (s *StructWithInit) init() { | ||
s.Debug = true | ||
} | ||
|
||
// StructValueWithInit ... | ||
//go:generate go run ../../../newc --value --init | ||
type StructValueWithInit struct { | ||
Debug bool | ||
} | ||
|
||
func (s *StructValueWithInit) init() { | ||
s.Debug = true | ||
} |