File: match.go

package info (click to toggle)
golang-github-zenazn-goji 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 464 kB
  • sloc: makefile: 3
file content (66 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download | duplicates (3)
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
package web

// The key used to store route Matches in the Goji environment. If this key is
// present in the environment and contains a value of type Match, routing will
// not be performed, and the Match's Handler will be used instead.
const MatchKey = "goji.web.Match"

// Match is the type of routing matches. It is inserted into C.Env under
// MatchKey when the Mux.Router middleware is invoked. If MatchKey is present at
// route dispatch time, the Handler of the corresponding Match will be called
// instead of performing routing as usual.
//
// By computing a Match and inserting it into the Goji environment as part of a
// middleware stack (see Mux.Router, for instance), it is possible to customize
// Goji's routing behavior or replace it entirely.
type Match struct {
	// Pattern is the Pattern that matched during routing. Will be nil if no
	// route matched (Handler will be set to the Mux's NotFound handler)
	Pattern Pattern
	// The Handler corresponding to the matched pattern.
	Handler Handler
}

// GetMatch returns the Match stored in the Goji environment, or an empty Match
// if none exists (valid Matches always have a Handler property).
func GetMatch(c C) Match {
	if c.Env == nil {
		return Match{}
	}
	mi, ok := c.Env[MatchKey]
	if !ok {
		return Match{}
	}
	if m, ok := mi.(Match); ok {
		return m
	}
	return Match{}
}

// RawPattern returns the PatternType that was originally passed to ParsePattern
// or any of the HTTP method functions (Get, Post, etc.).
func (m Match) RawPattern() PatternType {
	switch v := m.Pattern.(type) {
	case regexpPattern:
		return v.re
	case stringPattern:
		return v.raw
	default:
		return v
	}
}

// RawHandler returns the HandlerType that was originally passed to the HTTP
// method functions (Get, Post, etc.).
func (m Match) RawHandler() HandlerType {
	switch v := m.Handler.(type) {
	case netHTTPHandlerWrap:
		return v.Handler
	case handlerFuncWrap:
		return v.fn
	case netHTTPHandlerFuncWrap:
		return v.fn
	default:
		return v
	}
}