Repository navigation
simplifying if-else chains to switches - #6208
Conversation
Codecov Report
@@ Coverage Diff @@
## master #6208 +/- ##
==========================================
+ Coverage 58.41% 58.46% +0.05%
==========================================
Files 222 222
Lines 32058 32043 -15
==========================================
+ Hits 18726 18734 +8
+ Misses 11689 11685 -4
+ Partials 1643 1624 -19
Continue to review full report at Codecov.
|
| return err | ||
| } | ||
| go func() { | ||
| if err = GetObjectFn(ctx, bucket, object, 0, objInfo.Size, io.MultiWriter(writer, pipeWriter), etag); err != nil { |
There was a problem hiding this comment.
These changes are not related to the changes mentioned by this PR, would it be possible to send this separately?
| hostIPSet, _ := getHostIP4(host) | ||
| if IPSet, ok := pathIPMap[endpoint.Path]; ok { | ||
| if !IPSet.Intersection(hostIPSet).IsEmpty() { | ||
| if ipSet, ok := pathIPMap[endpoint.Path]; ok { |
There was a problem hiding this comment.
Keep cleanups to separate PR, this doesn't fix anything related to if-else or cyclomatic complexity.
| // Fallocate is not POSIX and not supported under Windows | ||
| // Always return successful | ||
| func Fallocate(fd int, offset int64, len int64) error { | ||
| func Fallocate(fd int, offset int64, length int64) error { |
There was a problem hiding this comment.
Same here len is synonymous to length, no reason to rename it. The corresponding function in fallocate_linux.go is untouched..
There was a problem hiding this comment.
It's a builtin function https://golang.org/pkg/builtin/#len
| } | ||
|
|
||
| API := "SYSTEM" | ||
| apiName := "SYSTEM" |
There was a problem hiding this comment.
Same here avoid cleanups like this....
| if pathErr, ok := err.(*os.PathError); ok { | ||
| switch pathErr.Err { | ||
| case syscall.ENOSYS: | ||
| if pathErr.Err == syscall.ENOSYS { |
There was a problem hiding this comment.
Further simplification is possible simply do return pathErr.Err == syscall.ENOSYS
| if pathErr, ok := err.(*os.PathError); ok { | ||
| switch pathErr.Err { | ||
| case syscall.EIO: | ||
| if pathErr.Err == syscall.EIO { |
There was a problem hiding this comment.
Further simplification is possible simply do return pathErr.Err == syscall.EIO
| if pathErr, ok := err.(*os.PathError); ok { | ||
| switch pathErr.Err { | ||
| case syscall.EISDIR: | ||
| if pathErr.Err == syscall.EISDIR { |
There was a problem hiding this comment.
Further simplification is possible simply do return pathErr.Err == syscall.EISDIR
| if pathErr, ok := err.(*os.PathError); ok { | ||
| switch pathErr.Err { | ||
| case syscall.ENOTDIR: | ||
| if pathErr.Err == syscall.ENOTDIR { |
There was a problem hiding this comment.
Further simplification is possible simply do return pathErr.Err == syscall.ENOTDIR
| if pathErr, ok := err.(*os.PathError); ok { | ||
| switch pathErr.Err { | ||
| case syscall.ENAMETOOLONG: | ||
| if pathErr.Err == syscall.ENAMETOOLONG { |
There was a problem hiding this comment.
Further simplification is possible simply do return pathErr.Err == syscall.ENAMETOOLONG
| // configurable version, meant for only advanced use cases. For the most part | ||
| // one should use newRetryTimerSimple and newRetryTimer. | ||
| func newRetryTimerWithJitter(unit time.Duration, cap time.Duration, jitter float64, doneCh chan struct{}) <-chan int { | ||
| func newRetryTimerWithJitter(unit time.Duration, max time.Duration, jitter float64, doneCh chan struct{}) <-chan int { |
There was a problem hiding this comment.
Do not change the variable name cap - any good reason you change it?
There was a problem hiding this comment.
Because of https://golang.org/pkg/builtin/#cap same as https://golang.org/pkg/builtin/#len in another file.
cristaloleg
left a comment
There was a problem hiding this comment.
Thanks for the quick review. Will address suggestions quickly.
| return err | ||
| } | ||
| go func() { | ||
| if err = GetObjectFn(ctx, bucket, object, 0, objInfo.Size, io.MultiWriter(writer, pipeWriter), etag); err != nil { |
| hostIPSet, _ := getHostIP4(host) | ||
| if IPSet, ok := pathIPMap[endpoint.Path]; ok { | ||
| if !IPSet.Intersection(hostIPSet).IsEmpty() { | ||
| if ipSet, ok := pathIPMap[endpoint.Path]; ok { |
| } | ||
|
|
||
| API := "SYSTEM" | ||
| apiName := "SYSTEM" |
| if pathErr, ok := err.(*os.PathError); ok { | ||
| switch pathErr.Err { | ||
| case syscall.ENOSYS: | ||
| if pathErr.Err == syscall.ENOSYS { |
| // Fallocate is not POSIX and not supported under Windows | ||
| // Always return successful | ||
| func Fallocate(fd int, offset int64, len int64) error { | ||
| func Fallocate(fd int, offset int64, length int64) error { |
There was a problem hiding this comment.
It's a builtin function https://golang.org/pkg/builtin/#len
| // configurable version, meant for only advanced use cases. For the most part | ||
| // one should use newRetryTimerSimple and newRetryTimer. | ||
| func newRetryTimerWithJitter(unit time.Duration, cap time.Duration, jitter float64, doneCh chan struct{}) <-chan int { | ||
| func newRetryTimerWithJitter(unit time.Duration, max time.Duration, jitter float64, doneCh chan struct{}) <-chan int { |
There was a problem hiding this comment.
Because of https://golang.org/pkg/builtin/#cap same as https://golang.org/pkg/builtin/#len in another file.
| // ParseBoolFlag - parses string into BoolFlag. | ||
| func ParseBoolFlag(s string) (bf BoolFlag, err error) { | ||
| if s == "on" { | ||
| switch { |
There was a problem hiding this comment.
this can be changed to
switch s {
case "on":
bf = true
case "off":
bf = false
default:
err = fmt.Errorf("invalid value ‘%s’ for BoolFlag", s)
}
| } else if os.IsNotExist(err) { | ||
| case os.IsNotExist(err): | ||
| return errFileNotFound | ||
| } |
There was a problem hiding this comment.
Don't we need default here (although it was even also missing in the original code);
default:
return err
fea64af
| return err | ||
| } | ||
| } | ||
| return err |
There was a problem hiding this comment.
It will look cleaner, if it is changed to return nil
There was a problem hiding this comment.
@cristaloleg,
Could you please incorporate @kannappanr's comment?
Thanks
|
Can you fix the conflicts @cristaloleg ? |
|
|
||
| if err = reliableRename(srcFilePath, dstFilePath); err != nil { | ||
| if isSysErrNotDir(err) { | ||
| switch { |
There was a problem hiding this comment.
Sorry, I think you misunderstood what I meant. I wanted the renameAll function to look like this
func renameAll(srcFilePath, dstFilePath string) (err error) {
if srcFilePath == "" || dstFilePath == "" {
return errInvalidArgument
}
if err = checkPathLength(srcFilePath); err != nil {
return err
}
if err = checkPathLength(dstFilePath); err != nil {
return err
}
if err = reliableRename(srcFilePath, dstFilePath); err != nil {
switch {
case isSysErrNotDir(err):
return errFileAccessDenied
case isSysErrPathNotFound(err):
// This is a special case should be handled only for
// windows, because windows API does not return "not a
// directory" error message. Handle this specifically here.
return errFileAccessDenied
case isSysErrCrossDevice(err):
return fmt.Errorf("%s (%s)->(%s)", errCrossDeviceLink, srcFilePath, dstFilePath)
case os.IsNotExist(err):
return errFileNotFound
default:
return err
}
}
return nil
}
There was a problem hiding this comment.
Oh, now it's clear 😉
Mint Automation
6208-7dd973e/mint-dist-xl.sh.log: |
|
Thanks to everyone 👍 |
Description
Simplifying if-else chains to switches.
All the changes were suggested by https://github.com/go-critic/go-critic
Motivation and Context
To make code easier to read.
How Has This Been Tested?
Regular build, there is no logical changes, only style and code simplification.
Types of changes
Checklist:
mintPR # here: )