-
Notifications
You must be signed in to change notification settings - Fork 6k
simplifying if-else chains to switches #6208
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
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.
|
cmd/disk-cache.go
Outdated
@@ -230,7 +230,7 @@ func (c cacheObjects) GetObject(ctx context.Context, bucket, object string, star | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are not related to the changes mentioned by this PR, would it be possible to send this separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, sure.
cmd/endpoint.go
Outdated
@@ -330,12 +330,12 @@ func CreateEndpoints(serverAddr string, args ...[]string) (string, EndpointList, | |||
host = endpoint.Host | |||
} | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep cleanups to separate PR, this doesn't fix anything related to if-else
or cyclomatic complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
cmd/fallocate.go
Outdated
@@ -20,6 +20,6 @@ package cmd | |||
|
|||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a builtin function https://golang.org/pkg/builtin/#len
cmd/logger/logger.go
Outdated
@@ -283,9 +284,9 @@ func LogIf(ctx context.Context, err error) { | |||
req = &ReqInfo{API: "SYSTEM"} | |||
} | |||
|
|||
API := "SYSTEM" | |||
apiName := "SYSTEM" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here avoid cleanups like this....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
cmd/posix-errors.go
Outdated
@@ -25,8 +25,7 @@ import ( | |||
// Function not implemented error | |||
func isSysErrNoSys(err error) bool { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further simplification is possible simply do return pathErr.Err == syscall.ENOSYS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed
cmd/posix-errors.go
Outdated
@@ -58,8 +55,7 @@ func isSysErrNoSpace(err error) bool { | |||
// Input/output error | |||
func isSysErrIO(err error) bool { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further simplification is possible simply do return pathErr.Err == syscall.EIO
cmd/posix-errors.go
Outdated
@@ -69,8 +65,7 @@ func isSysErrIO(err error) bool { | |||
// Check if the given error corresponds to EISDIR (is a directory). | |||
func isSysErrIsDir(err error) bool { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further simplification is possible simply do return pathErr.Err == syscall.EISDIR
cmd/posix-errors.go
Outdated
@@ -80,8 +75,7 @@ func isSysErrIsDir(err error) bool { | |||
// Check if the given error corresponds to ENOTDIR (is not a directory). | |||
func isSysErrNotDir(err error) bool { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further simplification is possible simply do return pathErr.Err == syscall.ENOTDIR
cmd/posix-errors.go
Outdated
@@ -91,8 +85,7 @@ func isSysErrNotDir(err error) bool { | |||
// Check if the given error corresponds to the ENAMETOOLONG (name too long). | |||
func isSysErrTooLong(err error) bool { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further simplification is possible simply do return pathErr.Err == syscall.ENAMETOOLONG
cmd/retry.go
Outdated
@@ -61,7 +61,7 @@ var globalRandomSource = rand.New(&lockedRandSource{ | |||
// until the maximum retry attempts are reached. - this function is a fully | |||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not change the variable name cap
- any good reason you change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of https://golang.org/pkg/builtin/#cap same as https://golang.org/pkg/builtin/#len in another file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick review. Will address suggestions quickly.
cmd/disk-cache.go
Outdated
@@ -230,7 +230,7 @@ func (c cacheObjects) GetObject(ctx context.Context, bucket, object string, star | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, sure.
cmd/endpoint.go
Outdated
@@ -330,12 +330,12 @@ func CreateEndpoints(serverAddr string, args ...[]string) (string, EndpointList, | |||
host = endpoint.Host | |||
} | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
cmd/logger/logger.go
Outdated
@@ -283,9 +284,9 @@ func LogIf(ctx context.Context, err error) { | |||
req = &ReqInfo{API: "SYSTEM"} | |||
} | |||
|
|||
API := "SYSTEM" | |||
apiName := "SYSTEM" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
cmd/posix-errors.go
Outdated
@@ -25,8 +25,7 @@ import ( | |||
// Function not implemented error | |||
func isSysErrNoSys(err error) bool { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed
cmd/fallocate.go
Outdated
@@ -20,6 +20,6 @@ package cmd | |||
|
|||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a builtin function https://golang.org/pkg/builtin/#len
cmd/retry.go
Outdated
@@ -61,7 +61,7 @@ var globalRandomSource = rand.New(&lockedRandSource{ | |||
// until the maximum retry attempts are reached. - this function is a fully | |||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of https://golang.org/pkg/builtin/#cap same as https://golang.org/pkg/builtin/#len in another file.
cmd/bool-flag.go
Outdated
@@ -56,11 +56,12 @@ func (bf *BoolFlag) UnmarshalJSON(data []byte) (err error) { | |||
|
|||
// ParseBoolFlag - parses string into BoolFlag. | |||
func ParseBoolFlag(s string) (bf BoolFlag, err error) { | |||
if s == "on" { | |||
switch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
cmd/os-reliable.go
Outdated
return fmt.Errorf("%s (%s)->(%s)", errCrossDeviceLink, srcFilePath, dstFilePath) | ||
} else if os.IsNotExist(err) { | ||
case os.IsNotExist(err): | ||
return errFileNotFound | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need default
here (although it was even also missing in the original code);
default:
return err
fea64af
cmd/os-reliable.go
Outdated
@@ -95,6 +95,8 @@ func renameAll(srcFilePath, dstFilePath string) (err error) { | |||
return fmt.Errorf("%s (%s)->(%s)", errCrossDeviceLink, srcFilePath, dstFilePath) | |||
case os.IsNotExist(err): | |||
return errFileNotFound | |||
default: | |||
return err | |||
} | |||
} | |||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will look cleaner, if it is changed to return nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cristaloleg,
Could you please incorporate @kannappanr's comment?
Thanks
Can you fix the conflicts @cristaloleg ? |
@@ -83,17 +83,20 @@ func renameAll(srcFilePath, dstFilePath string) (err error) { | |||
} | |||
|
|||
if err = reliableRename(srcFilePath, dstFilePath); err != nil { | |||
if isSysErrNotDir(err) { | |||
switch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, now it's clear 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Mint Automation
6208-7dd973e/mint-dist-xl.sh.log:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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:
mint
PR # here: )