Skip to content

Commit 7b4e021

Browse files
baerwangdaveshanley
authored andcommitted
style: channel use struct
1 parent c4e3ad1 commit 7b4e021

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

validator.go

+17-25
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ func (v *validator) ValidateHttpRequestWithPathItem(request *http.Request, pathI
170170
reqBodyValidator := v.requestValidator
171171

172172
// create some channels to handle async validation
173-
doneChan := make(chan bool)
173+
doneChan := make(chan struct{})
174174
errChan := make(chan []*errors.ValidationError)
175-
controlChan := make(chan bool)
175+
controlChan := make(chan struct{})
176176

177177
// async param validation function.
178-
parameterValidationFunc := func(control chan bool, errorChan chan []*errors.ValidationError) {
178+
parameterValidationFunc := func(control chan struct{}, errorChan chan []*errors.ValidationError) {
179179
paramErrs := make(chan []*errors.ValidationError)
180-
paramControlChan := make(chan bool)
181-
paramFunctionControlChan := make(chan bool)
180+
paramControlChan := make(chan struct{})
181+
paramFunctionControlChan := make(chan struct{})
182182
var paramValidationErrors []*errors.ValidationError
183183

184184
validations := []validationFunction{
@@ -190,7 +190,7 @@ func (v *validator) ValidateHttpRequestWithPathItem(request *http.Request, pathI
190190
}
191191

192192
// listen for validation errors on parameters. everything will run async.
193-
paramListener := func(control chan bool, errorChan chan []*errors.ValidationError) {
193+
paramListener := func(control chan struct{}, errorChan chan []*errors.ValidationError) {
194194
completedValidations := 0
195195
for {
196196
select {
@@ -199,22 +199,22 @@ func (v *validator) ValidateHttpRequestWithPathItem(request *http.Request, pathI
199199
case <-control:
200200
completedValidations++
201201
if completedValidations == len(validations) {
202-
paramFunctionControlChan <- true
202+
paramFunctionControlChan <- struct{}{}
203203
return
204204
}
205205
}
206206
}
207207
}
208208

209209
validateParamFunction := func(
210-
control chan bool,
210+
control chan struct{},
211211
errorChan chan []*errors.ValidationError,
212212
validatorFunc validationFunction) {
213213
valid, pErrs := validatorFunc(request, pathItem, pathValue)
214214
if !valid {
215215
errorChan <- pErrs
216216
}
217-
control <- true
217+
control <- struct{}{}
218218
}
219219
go paramListener(paramControlChan, paramErrs)
220220
for i := range validations {
@@ -228,15 +228,15 @@ func (v *validator) ValidateHttpRequestWithPathItem(request *http.Request, pathI
228228
}
229229

230230
// let runValidation know we are done with this part.
231-
controlChan <- true
231+
controlChan <- struct{}{}
232232
}
233233

234-
requestBodyValidationFunc := func(control chan bool, errorChan chan []*errors.ValidationError) {
234+
requestBodyValidationFunc := func(control chan struct{}, errorChan chan []*errors.ValidationError) {
235235
valid, pErrs := reqBodyValidator.ValidateRequestBodyWithPathItem(request, pathItem, pathValue)
236236
if !valid {
237237
errorChan <- pErrs
238238
}
239-
control <- true
239+
control <- struct{}{}
240240
}
241241

242242
// build async functions
@@ -257,10 +257,7 @@ func (v *validator) ValidateHttpRequestWithPathItem(request *http.Request, pathI
257257

258258
// wait for all the validations to complete
259259
<-doneChan
260-
if len(validationErrors) > 0 {
261-
return false, validationErrors
262-
}
263-
return true, nil
260+
return !(len(validationErrors) > 0), validationErrors
264261
}
265262

266263
func (v *validator) ValidateHttpRequestSync(request *http.Request) (bool, []*errors.ValidationError) {
@@ -300,12 +297,7 @@ func (v *validator) ValidateHttpRequestSyncWithPathItem(request *http.Request, p
300297
}
301298

302299
validationErrors = append(validationErrors, paramValidationErrors...)
303-
304-
if len(validationErrors) > 0 {
305-
return false, validationErrors
306-
}
307-
308-
return true, nil
300+
return !(len(validationErrors) > 0), validationErrors
309301
}
310302

311303
type validator struct {
@@ -316,7 +308,7 @@ type validator struct {
316308
responseValidator responses.ResponseBodyValidator
317309
}
318310

319-
func runValidation(control, doneChan chan bool,
311+
func runValidation(control, doneChan chan struct{},
320312
errorChan chan []*errors.ValidationError,
321313
validationErrors *[]*errors.ValidationError,
322314
total int) {
@@ -332,12 +324,12 @@ func runValidation(control, doneChan chan bool,
332324
case <-control:
333325
completedValidations++
334326
if completedValidations == total {
335-
doneChan <- true
327+
doneChan <- struct{}{}
336328
return
337329
}
338330
}
339331
}
340332
}
341333

342334
type validationFunction func(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError)
343-
type validationFunctionAsync func(control chan bool, errorChan chan []*errors.ValidationError)
335+
type validationFunctionAsync func(control chan struct{}, errorChan chan []*errors.ValidationError)

0 commit comments

Comments
 (0)