Skip to content

Commit

Permalink
DE-1373 Get rid of defers in a loop (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc authored Dec 10, 2024
1 parent 6ec38d2 commit 5003ccc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ linters-settings:
- style
disabled-checks:
- singleCaseSwitch
- deferInLoop # TODO(DE-1373): enable
- hugeParam # TODO(v5): enable?
- sprintfQuotedString # noisy # TODO: enable
- exitAfterDefer # TODO: enable?
Expand Down
48 changes: 30 additions & 18 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,47 @@ func (f *FormDataPayload) getPayloadBuffer() (*bytes.Buffer, error) {
}

for _, file := range f.Files {
tmp, err := writer.CreateFormFile(file.key, path.Base(file.value))
if err != nil {
return nil, err
}
err := func() error {
tmp, err := writer.CreateFormFile(file.key, path.Base(file.value))
if err != nil {
return err
}

fp, err := os.Open(file.value)
if err != nil {
return nil, err
}
fp, err := os.Open(file.value)
if err != nil {
return err
}

// TODO(DE-1373): defer in a loop:
defer fp.Close()
defer fp.Close()

_, err = io.Copy(tmp, fp)
_, err = io.Copy(tmp, fp)
if err != nil {
return err
}

return nil
}()
if err != nil {
return nil, err
}
}

for _, file := range f.ReadClosers {
tmp, err := writer.CreateFormFile(file.key, file.name)
if err != nil {
return nil, err
}
err := func() error {
tmp, err := writer.CreateFormFile(file.key, file.name)
if err != nil {
return err
}

defer file.value.Close()

// TODO(DE-1373): defer in a loop:
defer file.value.Close()
_, err = io.Copy(tmp, file.value)
if err != nil {
return err
}

_, err = io.Copy(tmp, file.value)
return nil
}()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5003ccc

Please sign in to comment.