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

optimize handling of validator funcs to avoid unnecessary calls and only return single error if possible #50

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Changes from all commits
Commits
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
19 changes: 12 additions & 7 deletions pkg/c8y/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,19 @@ func (c *Client) SendRequest(ctx context.Context, options RequestOptions) (*Resp
}

// Optional request validator (allows users to verify the outgoing request before it is sent)
validatorErrors := make([]error, 0)
for _, validator := range options.ValidateFuncs {
if vErr := validator(req); vErr != nil {
validatorErrors = append(validatorErrors, vErr)
if len(options.ValidateFuncs) > 0 {
validatorErrors := make([]error, 0)
for _, validator := range options.ValidateFuncs {
if vErr := validator(req); vErr != nil {
validatorErrors = append(validatorErrors, vErr)
}
}
if len(validatorErrors) == 1 {
return nil, validatorErrors[0]
}
if len(validatorErrors) > 1 {
return nil, errors.Join(validatorErrors...)
}
}
if len(validatorErrors) > 0 {
return nil, errors.Join(validatorErrors...)
}

if dryRun {
Expand Down