Update gin.go

This commit is contained in:
hansongyu 2022-05-07 16:10:24 +08:00 committed by GitHub
parent 34172d8cd0
commit ee7c6d03b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 76 additions and 0 deletions

76
gin.go
View File

@ -337,6 +337,82 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
}
}
func (engine *Engine) delRoute(method, path string) {
assert1(path[0] == '/', "path must begin with '/'")
assert1(method != "", "HTTP method can not be empty")
root := engine.trees.get(method)
if root == nil {
return
}
target ,parent ,ok ,myself:= root.findNode(path)
if !ok{
return
}
if !myself {
parent.delChildNode(target)
}else {
root.delChildNode(target)
}
// Update maxParams
if paramsCount := countParams(path); paramsCount == engine.maxParams {
//TODO:find new maxParams
}
if sectionsCount := countSections(path); sectionsCount == engine.maxSections {
//TODO:find new maxParams
}
}
func (n *node) findNode(path string) (target *node,parent *node,found bool,myself bool){
if n.fullPath == path {
return n,nil,true,true
}
for _,child := range n.children {
if t,p,ok,m := child.findNode(path); ok {
if m {
return t,n,ok,false
}
return t,p,ok,false
}else {
continue
}
}
return nil,nil,false,false
}
func (n *node) delChildNode(target *node){
switch len(target.children) {
case 0:
for i, max := 0, len(n.indices); i < max; i++ {
if target.path[0] == n.indices[i] {
n.indices = n.indices[:i] + n.indices[i+1:]
n.children = append(n.children[:i], n.children[i+1:]...)
//TODO:update other params of n
break
}
}
case 1:
for i, max := 0, len(n.indices); i < max; i++ {
if target.path[0] == n.indices[i] {
n.indices = n.indices[:i] + string(target.children[0].path[0]) + n.indices[i+1:]
n.children[i] = target.children[0]
//TODO:update other params of n
break
}
}
default:
target.handlers = nil
//TODO:update other params of target
}
if len(n.children) == 1 {
//TODO:combine n and n.children[0]
}
}
// Routes returns a slice of registered routes, including some useful information, such as:
// the http method, path and the handler name.
func (engine *Engine) Routes() (routes RoutesInfo) {