Skip to content

Commit

Permalink
simplifying if-else chains to switches (#6208)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored and kannappanr committed Aug 6, 2018
1 parent a82500f commit 37de2db
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 138 deletions.
3 changes: 2 additions & 1 deletion cmd/admin-handlers.go
Expand Up @@ -478,8 +478,9 @@ func toAdminAPIErrCode(err error) APIErrorCode {
switch err {
case errXLWriteQuorum:
return ErrAdminConfigNoQuorum
default:
return toAPIErrorCode(err)
}
return toAPIErrorCode(err)
}

// SetConfigResult - represents detailed results of a set-config
Expand Down
6 changes: 4 additions & 2 deletions cmd/admin-rpc-client.go
Expand Up @@ -398,14 +398,16 @@ func getValidServerConfig(serverConfigs []serverConfig, errs []error) (scv serve
// seen. See example above for
// clarity.
continue
} else if j < i && serverConfigs[i].ConfigDiff(&serverConfigs[j]) == "" {
}
if j < i && serverConfigs[i].ConfigDiff(&serverConfigs[j]) == "" {
// serverConfigs[i] is equal to
// serverConfigs[j], update
// serverConfigs[j]'s counter since it
// is the lower index.
configCounter[j]++
break
} else if j == i {
}
if j == i {
// serverConfigs[i] is equal to no
// other value seen before. It is
// unique so far.
Expand Down
7 changes: 4 additions & 3 deletions cmd/bool-flag.go
Expand Up @@ -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 s {
case "on":
bf = true
} else if s == "off" {
case "off":
bf = false
} else {
default:
err = fmt.Errorf("invalid value ‘%s’ for BoolFlag", s)
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/common-main.go
Expand Up @@ -99,17 +99,18 @@ func handleCommonCmdArgs(ctx *cli.Context) {

var configDir string

if ctx.IsSet("config-dir") {
switch {
case ctx.IsSet("config-dir"):
configDir = ctx.String("config-dir")
} else if ctx.GlobalIsSet("config-dir") {
case ctx.GlobalIsSet("config-dir"):
configDir = ctx.GlobalString("config-dir")
// cli package does not expose parent's "config-dir" option. Below code is workaround.
if configDir == "" || configDir == getConfigDir() {
if ctx.Parent().GlobalIsSet("config-dir") {
configDir = ctx.Parent().GlobalString("config-dir")
}
}
} else {
default:
// Neither local nor global config-dir option is provided. In this case, try to use
// default config directory.
configDir = getConfigDir()
Expand Down
14 changes: 8 additions & 6 deletions cmd/fs-v1-helpers.go
Expand Up @@ -121,23 +121,25 @@ func fsMkdir(ctx context.Context, dirPath string) (err error) {
}

if err = os.Mkdir((dirPath), 0777); err != nil {
if os.IsExist(err) {
switch {
case os.IsExist(err):
return errVolumeExists
} else if os.IsPermission(err) {
case os.IsPermission(err):
logger.LogIf(ctx, errDiskAccessDenied)
return errDiskAccessDenied
} else if isSysErrNotDir(err) {
case isSysErrNotDir(err):
// File path cannot be verified since
// one of the parents is a file.
logger.LogIf(ctx, errDiskAccessDenied)
return errDiskAccessDenied
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
// Add specific case for windows.
logger.LogIf(ctx, errDiskAccessDenied)
return errDiskAccessDenied
default:
logger.LogIf(ctx, err)
return err
}
logger.LogIf(ctx, err)
return err
}

return nil
Expand Down
34 changes: 20 additions & 14 deletions cmd/fs-v1-rwpool.go
Expand Up @@ -93,18 +93,20 @@ func (fsi *fsIOPool) Open(path string) (*lock.RLockedFile, error) {
// Open file for reading with read lock.
newRlkFile, err := lock.RLockedOpenFile(path)
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return nil, errFileNotFound
} else if os.IsPermission(err) {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIsDir(err) {
case isSysErrIsDir(err):
return nil, errIsNotRegular
} else if isSysErrNotDir(err) {
case isSysErrNotDir(err):
return nil, errFileAccessDenied
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
return nil, errFileNotFound
default:
return nil, err
}
return nil, err
}

/// Save new reader on the map.
Expand Down Expand Up @@ -148,14 +150,16 @@ func (fsi *fsIOPool) Write(path string) (wlk *lock.LockedFile, err error) {

wlk, err = lock.LockedOpenFile(path, os.O_RDWR, 0666)
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return nil, errFileNotFound
} else if os.IsPermission(err) {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIsDir(err) {
case isSysErrIsDir(err):
return nil, errIsNotRegular
default:
return nil, err
}
return nil, err
}
return wlk, nil
}
Expand All @@ -175,14 +179,16 @@ func (fsi *fsIOPool) Create(path string) (wlk *lock.LockedFile, err error) {
// Attempt to create the file.
wlk, err = lock.LockedOpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
if os.IsPermission(err) {
switch {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIsDir(err) {
case isSysErrIsDir(err):
return nil, errIsNotRegular
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
return nil, errFileAccessDenied
default:
return nil, err
}
return nil, err
}

// Success.
Expand Down
9 changes: 5 additions & 4 deletions cmd/httprange.go
Expand Up @@ -89,7 +89,8 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
}

// rangeString contains first and last byte positions. eg. "bytes=2-5"
if offsetBegin > -1 && offsetEnd > -1 {
switch {
case offsetBegin > -1 && offsetEnd > -1:
if offsetBegin > offsetEnd {
// Last byte position is not greater than first byte position. eg. "bytes=5-2"
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
Expand All @@ -103,15 +104,15 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
if offsetEnd >= resourceSize {
offsetEnd = resourceSize - 1
}
} else if offsetBegin > -1 {
case offsetBegin > -1:
// rangeString contains only first byte position. eg. "bytes=8-"
if offsetBegin >= resourceSize {
// First byte position should not be >= resourceSize.
return nil, errInvalidRange
}

offsetEnd = resourceSize - 1
} else if offsetEnd > -1 {
case offsetEnd > -1:
// rangeString contains only last byte position. eg. "bytes=-3"
if offsetEnd == 0 {
// Last byte position should not be zero eg. "bytes=-0"
Expand All @@ -125,7 +126,7 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
}

offsetEnd = resourceSize - 1
} else {
default:
// rangeString contains first and last byte positions missing. eg. "bytes=-"
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/logger/logger.go
Expand Up @@ -94,13 +94,14 @@ type Console interface {
}

func consoleLog(console Console, msg string, args ...interface{}) {
if jsonFlag {
switch {
case jsonFlag:
// Strip escape control characters from json message
msg = ansiRE.ReplaceAllLiteralString(msg, "")
console.json(msg, args...)
} else if quiet {
case quiet:
console.quiet(msg, args...)
} else {
default:
console.pretty(msg, args...)
}
}
Expand Down
14 changes: 4 additions & 10 deletions cmd/object-api-errors.go
Expand Up @@ -375,20 +375,14 @@ func (e BackendDown) Error() string {

// isErrIncompleteBody - Check if error type is IncompleteBody.
func isErrIncompleteBody(err error) bool {
switch err.(type) {
case IncompleteBody:
return true
}
return false
_, ok := err.(IncompleteBody)
return ok
}

// isErrObjectNotFound - Check if error type is ObjectNotFound.
func isErrObjectNotFound(err error) bool {
switch err.(type) {
case ObjectNotFound:
return true
}
return false
_, ok := err.(ObjectNotFound)
return ok
}

// isInsufficientReadQuorum - Check if error type is InsufficientReadQuorum.
Expand Down
13 changes: 8 additions & 5 deletions cmd/os-reliable.go
Expand Up @@ -130,20 +130,23 @@ func renameAll(srcFilePath, dstFilePath string) (err error) {
}

if err = reliableRename(srcFilePath, dstFilePath); err != nil {
if isSysErrNotDir(err) {
switch {
case isSysErrNotDir(err):
return errFileAccessDenied
} else if isSysErrPathNotFound(err) {
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
} else if isSysErrCrossDevice(err) {
case isSysErrCrossDevice(err):
return fmt.Errorf("%s (%s)->(%s)", errCrossDeviceLink, srcFilePath, dstFilePath)
} else if os.IsNotExist(err) {
case os.IsNotExist(err):
return errFileNotFound
default:
return err
}
}
return err
return nil
}

// Reliably retries os.RenameAll if for some reason os.RenameAll returns
Expand Down

0 comments on commit 37de2db

Please sign in to comment.