mirror of https://github.com/gofiber/fiber.git
Improved some conditions (#1386)
* simplify `u <= (1<<7)-1` to `u < (1 << 7)` * It's not recommended to use `len` for empty string, we can check with string with "" * It's not recommended to use `len` for empty string, we can check with string with "" * It's not recommended to use `len` for empty string, we can check with string with "" * It's not recommended to use `len` for empty string, we can check with string with "" * Instead Bool comparison can using simplified bool check if !var = false checking * Unnecessary use of fmt.Sprintf for value without format * For check condition two value not required ! method * nil check may not be enough for slice, better check with len * function parameters combined * When the form returns error information, the text content should not start with a capital letter or end with a punctuation mark * error var invalidPath should have name of the form errFoo, It is recommended that the error variables that are part of an API should be named * change to condition len(x), it's faster https://github.com/gofiber/fiber/pull/1386#discussion_r652369520 * Update write.go * Update write_bytes.go * Update store.go Co-authored-by: RW <rene@gofiber.io>pull/1391/head
parent
1def652e57
commit
7609117cec
|
@ -258,7 +258,7 @@ func (t *Template) Reset(template, startTag, endTag string) error {
|
|||
s = s[n+len(a):]
|
||||
n = bytes.Index(s, b)
|
||||
if n < 0 {
|
||||
return fmt.Errorf("Cannot find end tag=%q in the template=%q starting from %q", endTag, template, s)
|
||||
return fmt.Errorf("cannot find end tag=%q in the template=%q starting from %q", endTag, template, s)
|
||||
}
|
||||
|
||||
t.tags = append(t.tags, unsafeBytes2String(s[:n]))
|
||||
|
|
|
@ -212,7 +212,7 @@ func parseStatLine(line string) (*TimesStat, error) {
|
|||
return nil, errors.New("stat does not contain cpu info")
|
||||
}
|
||||
|
||||
if strings.HasPrefix(fields[0], "cpu") == false {
|
||||
if !strings.HasPrefix(fields[0], "cpu") {
|
||||
return nil, errors.New("not contain cpu")
|
||||
}
|
||||
|
||||
|
|
|
@ -140,7 +140,7 @@ func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename stri
|
|||
ret = append(ret, nic)
|
||||
}
|
||||
|
||||
if pernic == false {
|
||||
if !pernic {
|
||||
return getIOCountersAll(ret)
|
||||
}
|
||||
|
||||
|
|
|
@ -699,7 +699,7 @@ func ReadBytesBytes(b []byte, scratch []byte) (v []byte, o []byte, err error) {
|
|||
return readBytesBytes(b, scratch, false)
|
||||
}
|
||||
|
||||
func readBytesBytes(b []byte, scratch []byte, zc bool) (v []byte, o []byte, err error) {
|
||||
func readBytesBytes(b, scratch []byte, zc bool) (v, o []byte, err error) {
|
||||
l := len(b)
|
||||
if l < 1 {
|
||||
return nil, nil, ErrShortBytes
|
||||
|
@ -766,7 +766,7 @@ func readBytesBytes(b []byte, scratch []byte, zc bool) (v []byte, o []byte, err
|
|||
// Possible errors:
|
||||
// - ErrShortBytes (b not long enough)
|
||||
// - TypeError{} (object not 'bin')
|
||||
func ReadBytesZC(b []byte) (v []byte, o []byte, err error) {
|
||||
func ReadBytesZC(b []byte) (v, o []byte, err error) {
|
||||
return readBytesBytes(b, nil, true)
|
||||
}
|
||||
|
||||
|
|
|
@ -172,7 +172,7 @@ func AppendUint16(b []byte, u uint16) []byte { return AppendUint64(b, uint64(u))
|
|||
func AppendUint32(b []byte, u uint32) []byte { return AppendUint64(b, uint64(u)) }
|
||||
|
||||
// AppendBytes appends bytes to the slice as MessagePack 'bin' data
|
||||
func AppendBytes(b []byte, bts []byte) []byte {
|
||||
func AppendBytes(b, bts []byte) []byte {
|
||||
sz := len(bts)
|
||||
var o []byte
|
||||
var n int
|
||||
|
@ -229,7 +229,7 @@ func AppendString(b []byte, s string) []byte {
|
|||
|
||||
// AppendStringFromBytes appends a []byte
|
||||
// as a MessagePack 'str' to the slice 'b.'
|
||||
func AppendStringFromBytes(b []byte, str []byte) []byte {
|
||||
func AppendStringFromBytes(b, str []byte) []byte {
|
||||
sz := len(str)
|
||||
var n int
|
||||
var o []byte
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
var invalidPath = errors.New("schema: invalid path")
|
||||
var errInvalidPath = errors.New("schema: invalid path")
|
||||
|
||||
// newCache returns a new cache.
|
||||
func newCache() *cache {
|
||||
|
@ -53,13 +53,13 @@ func (c *cache) parsePath(p string, t reflect.Type) ([]pathPart, error) {
|
|||
keys := strings.Split(p, ".")
|
||||
for i := 0; i < len(keys); i++ {
|
||||
if t.Kind() != reflect.Struct {
|
||||
return nil, invalidPath
|
||||
return nil, errInvalidPath
|
||||
}
|
||||
if struc = c.get(t); struc == nil {
|
||||
return nil, invalidPath
|
||||
return nil, errInvalidPath
|
||||
}
|
||||
if field = struc.get(keys[i]); field == nil {
|
||||
return nil, invalidPath
|
||||
return nil, errInvalidPath
|
||||
}
|
||||
// Valid field. Append index.
|
||||
path = append(path, field.name)
|
||||
|
@ -72,10 +72,10 @@ func (c *cache) parsePath(p string, t reflect.Type) ([]pathPart, error) {
|
|||
// So checking i+2 is not necessary anymore.
|
||||
i++
|
||||
if i+1 > len(keys) {
|
||||
return nil, invalidPath
|
||||
return nil, errInvalidPath
|
||||
}
|
||||
if index64, err = strconv.ParseInt(keys[i], 10, 0); err != nil {
|
||||
return nil, invalidPath
|
||||
return nil, errInvalidPath
|
||||
}
|
||||
parts = append(parts, pathPart{
|
||||
path: path,
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func GetTLSConfigs() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err error) {
|
||||
func GetTLSConfigs() (serverTLSConf, clientTLSConf *tls.Config, err error) {
|
||||
// set up our CA certificate
|
||||
ca := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(2021),
|
||||
|
|
|
@ -247,7 +247,7 @@ func Test_Cache_CustomNext(t *testing.T) {
|
|||
|
||||
app.Use(New(Config{
|
||||
Next: func(c *fiber.Ctx) bool {
|
||||
return !(c.Response().StatusCode() == fiber.StatusOK)
|
||||
return c.Response().StatusCode() != fiber.StatusOK
|
||||
},
|
||||
CacheControl: true,
|
||||
}))
|
||||
|
@ -305,7 +305,7 @@ func Test_CacheHeader(t *testing.T) {
|
|||
app.Use(New(Config{
|
||||
Expiration: 10 * time.Second,
|
||||
Next: func(c *fiber.Ctx) bool {
|
||||
return !(c.Response().StatusCode() == fiber.StatusOK)
|
||||
return c.Response().StatusCode() != fiber.StatusOK
|
||||
},
|
||||
}))
|
||||
|
||||
|
|
|
@ -166,7 +166,7 @@ func Test_Query_Params(t *testing.T) {
|
|||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
|
||||
expected := fmt.Sprintf("foo=bar&baz=moz")
|
||||
expected := "foo=bar&baz=moz"
|
||||
utils.AssertEqual(t, expected, buf.String())
|
||||
}
|
||||
|
||||
|
@ -192,7 +192,7 @@ func Test_Response_Body(t *testing.T) {
|
|||
_, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
expectedGetResponse := fmt.Sprintf("Sample response body")
|
||||
expectedGetResponse := "Sample response body"
|
||||
utils.AssertEqual(t, expectedGetResponse, buf.String())
|
||||
|
||||
buf.Reset() // Reset buffer to test POST
|
||||
|
@ -200,7 +200,7 @@ func Test_Response_Body(t *testing.T) {
|
|||
_, err = app.Test(httptest.NewRequest("POST", "/test", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
expectedPostResponse := fmt.Sprintf("Post in test")
|
||||
expectedPostResponse := "Post in test"
|
||||
utils.AssertEqual(t, expectedPostResponse, buf.String())
|
||||
}
|
||||
|
||||
|
|
4
path.go
4
path.go
|
@ -96,7 +96,7 @@ func parseRoute(pattern string) routeParser {
|
|||
// addParameterMetaInfo add important meta information to the parameter segments
|
||||
// to simplify the search for the end of the parameter
|
||||
func addParameterMetaInfo(segs []*routeSegment) []*routeSegment {
|
||||
comparePart := ""
|
||||
var comparePart string
|
||||
segLen := len(segs)
|
||||
// loop from end to begin
|
||||
for i := segLen - 1; i >= 0; i-- {
|
||||
|
@ -278,7 +278,7 @@ func (routeParser *routeParser) getMatch(detectionPath, path string, params *[ma
|
|||
detectionPath, path = detectionPath[i:], path[i:]
|
||||
}
|
||||
}
|
||||
if len(detectionPath) != 0 && !partialCheck {
|
||||
if detectionPath != "" && !partialCheck {
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
|
@ -282,7 +282,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router {
|
|||
|
||||
func (app *App) registerStatic(prefix, root string, config ...Static) Router {
|
||||
// For security we want to restrict to the current work directory.
|
||||
if len(root) == 0 {
|
||||
if root == "" {
|
||||
root = "."
|
||||
}
|
||||
// Cannot have an empty prefix
|
||||
|
@ -359,7 +359,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
|
|||
fileHandler := fs.NewRequestHandler()
|
||||
handler := func(c *Ctx) error {
|
||||
// Don't execute middleware if Next returns true
|
||||
if config != nil && config[0].Next != nil && config[0].Next(c) {
|
||||
if len(config) != 0 && config[0].Next != nil && config[0].Next(c) {
|
||||
return c.Next()
|
||||
}
|
||||
// Serve file
|
||||
|
|
Loading…
Reference in New Issue