Skip to content

Commit 482dd09

Browse files
authored
Enforce new linting rules: errname, errorlint, lll, stylecheck (#32)
1 parent ec08a5a commit 482dd09

16 files changed

+890
-335
lines changed

.golangci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
linters:
2+
enable:
3+
- errname
4+
- errorlint
5+
- lll
6+
- stylecheck
7+
linters-settings:
8+
lll:
9+
tab-width: 4

CONTRIBUTING.md

+69-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,72 @@
11
# Contributing
22

3-
## Styleguides
3+
## Development guidelines
44

5-
- Use default `goimports` for code formatting.
6-
- Use default `golint`, `golangci-lint` and `go vet` for linting.
7-
- Do not use boolean flag in function arguments.
5+
### Formatter
6+
7+
We use `golangci-lint` to validate the code formatting. Make sure to have your code formatted before opening pull requests.
8+
9+
### Line length limit
10+
11+
Every code line should not exceed 80 characters when possible, but the hard limit is at 120 characters. Lines with documentation have an exception with the hard limit at 80 characters. A tab is treated as 4 spaces. It is recommended to set ruler guide at 80 and 120 characters on your editor.
12+
13+
### Wrapping long struct initialization
14+
15+
If the struct initialization is too long for the 80 character limit, wrap the initialization by assigning each field on its own line and end the initialization on its own line.
16+
17+
**WRONG**
18+
19+
```go
20+
a := User{
21+
Firstname: "John", Lastname: "Doe",
22+
}
23+
24+
a := User{
25+
Firstname: "John",
26+
Lastname: "Doe"}
27+
28+
a := User{Firstname: "John",
29+
Lastname: "Doe",
30+
}
31+
```
32+
33+
**RIGHT**
34+
35+
```go
36+
a := User{
37+
Firstname: "John",
38+
Lastname: "Doe",
39+
}
40+
```
41+
42+
### Wrapping long function definitions
43+
44+
If the function definition is too long for the 80 character limit, wrap the function definition by writing with as few lines as possible while avoiding trailing open parenthesis and traling commas after the last parameter and the last return type.
45+
46+
**WRONG**
47+
48+
```go
49+
func doSomething(
50+
a,
51+
b,
52+
c,
53+
) (d, error) {
54+
55+
func doSomething(a, b, c,
56+
) (d, error) {
57+
58+
func doSomething(a, b, c) (
59+
d, error) {
60+
```
61+
62+
**RIGHT**
63+
64+
```go
65+
func doSomething(a, b,
66+
c) (d, error) {
67+
68+
func doSomething(a, b, c) (d,
69+
error) {
70+
```
71+
72+
If the function definition spans multiple lines, the function body should start with an empty line to help distinguishing two elements.

internal/code/extractor_test.go

+45-11
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,50 @@ type UserRepository interface {
129129
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
130130
},
131131
Returns: []code.Type{
132-
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
132+
code.ArrayType{
133+
ContainedType: code.PointerType{
134+
ContainedType: code.SimpleType("UserModel"),
135+
},
136+
},
133137
code.TypeError,
134138
},
135139
},
136140
{
137141
Name: "FindByAgeBetween",
138142
Params: []code.Param{
139-
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
140-
{Name: "fromAge", Type: code.TypeInt},
141-
{Name: "toAge", Type: code.TypeInt},
143+
{
144+
Name: "ctx",
145+
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
146+
},
147+
{
148+
Name: "fromAge",
149+
Type: code.TypeInt,
150+
},
151+
{
152+
Name: "toAge",
153+
Type: code.TypeInt,
154+
},
142155
},
143156
Returns: []code.Type{
144-
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
157+
code.ArrayType{
158+
ContainedType: code.PointerType{
159+
ContainedType: code.SimpleType("UserModel"),
160+
},
161+
},
145162
code.TypeError,
146163
},
147164
},
148165
{
149166
Name: "InsertOne",
150167
Params: []code.Param{
151-
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
152-
{Name: "user", Type: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
168+
{
169+
Name: "ctx",
170+
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
171+
},
172+
{
173+
Name: "user",
174+
Type: code.PointerType{ContainedType: code.SimpleType("UserModel")},
175+
},
153176
},
154177
Returns: []code.Type{
155178
code.InterfaceType{},
@@ -159,9 +182,18 @@ type UserRepository interface {
159182
{
160183
Name: "UpdateAgreementByID",
161184
Params: []code.Param{
162-
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
163-
{Name: "agreement", Type: code.MapType{KeyType: code.TypeString, ValueType: code.TypeBool}},
164-
{Name: "id", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
185+
{
186+
Name: "ctx",
187+
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
188+
},
189+
{
190+
Name: "agreement",
191+
Type: code.MapType{KeyType: code.TypeString, ValueType: code.TypeBool},
192+
},
193+
{
194+
Name: "id",
195+
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
196+
},
165197
},
166198
Returns: []code.Type{
167199
code.TypeBool,
@@ -273,7 +305,9 @@ type UserRepository interface {
273305
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
274306
},
275307
Returns: []code.Type{
276-
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
308+
code.ArrayType{
309+
ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")},
310+
},
277311
code.TypeError,
278312
},
279313
},

internal/code/package.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func ParsePackage(pkgs map[string]*ast.Package) (Package, error) {
2424
}
2525

2626
// Package stores package name, struct and interface implementations as a result
27-
// from ParsePackage
27+
// from ParsePackage.
2828
type Package struct {
2929
Name string
3030
Structs map[string]Struct

0 commit comments

Comments
 (0)