File: impl.go

package info (click to toggle)
golang-github-ant0ine-go-json-rest 3.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: makefile: 2
file content (426 lines) | stat: -rw-r--r-- 9,621 bytes parent folder | download
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Special Trie implementation for HTTP routing.
//
// This Trie implementation is designed to support strings that includes
// :param and *splat parameters. Strings that are commonly used to represent
// the Path in HTTP routing. This implementation also maintain for each Path
// a map of HTTP Methods associated with the Route.
//
// You probably don't need to use this package directly.
//
package trie

import (
	"errors"
	"fmt"
)

func splitParam(remaining string) (string, string) {
	i := 0
	for len(remaining) > i && remaining[i] != '/' && remaining[i] != '.' {
		i++
	}
	return remaining[:i], remaining[i:]
}

func splitRelaxed(remaining string) (string, string) {
	i := 0
	for len(remaining) > i && remaining[i] != '/' {
		i++
	}
	return remaining[:i], remaining[i:]
}

type node struct {
	HttpMethodToRoute map[string]interface{}

	Children       map[string]*node
	ChildrenKeyLen int

	ParamChild *node
	ParamName  string

	RelaxedChild *node
	RelaxedName  string

	SplatChild *node
	SplatName  string
}

func (n *node) addRoute(httpMethod, pathExp string, route interface{}, usedParams []string) error {

	if len(pathExp) == 0 {
		// end of the path, leaf node, update the map
		if n.HttpMethodToRoute == nil {
			n.HttpMethodToRoute = map[string]interface{}{
				httpMethod: route,
			}
			return nil
		} else {
			if n.HttpMethodToRoute[httpMethod] != nil {
				return errors.New("node.Route already set, duplicated path and method")
			}
			n.HttpMethodToRoute[httpMethod] = route
			return nil
		}
	}

	token := pathExp[0:1]
	remaining := pathExp[1:]
	var nextNode *node

	if token[0] == ':' {
		// :param case
		var name string
		name, remaining = splitParam(remaining)

		// Check param name is unique
		for _, e := range usedParams {
			if e == name {
				return errors.New(
					fmt.Sprintf("A route can't have two placeholders with the same name: %s", name),
				)
			}
		}
		usedParams = append(usedParams, name)

		if n.ParamChild == nil {
			n.ParamChild = &node{}
			n.ParamName = name
		} else {
			if n.ParamName != name {
				return errors.New(
					fmt.Sprintf(
						"Routes sharing a common placeholder MUST name it consistently: %s != %s",
						n.ParamName,
						name,
					),
				)
			}
		}
		nextNode = n.ParamChild
	} else if token[0] == '#' {
		// #param case
		var name string
		name, remaining = splitRelaxed(remaining)

		// Check param name is unique
		for _, e := range usedParams {
			if e == name {
				return errors.New(
					fmt.Sprintf("A route can't have two placeholders with the same name: %s", name),
				)
			}
		}
		usedParams = append(usedParams, name)

		if n.RelaxedChild == nil {
			n.RelaxedChild = &node{}
			n.RelaxedName = name
		} else {
			if n.RelaxedName != name {
				return errors.New(
					fmt.Sprintf(
						"Routes sharing a common placeholder MUST name it consistently: %s != %s",
						n.RelaxedName,
						name,
					),
				)
			}
		}
		nextNode = n.RelaxedChild
	} else if token[0] == '*' {
		// *splat case
		name := remaining
		remaining = ""

		// Check param name is unique
		for _, e := range usedParams {
			if e == name {
				return errors.New(
					fmt.Sprintf("A route can't have two placeholders with the same name: %s", name),
				)
			}
		}

		if n.SplatChild == nil {
			n.SplatChild = &node{}
			n.SplatName = name
		}
		nextNode = n.SplatChild
	} else {
		// general case
		if n.Children == nil {
			n.Children = map[string]*node{}
			n.ChildrenKeyLen = 1
		}
		if n.Children[token] == nil {
			n.Children[token] = &node{}
		}
		nextNode = n.Children[token]
	}

	return nextNode.addRoute(httpMethod, remaining, route, usedParams)
}

func (n *node) compress() {
	// *splat branch
	if n.SplatChild != nil {
		n.SplatChild.compress()
	}
	// :param branch
	if n.ParamChild != nil {
		n.ParamChild.compress()
	}
	// #param branch
	if n.RelaxedChild != nil {
		n.RelaxedChild.compress()
	}
	// main branch
	if len(n.Children) == 0 {
		return
	}
	// compressable ?
	canCompress := true
	for _, node := range n.Children {
		if node.HttpMethodToRoute != nil || node.SplatChild != nil || node.ParamChild != nil || node.RelaxedChild != nil {
			canCompress = false
		}
	}
	// compress
	if canCompress {
		merged := map[string]*node{}
		for key, node := range n.Children {
			for gdKey, gdNode := range node.Children {
				mergedKey := key + gdKey
				merged[mergedKey] = gdNode
			}
		}
		n.Children = merged
		n.ChildrenKeyLen++
		n.compress()
		// continue
	} else {
		for _, node := range n.Children {
			node.compress()
		}
	}
}

func printFPadding(padding int, format string, a ...interface{}) {
	for i := 0; i < padding; i++ {
		fmt.Print(" ")
	}
	fmt.Printf(format, a...)
}

