Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal/params pacakge test #474

Merged
merged 29 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3300cf5
feat: add params pacakge test
hlts2 Jun 15, 2020
4166c2c
feat: add comment for the godoc
hlts2 Jun 15, 2020
b44eb3c
fix: apply suggestion
hlts2 Jun 15, 2020
e1e1446
fix: apply suggestion
hlts2 Jun 16, 2020
207ee49
fix: added document about unused variables
hlts2 Jun 16, 2020
d392959
fix: apply suggestion
hlts2 Jun 16, 2020
35b8165
fix: apply suggestion
hlts2 Jun 16, 2020
6320312
fix: apply suggestion
hlts2 Jun 16, 2020
7f94273
fix: apply suggestion
hlts2 Jun 16, 2020
d677d82
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
e0e0bea
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
7834ea1
fix: apply suggestion
hlts2 Jun 16, 2020
31c6762
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
10435c8
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
2fbab43
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
3f1a47b
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
a310c82
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
fb09a11
Update docs/contributing/coding-style.md
hlts2 Jun 16, 2020
9432e33
fix: apply suggestion
hlts2 Jun 16, 2020
6a7c131
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
d6bf1f6
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
e242e96
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
9c27a65
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
7814667
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
c72dad7
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
d2eed9a
fix: apply suggestion
hlts2 Jun 17, 2020
dc81753
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
6a24daf
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
9257230
Update docs/contributing/coding-style.md
hlts2 Jun 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/contributing/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,30 @@ func (s *something) SetSignedTok(st string) {
}
```

### Unused Variables
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug.
So please delete the unused variable.

Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.<br />
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

```go
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
type server struct {
addr string
port int
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
}

srv := &server {
addr: "192.168.33.10:1234",
// port <- unused variables
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
}

if err := srv.Run(); err != nil {
log.Fatal(err)
}

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
```

### Error handling

All errors should define in [internal/errors package](https://github.com/vdaas/vald/blob/master/internal/errors). All errors should be start with `Err` prefix, and all errors should be handle if possible.
Expand Down Expand Up @@ -603,3 +627,44 @@ We do not suggest to modify the generated code other than the `tests` variable,
}
// generated test code
```

1. Unused fields

By default, the template provides `fields` structure to initialize object of the test target.

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
But in some cases, not all `fields` are needed, so please delete the unnecessary fields.

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
For example, the following struct and the corresponding function:

```golang
type server struct {
addr string
port int
}
func (s *server) Addr() string {
return s.addr
}
```

And the generated test code is:
```golang
func Test_server_Addr(t *testing.T) {
type fields struct {
addr string
port int
}
type want struct {
// generated test code
```

Since the `port` variable is not used in this test case, you can delete the `port` definition in the test case.

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
```golang
func Test_server_Addr(t *testing.T) {
type fields struct {
addr string
// port int <-- this line should be deleted
}
type want struct {
// generated test code
```
6 changes: 6 additions & 0 deletions internal/params/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,42 @@ var (
}
)

// WithConfigFilePathKeys returns Option that sets filePath.keys.
func WithConfigFilePathKeys(keys ...string) Option {
return func(p *parser) {
p.filePath.keys = append(p.filePath.keys, keys...)
}
}

// WithConfigFilePathDefault returns Option that sets filePath.defaultPath.
func WithConfigFilePathDefault(path string) Option {
return func(p *parser) {
p.filePath.defaultPath = path
}
}

// WithConfigFileDescription returns Option that sets filePath.description.
func WithConfigFileDescription(desc string) Option {
return func(p *parser) {
p.filePath.description = desc
}
}

// WithVersionKeys returns Option that sets version.keys.
func WithVersionKeys(keys ...string) Option {
return func(p *parser) {
p.version.keys = append(p.version.keys, keys...)
}
}

// WithVersionFlagDefault returns Option that sets version.defaultFlag.
func WithVersionFlagDefault(flag bool) Option {
return func(p *parser) {
p.version.defaultFlag = flag
}
}

// WithVersionDescription returns Option that sets version.description.
func WithVersionDescription(desc string) Option {
return func(p *parser) {
p.version.description = desc
Expand Down
Loading