forked from go-chi/docgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
37 lines (31 loc) · 809 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package docgen
func copyDocRouter(dr DocRouter) DocRouter {
var cloneRouter func(dr DocRouter) DocRouter
var cloneRoutes func(drt DocRoutes) DocRoutes
cloneRoutes = func(drts DocRoutes) DocRoutes {
rts := DocRoutes{}
for pat, drt := range drts {
rt := DocRoute{Pattern: drt.Pattern}
if len(drt.Handlers) > 0 {
rt.Handlers = DocHandlers{}
for meth, dh := range drt.Handlers {
rt.Handlers[meth] = dh
}
}
if drt.Router != nil {
rr := cloneRouter(*drt.Router)
rt.Router = &rr
}
rts[pat] = rt
}
return rts
}
cloneRouter = func(dr DocRouter) DocRouter {
cr := DocRouter{}
cr.Middlewares = make([]DocMiddleware, len(dr.Middlewares))
copy(cr.Middlewares, dr.Middlewares)
cr.Routes = cloneRoutes(dr.Routes)
return cr
}
return cloneRouter(dr)
}