// Private function for now
func (n *node) printDebug(level int) {
	level++
	// *splat branch
	if n.SplatChild != nil {
		printFPadding(level, "*splat\n")
		n.SplatChild.printDebug(level)
	}
	// :param branch
	if n.ParamChild != nil {
		printFPadding(level, ":param\n")
		n.ParamChild.printDebug(level)
	}
	// #param branch
	if n.RelaxedChild != nil {
		printFPadding(level, "#relaxed\n")
		n.RelaxedChild.printDebug(level)
	}
	// main branch
	for key, node := range n.Children {
		printFPadding(level, "\"%s\"\n", key)
		node.printDebug(level)
	}
}

// utility for the node.findRoutes recursive method

type paramMatch struct {
	name  string
	value string
}

type findContext struct {
	paramStack []paramMatch
	matchFunc  func(httpMethod, path string, node *node)
}

func newFindContext() *findContext {
	return &findContext{
		paramStack: []paramMatch{},
	}
}

func (fc *findContext) pushParams(name, value string) {
	fc.paramStack = append(
		fc.paramStack,
		paramMatch{name, value},
	)
}

func (fc *findContext) popParams() {
	fc.paramStack = fc.paramStack[:len(fc.paramStack)-1]
}

func (fc *findContext) paramsAsMap() map[string]string {
	r := map[string]string{}
	for _, param := range fc.paramStack {
		if r[param.name] != "" {
			// this is checked at addRoute time, and should never happen.
			panic(fmt.Sprintf(
				"placeholder %s already found, placeholder names should be unique per route",
				param.name,
			))
		}
		r[param.name] = param.value
	}
	return r
}

type Match struct {
	// Same Route as in AddRoute
	Route interface{}
	// map of params matched for this result
	Params map[string]string
}

func (n *node) find(httpMethod, path string, context *findContext) {

	if n.HttpMethodToRoute != nil && path == "" {
		context.matchFunc(httpMethod, path, n)
	}

	if len(path) == 0 {
		return
	}

	// *splat branch
	if n.SplatChild != nil {
		context.pushParams(n.SplatName, path)
		n.SplatChild.find(httpMethod, "", context)
		context.popParams()
	}

	// :param branch
	if n.ParamChild != nil {
		value, remaining := splitParam(path)
		context.pushParams(n.ParamName, value)
		n.ParamChild.find(httpMethod, remaining, context)
		context.popParams()
	}

	// #param branch
	if n.RelaxedChild != nil {
		value, remaining := splitRelaxed(path)
		context.pushParams(n.RelaxedName, value)
		n.RelaxedChild.find(httpMethod, remaining, context)
		context.popParams()
	}

	// main branch
	length := n.ChildrenKeyLen
	if len(path) < length {
		return
	}
	token := path[0:length]
	remaining := path[length:]
	if n.Children[token] != nil {
		n.Children[token].find(httpMethod, remaining, context)
	}
}

type Trie struct {
	root *node
}

// Instanciate a Trie with an empty node as the root.
func New() *Trie {
	return &Trie{
		root: &node{},
	}
}

// Insert the route in the Trie following or creating the nodes corresponding to the path.
func (t *Trie) AddRoute(httpMethod, pathExp string, route interface{}) error {
	return t.root.addRoute(httpMethod, pathExp, route, []string{})
}

// Reduce the size of the tree, must be done after the last AddRoute.
func (t *Trie) Compress() {
	t.root.compress()
}

// Private function for now.
func (t *Trie) printDebug() {
	fmt.Print("<trie>\n")
	t.root.printDebug(0)
	fmt.Print("</trie>\n")
}

// Given a path and an http method, return all the matching routes.
func (t *Trie) FindRoutes(httpMethod, path string) []*Match {
	context := newFindContext()
	matches := []*Match{}
	context.matchFunc = func(httpMethod, path string, node *node) {
		if node.HttpMethodToRoute[httpMethod] != nil {
			// path and method match, found a route !
			matches = append(
				matches,
				&Match{
					Route:  node.HttpMethodToRoute[httpMethod],
					Params: context.paramsAsMap(),
				},
			)
		}
	}
	t.root.find(httpMethod, path, context)
	return matches
}

// Same as FindRoutes, but return in addition a boolean indicating if the path was matched.
// Useful to return 405
func (t *Trie) FindRoutesAndPathMatched(httpMethod, path string) ([]*Match, bool) {
	context := newFindContext()
	pathMatched := false
	matches := []*Match{}
	context.matchFunc = func(httpMethod, path string, node *node) {
		pathMatched = true
		if node.HttpMethodToRoute[httpMethod] != nil {
			// path and method match, found a route !
			matches = append(
				matches,
				&Match{
					Route:  node.HttpMethodToRoute[httpMethod],
					Params: context.paramsAsMap(),
				},
			)
		}
	}
	t.root.find(httpMethod, path, context)
	return matches, pathMatched
}

// Given a path, and whatever the http method, return all the matching routes.
func (t *Trie) FindRoutesForPath(path string) []*Match {
	context := newFindContext()
	matches := []*Match{}
	context.matchFunc = func(httpMethod, path string, node *node) {
		params := context.paramsAsMap()
		for _, route := range node.HttpMethodToRoute {
			matches = append(
				matches,
				&Match{
					Route:  route,
					Params: params,
				},
			)
		}
	}
	t.root.find("", path, context)
	return matches
}