Skip to content

Commit b6ed918

Browse files
committed
Merge branch 'master' of github.com:ant0ine/go-json-rest
2 parents 864fd10 + 00d7bc2 commit b6ed918

File tree

1 file changed

+88
-88
lines changed

1 file changed

+88
-88
lines changed

rest/trie/impl.go

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,82 @@ func (n *node) addRoute(httpMethod, pathExp string, route interface{}, usedParam
162162
return nextNode.addRoute(httpMethod, remaining, route, usedParams)
163163
}
164164

165+
func (n *node) compress() {
166+
// *splat branch
167+
if n.SplatChild != nil {
168+
n.SplatChild.compress()
169+
}
170+
// :param branch
171+
if n.ParamChild != nil {
172+
n.ParamChild.compress()
173+
}
174+
// #param branch
175+
if n.RelaxedChild != nil {
176+
n.RelaxedChild.compress()
177+
}
178+
// main branch
179+
if len(n.Children) == 0 {
180+
return
181+
}
182+
// compressable ?
183+
canCompress := true
184+
for _, node := range n.Children {
185+
if node.HttpMethodToRoute != nil || node.SplatChild != nil || node.ParamChild != nil || node.RelaxedChild != nil {
186+
canCompress = false
187+
}
188+
}
189+
// compress
190+
if canCompress {
191+
merged := map[string]*node{}
192+
for key, node := range n.Children {
193+
for gdKey, gdNode := range node.Children {
194+
mergedKey := key + gdKey
195+
merged[mergedKey] = gdNode
196+
}
197+
}
198+
n.Children = merged
199+
n.ChildrenKeyLen++
200+
n.compress()
201+
// continue
202+
} else {
203+
for _, node := range n.Children {
204+
node.compress()
205+
}
206+
}
207+
}
208+
209+
func printFPadding(padding int, format string, a ...interface{}) {
210+
for i := 0; i < padding; i++ {
211+
fmt.Print(" ")
212+
}
213+
fmt.Printf(format, a...)
214+
}
215+
216+
// Private function for now
217+
func (n *node) printDebug(level int) {
218+
level++
219+
// *splat branch
220+
if n.SplatChild != nil {
221+
printFPadding(level, "*splat\n")
222+
n.SplatChild.printDebug(level)
223+
}
224+
// :param branch
225+
if n.ParamChild != nil {
226+
printFPadding(level, ":param\n")
227+
n.ParamChild.printDebug(level)
228+
}
229+
// #param branch
230+
if n.RelaxedChild != nil {
231+
printFPadding(level, "#relaxed\n")
232+
n.RelaxedChild.printDebug(level)
233+
}
234+
// main branch
235+
for key, node := range n.Children {
236+
printFPadding(level, "\"%s\"\n", key)
237+
node.printDebug(level)
238+
}
239+
}
240+
165241
// utility for the node.findRoutes recursive method
166242

167243
type paramMatch struct {
@@ -258,82 +334,6 @@ func (n *node) find(httpMethod, path string, context *findContext) {
258334
}
259335
}
260336

261-
func (n *node) compress() {
262-
// *splat branch
263-
if n.SplatChild != nil {
264-
n.SplatChild.compress()
265-
}
266-
// :param branch
267-
if n.ParamChild != nil {
268-
n.ParamChild.compress()
269-
}
270-
// #param branch
271-
if n.RelaxedChild != nil {
272-
n.RelaxedChild.compress()
273-
}
274-
// main branch
275-
if len(n.Children) == 0 {
276-
return
277-
}
278-
// compressable ?
279-
canCompress := true
280-
for _, node := range n.Children {
281-
if node.HttpMethodToRoute != nil || node.SplatChild != nil || node.ParamChild != nil || node.RelaxedChild != nil {
282-
canCompress = false
283-
}
284-
}
285-
// compress
286-
if canCompress {
287-
merged := map[string]*node{}
288-
for key, node := range n.Children {
289-
for gdKey, gdNode := range node.Children {
290-
mergedKey := key + gdKey
291-
merged[mergedKey] = gdNode
292-
}
293-
}
294-
n.Children = merged
295-
n.ChildrenKeyLen++
296-
n.compress()
297-
// continue
298-
} else {
299-
for _, node := range n.Children {
300-
node.compress()
301-
}
302-
}
303-
}
304-
305-
func printFPadding(padding int, format string, a ...interface{}) {
306-
for i := 0; i < padding; i++ {
307-
fmt.Print(" ")
308-
}
309-
fmt.Printf(format, a...)
310-
}
311-
312-
// Private function for now
313-
func (n *node) printDebug(level int) {
314-
level++
315-
// *splat branch
316-
if n.SplatChild != nil {
317-
printFPadding(level, "*splat\n")
318-
n.SplatChild.printDebug(level)
319-
}
320-
// :param branch
321-
if n.ParamChild != nil {
322-
printFPadding(level, ":param\n")
323-
n.ParamChild.printDebug(level)
324-
}
325-
// #param branch
326-
if n.RelaxedChild != nil {
327-
printFPadding(level, "#relaxed\n")
328-
n.RelaxedChild.printDebug(level)
329-
}
330-
// main branch
331-
for key, node := range n.Children {
332-
printFPadding(level, "\"%s\"\n", key)
333-
node.printDebug(level)
334-
}
335-
}
336-
337337
type Trie struct {
338338
root *node
339339
}
@@ -350,6 +350,18 @@ func (t *Trie) AddRoute(httpMethod, pathExp string, route interface{}) error {
350350
return t.root.addRoute(httpMethod, pathExp, route, []string{})
351351
}
352352

353+
// Reduce the size of the tree, must be done after the last AddRoute.
354+
func (t *Trie) Compress() {
355+
t.root.compress()
356+
}
357+
358+
// Private function for now.
359+
func (t *Trie) printDebug() {
360+
fmt.Print("<trie>\n")
361+
t.root.printDebug(0)
362+
fmt.Print("</trie>\n")
363+
}
364+
353365
// Given a path and an http method, return all the matching routes.
354366
func (t *Trie) FindRoutes(httpMethod, path string) []*Match {
355367
context := newFindContext()
@@ -412,15 +424,3 @@ func (t *Trie) FindRoutesForPath(path string) []*Match {
412424
t.root.find("", path, context)
413425
return matches
414426
}
415-
416-
// Reduce the size of the tree, must be done after the last AddRoute.
417-
func (t *Trie) Compress() {
418-
t.root.compress()
419-
}
420-
421-
// Private function for now.
422-
func (t *Trie) printDebug() {
423-
fmt.Print("<trie>\n")
424-
t.root.printDebug(0)
425-
fmt.Print("</trie>\n")
426-
}

0 commit comments

Comments
 (0)