OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package user | 5 package user |
6 | 6 |
7 import ( | 7 import ( |
8 "golang.org/x/net/context" | 8 "golang.org/x/net/context" |
9 ) | 9 ) |
10 | 10 |
11 type key int | 11 type key int |
12 | 12 |
13 var serviceKey key | 13 var ( |
| 14 » serviceKey key |
| 15 » serviceFilterKey key = 1 |
| 16 ) |
14 | 17 |
15 // Factory is the function signature for factory methods compatible with | 18 // Factory is the function signature for factory methods compatible with |
16 // SetFactory. | 19 // SetFactory. |
17 type Factory func(context.Context) Interface | 20 type Factory func(context.Context) Interface |
18 | 21 |
19 // Get pulls the user service implementation from context or nil if it | 22 // Filter is the function signature for a filter user implementation. It |
20 // wasn't set. | 23 // gets the current user implementation, and returns a new user implementation |
21 func Get(c context.Context) Interface { | 24 // backed by the one passed in. |
| 25 type Filter func(context.Context, Interface) Interface |
| 26 |
| 27 // getUnfiltered gets gets the Interface implementation from context without |
| 28 // any of the filters applied. |
| 29 func getUnfiltered(c context.Context) Interface { |
22 if f, ok := c.Value(serviceKey).(Factory); ok && f != nil { | 30 if f, ok := c.Value(serviceKey).(Factory); ok && f != nil { |
23 return f(c) | 31 return f(c) |
24 } | 32 } |
25 return nil | 33 return nil |
26 } | 34 } |
27 | 35 |
| 36 func getCurFilters(c context.Context) []Filter { |
| 37 curFiltsI := c.Value(serviceFilterKey) |
| 38 if curFiltsI != nil { |
| 39 return curFiltsI.([]Filter) |
| 40 } |
| 41 return nil |
| 42 } |
| 43 |
| 44 // Get pulls the user service implementation from context or nil if it |
| 45 // wasn't set. |
| 46 func Get(c context.Context) Interface { |
| 47 ret := getUnfiltered(c) |
| 48 if ret == nil { |
| 49 return nil |
| 50 } |
| 51 for _, f := range getCurFilters(c) { |
| 52 ret = f(c, ret) |
| 53 } |
| 54 return ret |
| 55 } |
| 56 |
28 // SetFactory sets the function to produce user.Interface instances, | 57 // SetFactory sets the function to produce user.Interface instances, |
29 // as returned by the Get method. | 58 // as returned by the Get method. |
30 func SetFactory(c context.Context, f Factory) context.Context { | 59 func SetFactory(c context.Context, f Factory) context.Context { |
31 return context.WithValue(c, serviceKey, f) | 60 return context.WithValue(c, serviceKey, f) |
32 } | 61 } |
33 | 62 |
34 // Set sets the user service in this context. Useful for testing with a quick | 63 // Set sets the user service in this context. Useful for testing with a quick |
35 // mock. This is just a shorthand SetFactory invocation to set a factory which | 64 // mock. This is just a shorthand SetFactory invocation to set a factory which |
36 // always returns the same object. | 65 // always returns the same object. |
37 func Set(c context.Context, u Interface) context.Context { | 66 func Set(c context.Context, u Interface) context.Context { |
38 return SetFactory(c, func(context.Context) Interface { return u }) | 67 return SetFactory(c, func(context.Context) Interface { return u }) |
39 } | 68 } |
| 69 |
| 70 // AddFilters adds Interface filters to the context. |
| 71 func AddFilters(c context.Context, filts ...Filter) context.Context { |
| 72 if len(filts) == 0 { |
| 73 return c |
| 74 } |
| 75 cur := getCurFilters(c) |
| 76 newFilts := make([]Filter, 0, len(cur)+len(filts)) |
| 77 newFilts = append(newFilts, getCurFilters(c)...) |
| 78 newFilts = append(newFilts, filts...) |
| 79 return context.WithValue(c, serviceFilterKey, newFilts) |
| 80 } |
OLD | NEW |