@@ -3,7 +3,6 @@ package chain
33import (
44 "context"
55 "net/http"
6- "strings"
76)
87
98type chainContextKey struct {}
@@ -32,19 +31,17 @@ type Context struct {
3231 data map [any ]any
3332 handler Handle
3433 router * Router
35- MatchedRoutePath string
34+ Route * RouteInfo
3635 Writer http.ResponseWriter
3736 Request * http.Request
3837 Crypto * cryptoImpl
39- root * Context
38+ parent * Context
39+ index int
4040 children []* Context
4141}
4242
4343// Set define um valor compartilhado no contexto de execução da requisição
4444func (ctx * Context ) Set (key any , value any ) {
45- if ctx .root != nil {
46- ctx .root .Set (key , value )
47- }
4845 if ctx .data == nil {
4946 ctx .data = make (map [any ]any )
5047 }
@@ -53,46 +50,86 @@ func (ctx *Context) Set(key any, value any) {
5350
5451// Get obtém um valor compartilhado no contexto de execução da requisição
5552func (ctx * Context ) Get (key any ) (any , bool ) {
56- if ctx .root != nil {
57- return ctx .root .Get (key )
53+ if ctx .data != nil {
54+ value , exists := ctx .data [key ]
55+ if exists {
56+ return value , exists
57+ }
5858 }
5959
60- if ctx .data = = nil {
61- return nil , false
60+ if ctx .parent ! = nil {
61+ return ctx . parent . Get ( key )
6262 }
63- value , exists := ctx .data [key ]
64- return value , exists
63+ return nil , false
6564}
6665
67- func (ctx * Context ) WithParams (names []string , values []string ) * Context {
66+ func (ctx * Context ) Destroy () {
67+ if ctx .parent == nil {
68+ // root context, will be removed automaticaly
69+ return
70+ }
71+ if ctx .parent .children != nil {
72+ ctx .parent .children [ctx .index ] = nil
73+ }
74+ ctx .parent = nil
75+ ctx .children = nil
76+
77+ if ctx .router != nil {
78+ ctx .router .poolPutContext (ctx )
79+ }
80+ }
81+
82+ func (ctx * Context ) Child () * Context {
6883 var child * Context
6984 if ctx .router != nil {
70- child = ctx .router .GetContext (ctx .Request , ctx .Writer , "" )
85+ child = ctx .router .poolGetContext (ctx .Request , ctx .Writer , "" )
7186 } else {
7287 child = & Context {
73- Writer : ctx .Writer ,
74- Request : ctx .Request ,
75- handler : ctx .handler ,
76- paramCount : len (names ),
77- paramNames : ctx .paramNames ,
78- paramValues : ctx .paramValues ,
88+ path : ctx .path ,
89+ Crypto : crypt ,
90+ Writer : ctx .Writer ,
91+ Request : ctx .Request ,
92+ handler : ctx .handler ,
7993 }
8094 }
81- for i := 0 ; i < len (names ); i ++ {
82- child .paramNames [i ] = names [i ]
83- child .paramValues [i ] = values [i ]
95+
96+ child .paramCount = ctx .paramCount
97+ child .paramNames = ctx .paramNames
98+ child .paramValues = ctx .paramValues
99+ child .pathSegments = ctx .pathSegments
100+ child .pathSegmentsCount = ctx .pathSegmentsCount
101+ child .Route = ctx .Route
102+
103+ child .parent = ctx
104+
105+ if ctx .children == nil {
106+ ctx .children = make ([]* Context , 0 )
84107 }
108+ child .index = len (ctx .children )
109+ ctx .children = append (ctx .children , child )
85110
86- if ctx .root == nil {
87- child .root = ctx
88- } else {
89- child .root = ctx .root
111+ return child
112+ }
113+
114+ // func (ctx *Context) With(key any, value any) *Context {
115+
116+ // }
117+
118+ func (ctx * Context ) WithParams (names []string , values []string ) * Context {
119+ child := ctx .Child ()
120+ child .paramCount = len (names )
121+ child .paramNames = [32 ]string {}
122+ child .paramValues = [32 ]string {}
123+
124+ for i , name := range ctx .paramNames {
125+ child .paramNames [i ] = name
126+ child .paramValues [i ] = ctx .paramValues [i ]
90127 }
91128
92- if child .root .children == nil {
93- child .root .children = make ([]* Context , 0 )
129+ for i := 0 ; i < len (names ); i ++ {
130+ child .paramNames [i ] = names [i ]
131+ child .paramValues [i ] = values [i ]
94132 }
95- child .root .children = append (child .root .children , child )
96133
97134 return child
98135}
@@ -145,30 +182,5 @@ func (ctx *Context) addParameter(name string, value string) {
145182}
146183
147184func (ctx * Context ) parsePathSegments () {
148- var (
149- segmentStart = 0
150- segmentSize int
151- path = ctx .path
152- )
153- if len (path ) > 0 {
154- path = path [1 :]
155- }
156-
157- ctx .pathSegments [0 ] = 0
158- ctx .pathSegmentsCount = 1
159-
160- for {
161- segmentSize = strings .IndexByte (path , separator )
162- if segmentSize == - 1 {
163- segmentSize = len (path )
164- }
165- ctx .pathSegments [ctx .pathSegmentsCount ] = segmentStart + 1 + segmentSize
166-
167- if segmentSize == len (path ) {
168- break
169- }
170- ctx .pathSegmentsCount ++
171- path = path [segmentSize + 1 :]
172- segmentStart = segmentStart + 1 + segmentSize
173- }
185+ ctx .pathSegmentsCount = parsePathSegments (ctx .path , & ctx .pathSegments )
174186}
0 commit comments