@@ -20,7 +20,7 @@ Package being renamed to `dev.gaijin.team/go/exhaustruct/v4` and all further upd
20
20
### Installation
21
21
22
22
``` shell
23
- go get -u github.com/GaijinEntertainment/go- exhaustruct/v4/cmd/exhaustruct
23
+ go get -u dev.gaijin.team/go/ exhaustruct/v4/cmd/exhaustruct
24
24
```
25
25
26
26
### Usage
@@ -80,24 +80,27 @@ it's check regardless global configuration. Comment directives have precedence o
80
80
By default, linter will check all structures and report if any accessible field initialization is missing.
81
81
82
82
``` go
83
+ package main
84
+
83
85
type Config struct {
84
- Host string
85
- Port int
86
- Database string
86
+ Host string
87
+ Port int
88
+ Database string
87
89
}
88
90
89
91
// Without any flags - requires all fields
90
92
func createConfig () Config {
91
- return Config{} // ERROR: missing fields Host, Port, Database
93
+ return Config{} // ERROR: missing fields Host, Port, Database
92
94
}
93
95
94
96
func createValidConfig () Config {
95
- return Config{
96
- Host: " localhost" ,
97
- Port: 5432 ,
98
- Database: " mydb" ,
99
- }
97
+ return Config{
98
+ Host: " localhost" ,
99
+ Port: 5432 ,
100
+ Database: " mydb" ,
101
+ }
100
102
}
103
+
101
104
```
102
105
103
106
#### Empty Allowance Options
@@ -112,16 +115,19 @@ exhaustruct -allow-empty ./...
112
115
```
113
116
114
117
``` go
118
+ package main
119
+
115
120
// With -allow-empty: ALL empty structs are allowed
116
121
func createConfig () Config {
117
- return Config{} // OK: empty structs allowed globally
122
+ return Config{} // OK: empty structs allowed globally
118
123
}
119
124
120
125
var globalConfig = Config {} // OK: empty structs allowed globally
121
126
122
127
func processConfigs () []Config {
123
- return []Config{{}} // OK: empty structs allowed globally
128
+ return []Config{{}} // OK: empty structs allowed globally
124
129
}
130
+
125
131
```
126
132
127
133
##### 2. Return Statement Allowance (` -allow-empty-returns ` )
@@ -135,19 +141,22 @@ exhaustruct -allow-empty-returns ./...
135
141
```
136
142
137
143
``` go
144
+ package main
145
+
138
146
// With -allow-empty-returns: empty structs allowed only in return statements
139
147
func createConfig () Config {
140
- return Config{} // OK: empty struct in return statement
148
+ return Config{} // OK: empty struct in return statement
141
149
}
142
150
143
151
func initializeConfig () {
144
- var config = Config{} // ERROR: empty struct in variable declaration
145
- _ = config
152
+ var config = Config{} // ERROR: empty struct in variable declaration
153
+ _ = config
146
154
}
147
155
148
156
func processConfigs () []Config {
149
- return []Config{{}} // ERROR: empty struct in slice literal (not direct child of return statement)
157
+ return []Config{{}} // ERROR: empty struct in slice literal (not direct child of return statement)
150
158
}
159
+
151
160
```
152
161
153
162
##### 3. Variable Declaration Allowance (` -allow-empty-declarations ` )
@@ -161,21 +170,23 @@ exhaustruct -allow-empty-declarations ./...
161
170
```
162
171
163
172
``` go
173
+ package main
174
+
164
175
// With -allow-empty-declarations: empty structs allowed in variable declarations
165
176
func initializeConfig () {
166
- var config = Config{} // OK: empty struct in variable declaration
167
- config := Config{} // OK: empty struct in short variable declaration
168
- ptr := &Config{} // OK: empty struct in pointer declaration
169
- _ = config
170
- _ = ptr
177
+ var config = Config{} // OK: empty struct in variable declaration
178
+ config := Config{} // OK: empty struct in short variable declaration
179
+ ptr := &Config{} // OK: empty struct in pointer declaration
180
+ _ = config
181
+ _ = ptr
171
182
}
172
183
173
184
func createConfig () Config {
174
- return Config{} // ERROR: empty struct in return statement
185
+ return Config{} // ERROR: empty struct in return statement
175
186
}
176
187
177
188
func processConfigs () []Config {
178
- return []Config{{}} // ERROR: empty struct in slice literal
189
+ return []Config{{}} // ERROR: empty struct in slice literal
179
190
}
180
191
```
181
192
@@ -189,35 +200,38 @@ exhaustruct -allow-empty-include ".*Config.*" -allow-empty-include ".*Options.*"
189
200
```
190
201
191
202
``` go
203
+ package main
204
+
192
205
type DatabaseConfig struct {
193
- Host string
194
- Port int
206
+ Host string
207
+ Port int
195
208
}
196
209
197
210
type ServerOptions struct {
198
- Timeout int
199
- MaxConns int
211
+ Timeout int
212
+ MaxConns int
200
213
}
201
214
202
215
type UserData struct {
203
- Name string
204
- Email string
216
+ Name string
217
+ Email string
205
218
}
206
219
207
220
func example () {
208
- // OK: matches .*Config.* pattern
209
- config := DatabaseConfig{}
221
+ // OK: matches .*Config.* pattern
222
+ config := DatabaseConfig{}
210
223
211
- // OK: matches .*Options.* pattern
212
- opts := ServerOptions{}
224
+ // OK: matches .*Options.* pattern
225
+ opts := ServerOptions{}
213
226
214
- // ERROR: doesn't match any pattern
215
- user := UserData{}
227
+ // ERROR: doesn't match any pattern
228
+ user := UserData{}
216
229
217
- _ = config
218
- _ = opts
219
- _ = user
230
+ _ = config
231
+ _ = opts
232
+ _ = user
220
233
}
234
+
221
235
```
222
236
223
237
#### Errors handling
@@ -226,29 +240,35 @@ In order to avoid unnecessary noise, when dealing with non-pointer types returne
226
240
ignore non-error types, in case return statement contains non-nil value that satisfies ` error ` interface.
227
241
228
242
``` go
243
+ package main
244
+
245
+ import (
246
+ " errors"
247
+ )
248
+
229
249
type Shape struct {
230
- Length int
231
- Width int
250
+ Length int
251
+ Width int
232
252
}
233
253
234
254
func NewShape () (Shape , error ) {
235
- return Shape{}, errors.New (" error" ) // will not raise an error
255
+ return Shape{}, errors.New (" error" ) // will not raise an error
236
256
}
237
257
238
258
type MyError struct {
239
- Err error
259
+ Err error
240
260
}
241
261
242
262
func (e MyError ) Error () string {
243
- return e.Err .Error ()
263
+ return e.Err .Error ()
244
264
}
245
265
246
266
func NewSquare () (Shape , error ) {
247
- return Shape{}, &MyError{Err: errors.New (" error" )} // will not raise an error
267
+ return Shape{}, &MyError{Err: errors.New (" error" )} // will not raise an error
248
268
}
249
269
250
270
func NewCircle () (Shape , error ) {
251
- return Shape{}, &MyError{} // will raise "main.MyError is missing field Err"
271
+ return Shape{}, &MyError{} // will raise "main.MyError is missing field Err"
252
272
}
253
273
254
274
```
0 commit comments