@@ -22,7 +22,10 @@ const (
22
22
contextHost
23
23
contextPrefix
24
24
contextParams
25
+ contextRequest
25
26
contextMiddleware
27
+ contextScope
28
+ contextMethod
26
29
contextTime
27
30
)
28
31
@@ -49,24 +52,14 @@ func WithTime(ctx context.Context, t time.Time) context.Context {
49
52
return context .WithValue (ctx , contextTime , t )
50
53
}
51
54
52
- // WithRoute returns a context with various route parameters
53
- func WithRoute (ctx context.Context , route * Route ) context.Context {
54
- if route == nil {
55
- return ctx
56
- }
57
- if route .Label != "" {
58
- ctx = provider .WithLabel (ctx , route .Label )
59
- }
60
- if route .Host != "" {
61
- ctx = WithHost (ctx , route .Host )
62
- }
63
- if route .Prefix != "" {
64
- ctx = WithPrefix (ctx , route .Prefix )
65
- }
66
- if len (route .Parameters ) > 0 {
67
- ctx = context .WithValue (ctx , contextParams , route .Parameters )
68
- }
69
- return ctx
55
+ // WithScope returns a context with the given scooes
56
+ func WithScope (ctx context.Context , scope ... string ) context.Context {
57
+ return context .WithValue (ctx , contextScope , scope )
58
+ }
59
+
60
+ // WithMethod returns a context with the given methods
61
+ func WithMethod (ctx context.Context , method ... string ) context.Context {
62
+ return context .WithValue (ctx , contextMethod , method )
70
63
}
71
64
72
65
// WithMiddleware returns a context with the given middleware
@@ -77,6 +70,37 @@ func WithMiddleware(ctx context.Context, middleware ...server.Middleware) contex
77
70
return context .WithValue (ctx , contextMiddleware , append (Middleware (ctx ), middleware ... ))
78
71
}
79
72
73
+ // WithRoute returns a context with various route parameters
74
+ func WithRoute (ctx context.Context , route * matchedRoute ) context.Context {
75
+ if route == nil {
76
+ return ctx
77
+ }
78
+ if route .route != nil {
79
+ if route .label != "" {
80
+ ctx = provider .WithLabel (ctx , route .label )
81
+ }
82
+ if route .host != "" {
83
+ ctx = WithHost (ctx , route .host )
84
+ }
85
+ if route .prefix != "" {
86
+ ctx = WithPrefix (ctx , route .prefix )
87
+ }
88
+ if len (route .scopes ) > 0 {
89
+ ctx = WithScope (ctx , route .scopes ... )
90
+ }
91
+ if len (route .methods ) > 0 {
92
+ ctx = WithMethod (ctx , route .methods ... )
93
+ }
94
+ }
95
+ if len (route .parameters ) > 0 {
96
+ ctx = context .WithValue (ctx , contextParams , route .parameters )
97
+ }
98
+ if route .request != "" {
99
+ ctx = context .WithValue (ctx , contextRequest , route .request )
100
+ }
101
+ return ctx
102
+ }
103
+
80
104
// Host returns the host from the context, or zero value if not defined
81
105
func Host (ctx context.Context ) string {
82
106
return str (ctx , contextHost )
@@ -89,11 +113,7 @@ func Prefix(ctx context.Context) string {
89
113
90
114
// Params returns the path parameters from the context, or zero value if not defined
91
115
func Params (ctx context.Context ) []string {
92
- if value , ok := ctx .Value (contextParams ).([]string ); ok {
93
- return value
94
- } else {
95
- return nil
96
- }
116
+ return strslice (ctx , contextParams )
97
117
}
98
118
99
119
// Middleware returns a set of middleware from the context, or zero value if not defined
@@ -114,6 +134,16 @@ func Time(ctx context.Context) time.Time {
114
134
}
115
135
}
116
136
137
+ // Scope returns the stored scope or nil if not defined
138
+ func Scope (ctx context.Context ) []string {
139
+ return strslice (ctx , contextScope )
140
+ }
141
+
142
+ // Method returns the stored methods or nil if not defined
143
+ func Method (ctx context.Context ) []string {
144
+ return strslice (ctx , contextMethod )
145
+ }
146
+
117
147
////////////////////////////////////////////////////////////////////////////////
118
148
// PRIVATE METHODS
119
149
@@ -123,3 +153,10 @@ func str(ctx context.Context, key RouterContextKey) string {
123
153
}
124
154
return ""
125
155
}
156
+
157
+ func strslice (ctx context.Context , key RouterContextKey ) []string {
158
+ if value , ok := ctx .Value (key ).([]string ); ok {
159
+ return value
160
+ }
161
+ return nil
162
+ }
0 commit comments