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