From ed9c790fea8ae5fed142ad35b5682bdc404c3c41 Mon Sep 17 00:00:00 2001 From: alecbakholdin Date: Fri, 20 Dec 2024 21:26:16 -0500 Subject: [PATCH] fixed https://github.com/polyfloyd/go-errorlint linting --- baked_in.go | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/baked_in.go b/baked_in.go index 2f66c1836..c1afb8579 100644 --- a/baked_in.go +++ b/baked_in.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "io/fs" "net" @@ -1663,16 +1664,8 @@ func isFilePath(fl FieldLevel) bool { return false } if _, err = os.Stat(field.String()); err != nil { - switch t := err.(type) { - case *fs.PathError: - if t.Err == syscall.EINVAL { - // It's definitely an invalid character in the filepath. - return false - } - // It could be a permission error, a does-not-exist error, etc. - // Out-of-scope for this validation, though. - return true - default: + var pathErr *fs.PathError + if !errors.As(err, &pathErr) { // Something went *seriously* wrong. /* Per https://pkg.go.dev/os#Stat: @@ -1680,6 +1673,13 @@ func isFilePath(fl FieldLevel) bool { */ panic(err) } + if errors.Is(pathErr.Err, syscall.EINVAL) { + // It's definitely an invalid character in the filepath. + return false + } + // It could be a permission error, a does-not-exist error, etc. + // Out-of-scope for this validation, though. + return true } } @@ -2650,21 +2650,8 @@ func isDirPath(fl FieldLevel) bool { return false } if _, err = os.Stat(field.String()); err != nil { - switch t := err.(type) { - case *fs.PathError: - if t.Err == syscall.EINVAL { - // It's definitely an invalid character in the path. - return false - } - // It could be a permission error, a does-not-exist error, etc. - // Out-of-scope for this validation, though. - // Lastly, we make sure it is a directory. - if strings.HasSuffix(field.String(), string(os.PathSeparator)) { - return true - } else { - return false - } - default: + var pathErr *fs.PathError + if !errors.As(err, &pathErr) { // Something went *seriously* wrong. /* Per https://pkg.go.dev/os#Stat: @@ -2672,6 +2659,20 @@ func isDirPath(fl FieldLevel) bool { */ panic(err) } + + if errors.Is(pathErr.Err, syscall.EINVAL) { + // It's definitely an invalid character in the path. + return false + } + + if strings.HasSuffix(field.String(), string(os.PathSeparator)) { + // It could be a permission error, a does-not-exist error, etc. + // Out-of-scope for this validation, though. + // Lastly, we make sure it is a directory. + return true + } else { + return false + } } // We repeat the check here to make sure it is an explicit directory in case the above os.Stat didn't trigger an error. if strings.HasSuffix(field.String(), string(os.PathSeparator)) {