| 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 info | 5 package info |
| 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 infoKey key | 14 infoKey key |
| 15 infoFilterKey key = 1 | 15 infoFilterKey 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 GI implementation. It | 22 // Filter is the function signature for a filter GI implementation. It |
| 23 // gets the current GI implementation, and returns a new GI implementation | 23 // gets the current GI implementation, and returns a new GI 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(infoKey).(Factory); ok && f != nil { | 30 if f, ok := c.Value(infoKey).(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 // Get gets the Interface implementation from context. | 36 // Get gets the Interface implementation from context. |
| 37 func Get(c context.Context) Interface { | 37 func Get(c context.Context) Interface { |
| 38 ret := getUnfiltered(c) | 38 ret := getUnfiltered(c) |
| 39 if ret == nil { | 39 if ret == nil { |
| 40 return nil | 40 return nil |
| 41 } | 41 } |
| 42 for _, f := range getCurFilters(c) { | 42 for _, f := range getCurFilters(c) { |
| 43 ret = f(c, ret) | 43 ret = f(c, ret) |
| 44 } | 44 } |
| 45 » return ret | 45 » return infoImpl{ret} |
| 46 } | 46 } |
| 47 | 47 |
| 48 // SetFactory sets the function to produce Interface instances, as returned | 48 // SetFactory sets the function to produce RawInterface instances, as returned |
| 49 // by the Get method. | 49 // by the Get method. |
| 50 func SetFactory(c context.Context, gif Factory) context.Context { | 50 func SetFactory(c context.Context, gif Factory) context.Context { |
| 51 return context.WithValue(c, infoKey, gif) | 51 return context.WithValue(c, infoKey, gif) |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Set sets the current Interface object in the context. Useful for testing | 54 // Set sets the current RawInterface object in the context. Useful for testing |
| 55 // with a quick mock. This is just a shorthand SetFactory invocation to set | 55 // with a quick mock. This is just a shorthand SetFactory invocation to set |
| 56 // a factory which always returns the same object. | 56 // a factory which always returns the same object. |
| 57 func Set(c context.Context, gi Interface) context.Context { | 57 func Set(c context.Context, gi RawInterface) context.Context { |
| 58 » return SetFactory(c, func(context.Context) Interface { return gi }) | 58 » return SetFactory(c, func(context.Context) RawInterface { return gi }) |
| 59 } | 59 } |
| 60 | 60 |
| 61 func getCurFilters(c context.Context) []Filter { | 61 func getCurFilters(c context.Context) []Filter { |
| 62 curFiltsI := c.Value(infoFilterKey) | 62 curFiltsI := c.Value(infoFilterKey) |
| 63 if curFiltsI != nil { | 63 if curFiltsI != nil { |
| 64 return curFiltsI.([]Filter) | 64 return curFiltsI.([]Filter) |
| 65 } | 65 } |
| 66 return nil | 66 return nil |
| 67 } | 67 } |
| 68 | 68 |
| 69 // AddFilters adds Interface filters to the context. | 69 // AddFilters adds RawInterface filters to the context. |
| 70 func AddFilters(c context.Context, filts ...Filter) context.Context { | 70 func AddFilters(c context.Context, filts ...Filter) context.Context { |
| 71 if len(filts) == 0 { | 71 if len(filts) == 0 { |
| 72 return c | 72 return c |
| 73 } | 73 } |
| 74 cur := getCurFilters(c) | 74 cur := getCurFilters(c) |
| 75 newFilts := make([]Filter, 0, len(cur)+len(filts)) | 75 newFilts := make([]Filter, 0, len(cur)+len(filts)) |
| 76 newFilts = append(newFilts, getCurFilters(c)...) | 76 newFilts = append(newFilts, getCurFilters(c)...) |
| 77 newFilts = append(newFilts, filts...) | 77 newFilts = append(newFilts, filts...) |
| 78 return context.WithValue(c, infoFilterKey, newFilts) | 78 return context.WithValue(c, infoFilterKey, newFilts) |
| 79 } | 79 } |
| OLD | NEW |