Skip to content

Commit 96fb4ad

Browse files
joeybloggsjoeybloggs
authored andcommitted
some code cleanup
1 parent db6a0af commit 96fb4ad

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ type Ctx struct {
6262
websocket *websocket.Conn
6363
params Params
6464
handlers HandlersChain
65+
parent Context
6566
handlerName string
6667
index int
6768
formParsed bool
6869
multipartFormParsed bool
69-
parent Context
7070
}
7171

7272
var _ context.Context = &Ctx{}

lars.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ type customHandlers map[reflect.Type]CustomHandlerFunc
112112
type LARS struct {
113113
routeGroup
114114
trees map[string]*node
115-
// router *Router
116-
117-
// mostParams used to keep track of the most amount of
118-
// params in any URL and this will set the default capacity
119-
// of eachContext Params
120-
mostParams uint8
121115

122116
// function that gets called to create the context object... is total overridable using RegisterContext
123117
contextFunc ContextFunc
@@ -132,6 +126,11 @@ type LARS struct {
132126

133127
customHandlersFuncs customHandlers
134128

129+
// mostParams used to keep track of the most amount of
130+
// params in any URL and this will set the default capacity
131+
// of eachContext Params
132+
mostParams uint8
133+
135134
// Enables automatic redirection if the current route can't be matched but a
136135
// handler for the path with (without) the trailing slash exists.
137136
// For example if /foo/ is requested but a route only exists for /foo, the
@@ -351,8 +350,6 @@ END:
351350

352351
func (l *LARS) getOptions(c *Ctx) {
353352

354-
res := c.Response()
355-
356353
if c.request.URL.Path == "*" { // check server-wide OPTIONS
357354

358355
for m := range l.trees {
@@ -361,7 +358,7 @@ func (l *LARS) getOptions(c *Ctx) {
361358
continue
362359
}
363360

364-
res.Header().Add(Allow, m)
361+
c.response.Header().Add(Allow, m)
365362
}
366363

367364
} else {
@@ -372,28 +369,26 @@ func (l *LARS) getOptions(c *Ctx) {
372369
}
373370

374371
if c.handlers, _, _ = tree.find(c.request.URL.Path, c.params); c.handlers != nil {
375-
res.Header().Add(Allow, m)
372+
c.response.Header().Add(Allow, m)
376373
}
377374
}
378375

379376
}
380377

381-
res.Header().Add(Allow, OPTIONS)
378+
c.response.Header().Add(Allow, OPTIONS)
382379
c.handlers = l.automaticOPTIONS
383380

384381
return
385382
}
386383

387384
func (l *LARS) checkMethodNotAllowed(c *Ctx) (found bool) {
388385

389-
res := c.Response()
390-
391386
for m, tree := range l.trees {
392387

393388
if m != c.request.Method {
394389
if c.handlers, _, _ = tree.find(c.request.URL.Path, c.params); c.handlers != nil {
395390
// add methods
396-
res.Header().Add(Allow, m)
391+
c.response.Header().Add(Allow, m)
397392
found = true
398393
}
399394
}

lars_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,10 @@ func requestMultiPart(method string, url string, l *LARS) (int, string) {
969969
fmt.Println("ERR COPY:", err)
970970
}
971971

972-
writer.WriteField("username", "joeybloggs")
972+
err = writer.WriteField("username", "joeybloggs")
973+
if err != nil {
974+
fmt.Println("ERR:", err)
975+
}
973976

974977
err = writer.Close()
975978
if err != nil {

node.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import "net/url"
1111
type nodeType uint8
1212

1313
const (
14-
isStatic nodeType = iota // default
15-
isRoot
14+
// isStatic nodeType = iota // default
15+
isRoot nodeType = iota + 1
1616
hasParams
1717
matchesAny
1818
)
@@ -26,12 +26,12 @@ type existingParams map[string]struct{}
2626

2727
type node struct {
2828
path string
29-
wildChild bool
30-
nType nodeType
3129
indices string
3230
children []*node
3331
handler *methodChain
3432
priority uint32
33+
nType nodeType
34+
wildChild bool
3535
}
3636

3737
func (e existingParams) Check(param string, path string) {
@@ -76,8 +76,8 @@ func (n *node) add(path string, handlerName string, handler HandlersChain) (lp u
7676

7777
var err error
7878

79-
if path == "" {
80-
path = "/"
79+
if path == blank {
80+
path = basePath
8181
}
8282

8383
existing := make(existingParams)
@@ -140,7 +140,7 @@ func (n *node) add(path string, handlerName string, handler HandlersChain) (lp u
140140
if len(path) >= len(n.path) && n.path == path[:len(n.path)] {
141141

142142
// check for longer wildcard, e.g. :name and :names
143-
if len(n.path) >= len(path) || path[len(n.path)] == '/' {
143+
if len(n.path) >= len(path) || path[len(n.path)] == slashByte {
144144
continue walk
145145
}
146146
}
@@ -153,7 +153,7 @@ func (n *node) add(path string, handlerName string, handler HandlersChain) (lp u
153153
c := path[0]
154154

155155
// slash after param
156-
if n.nType == hasParams && c == '/' && len(n.children) == 1 {
156+
if n.nType == hasParams && c == slashByte && len(n.children) == 1 {
157157
n = n.children[0]
158158
n.priority++
159159
continue walk
@@ -214,7 +214,7 @@ func (n *node) insertChild(numParams uint8, existing existingParams, path string
214214

215215
// find wildcard end (either '/' or path end)
216216
end := i + 1
217-
for end < max && path[end] != '/' {
217+
for end < max && path[end] != slashByte {
218218
switch path[end] {
219219
// the wildcard name must not contain ':' and '*'
220220
case paramByte, wildByte:
@@ -281,7 +281,7 @@ func (n *node) insertChild(numParams uint8, existing existingParams, path string
281281

282282
// currently fixed width 1 for '/'
283283
i--
284-
if path[i] != '/' {
284+
if path[i] != slashByte {
285285
panic("no / before catch-all in path '" + fullPath + "'")
286286
}
287287

@@ -353,7 +353,7 @@ walk: // Outer loop for walking the tree
353353

354354
// find param end (either '/' or path end)
355355
end := 0
356-
for end < len(path) && path[end] != '/' {
356+
for end < len(path) && path[end] != slashByte {
357357
end++
358358
}
359359

0 commit comments

Comments
 (0)