Skip to content

Commit e90fe8a

Browse files
authored
♻️Refactor: remove redundant field method in DefaultCtx (#3372)
* ♻️Refactor: remove redundant field method in defaultCtx * ♻️Refactor: rename getMethodINT to getMethodInt
1 parent dab20c9 commit e90fe8a

File tree

6 files changed

+56
-51
lines changed

6 files changed

+56
-51
lines changed

app.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,17 +456,29 @@ const (
456456
DefaultWriteBufferSize = 4096
457457
)
458458

459+
const (
460+
methodGet = iota
461+
methodHead
462+
methodPost
463+
methodPut
464+
methodDelete
465+
methodConnect
466+
methodOptions
467+
methodTrace
468+
methodPatch
469+
)
470+
459471
// HTTP methods enabled by default
460472
var DefaultMethods = []string{
461-
MethodGet,
462-
MethodHead,
463-
MethodPost,
464-
MethodPut,
465-
MethodDelete,
466-
MethodConnect,
467-
MethodOptions,
468-
MethodTrace,
469-
MethodPatch,
473+
methodGet: MethodGet,
474+
methodHead: MethodHead,
475+
methodPost: MethodPost,
476+
methodPut: MethodPut,
477+
methodDelete: MethodDelete,
478+
methodConnect: MethodConnect,
479+
methodOptions: MethodOptions,
480+
methodTrace: MethodTrace,
481+
methodPatch: MethodPatch,
470482
}
471483

472484
// DefaultErrorHandler that process return errors from handlers

ctx.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ type DefaultCtx struct {
6161
res *DefaultRes // Default response api reference
6262
values [maxParams]string // Route parameter values
6363
viewBindMap sync.Map // Default view map to bind template engine
64-
method string // HTTP method
6564
baseURI string // HTTP base uri
6665
pathOriginal string // Original HTTP path
6766
flashMessages redirectionMsgs // Flash messages
@@ -70,7 +69,7 @@ type DefaultCtx struct {
7069
treePathHash int // Hash of the path for the search in the tree
7170
indexRoute int // Index of the current route
7271
indexHandler int // Index of the current handler
73-
methodINT int // HTTP method INT equivalent
72+
methodInt int // HTTP method INT equivalent
7473
matched bool // Non use route matched
7574
}
7675

@@ -1006,19 +1005,17 @@ func (c *DefaultCtx) Location(path string) {
10061005
func (c *DefaultCtx) Method(override ...string) string {
10071006
if len(override) == 0 {
10081007
// Nothing to override, just return current method from context
1009-
return c.method
1008+
return c.app.method(c.methodInt)
10101009
}
10111010

10121011
method := utils.ToUpper(override[0])
1013-
mINT := c.app.methodInt(method)
1014-
if mINT == -1 {
1012+
methodInt := c.app.methodInt(method)
1013+
if methodInt == -1 {
10151014
// Provided override does not valid HTTP method, no override, return current method
1016-
return c.method
1015+
return c.app.method(c.methodInt)
10171016
}
1018-
1019-
c.method = method
1020-
c.methodINT = mINT
1021-
return c.method
1017+
c.methodInt = methodInt
1018+
return method
10221019
}
10231020

10241021
// MultipartForm parse form entries from binary.
@@ -1486,7 +1483,7 @@ func (c *DefaultCtx) Route() *Route {
14861483
return &Route{
14871484
path: c.pathOriginal,
14881485
Path: c.pathOriginal,
1489-
Method: c.method,
1486+
Method: c.Method(),
14901487
Handlers: make([]Handler, 0),
14911488
Params: make([]string, 0),
14921489
}
@@ -1919,8 +1916,7 @@ func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) {
19191916
// Set paths
19201917
c.pathOriginal = c.app.getString(fctx.URI().PathOriginal())
19211918
// Set method
1922-
c.method = c.app.getString(fctx.Request.Header.Method())
1923-
c.methodINT = c.app.methodInt(c.method)
1919+
c.methodInt = c.app.methodInt(utils.UnsafeString(fctx.Request.Header.Method()))
19241920
// Attach *fasthttp.RequestCtx to ctx
19251921
c.fasthttp = fctx
19261922
// reset base uri
@@ -1951,8 +1947,8 @@ func (c *DefaultCtx) getBody() []byte {
19511947
}
19521948

19531949
// Methods to use with next stack.
1954-
func (c *DefaultCtx) getMethodINT() int {
1955-
return c.methodINT
1950+
func (c *DefaultCtx) getMethodInt() int {
1951+
return c.methodInt
19561952
}
19571953

19581954
func (c *DefaultCtx) getIndexRoute() int {

ctx_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type CustomCtx interface {
1717
Reset(fctx *fasthttp.RequestCtx)
1818

1919
// Methods to use with next stack.
20-
getMethodINT() int
20+
getMethodInt() int
2121
getIndexRoute() int
2222
getTreePathHash() int
2323
getDetectionPath() string

ctx_interface_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helpers.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"path/filepath"
1616
"reflect"
17+
"slices"
1718
"strconv"
1819
"strings"
1920
"sync"
@@ -107,7 +108,7 @@ func (app *App) methodExist(c *DefaultCtx) bool {
107108
methods := app.config.RequestMethods
108109
for i := 0; i < len(methods); i++ {
109110
// Skip original method
110-
if c.getMethodINT() == i {
111+
if c.getMethodInt() == i {
111112
continue
112113
}
113114
// Reset stack index
@@ -151,7 +152,7 @@ func (app *App) methodExistCustom(c CustomCtx) bool {
151152
methods := app.config.RequestMethods
152153
for i := 0; i < len(methods); i++ {
153154
// Skip original method
154-
if c.getMethodINT() == i {
155+
if c.getMethodInt() == i {
155156
continue
156157
}
157158
// Reset stack index
@@ -652,39 +653,35 @@ func getBytesImmutable(s string) []byte {
652653
func (app *App) methodInt(s string) int {
653654
// For better performance
654655
if len(app.configured.RequestMethods) == 0 {
655-
// TODO: Use iota instead
656656
switch s {
657657
case MethodGet:
658-
return 0
658+
return methodGet
659659
case MethodHead:
660-
return 1
660+
return methodHead
661661
case MethodPost:
662-
return 2
662+
return methodPost
663663
case MethodPut:
664-
return 3
664+
return methodPut
665665
case MethodDelete:
666-
return 4
666+
return methodDelete
667667
case MethodConnect:
668-
return 5
668+
return methodConnect
669669
case MethodOptions:
670-
return 6
670+
return methodOptions
671671
case MethodTrace:
672-
return 7
672+
return methodTrace
673673
case MethodPatch:
674-
return 8
674+
return methodPatch
675675
default:
676676
return -1
677677
}
678678
}
679-
680679
// For method customization
681-
for i, v := range app.config.RequestMethods {
682-
if s == v {
683-
return i
684-
}
685-
}
680+
return slices.Index(app.config.RequestMethods, s)
681+
}
686682

687-
return -1
683+
func (app *App) method(methodInt int) string {
684+
return app.config.RequestMethods[methodInt]
688685
}
689686

690687
// IsMethodSafe reports whether the HTTP method is considered safe.

router.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ func (r *Route) match(detectionPath, path string, params *[maxParams]string) boo
110110

111111
func (app *App) nextCustom(c CustomCtx) (bool, error) { //nolint:unparam // bool param might be useful for testing
112112
// Get stack length
113-
tree, ok := app.treeStack[c.getMethodINT()][c.getTreePathHash()]
113+
tree, ok := app.treeStack[c.getMethodInt()][c.getTreePathHash()]
114114
if !ok {
115-
tree = app.treeStack[c.getMethodINT()][0]
115+
tree = app.treeStack[c.getMethodInt()][0]
116116
}
117117
lenr := len(tree) - 1
118118

@@ -158,9 +158,9 @@ func (app *App) nextCustom(c CustomCtx) (bool, error) { //nolint:unparam // bool
158158

159159
func (app *App) next(c *DefaultCtx) (bool, error) {
160160
// Get stack length
161-
tree, ok := app.treeStack[c.methodINT][c.treePathHash]
161+
tree, ok := app.treeStack[c.methodInt][c.treePathHash]
162162
if !ok {
163-
tree = app.treeStack[c.methodINT][0]
163+
tree = app.treeStack[c.methodInt][0]
164164
}
165165
lenTree := len(tree) - 1
166166

@@ -202,7 +202,7 @@ func (app *App) next(c *DefaultCtx) (bool, error) {
202202
}
203203

204204
// If c.Next() does not match, return 404
205-
err := NewError(StatusNotFound, "Cannot "+c.method+" "+html.EscapeString(c.pathOriginal))
205+
err := NewError(StatusNotFound, "Cannot "+c.Method()+" "+html.EscapeString(c.pathOriginal))
206206
if !c.matched && app.methodExist(c) {
207207
// If no match, scan stack again if other methods match the request
208208
// Moved from app.handler because middleware may break the route chain
@@ -221,7 +221,7 @@ func (app *App) defaultRequestHandler(rctx *fasthttp.RequestCtx) {
221221
defer app.ReleaseCtx(ctx)
222222

223223
// Check if the HTTP method is valid
224-
if ctx.methodINT == -1 {
224+
if ctx.methodInt == -1 {
225225
_ = ctx.SendStatus(StatusNotImplemented) //nolint:errcheck // Always return nil
226226
return
227227
}

0 commit comments

Comments
 (0)