Chromium Code Reviews| Index: go/src/infra/gae/libs/gae/globalinfo.go |
| diff --git a/go/src/infra/gae/libs/gae/globalinfo.go b/go/src/infra/gae/libs/gae/globalinfo.go |
| index afeaac492baf92da423b775dde503ad97dde7691..e3ed5199e472e7ba40413e444f2af8509e7db3d4 100644 |
| --- a/go/src/infra/gae/libs/gae/globalinfo.go |
| +++ b/go/src/infra/gae/libs/gae/globalinfo.go |
| @@ -38,14 +38,32 @@ type GlobalInfo interface { |
| // SetGIFactory. |
| type GIFactory func(context.Context) GlobalInfo |
| -// GetGI gets gets the GlobalInfo implementation from context. |
| -func GetGI(c context.Context) GlobalInfo { |
| +// GIFilter is the function signature for a filter GI implementation. It |
| +// gets the current GI implementation, and returns a new GI implementation |
| +// backed by the one passed in. |
| +type GIFilter func(context.Context, GlobalInfo) GlobalInfo |
| + |
| +// GetGIUnfiltered gets gets the GlobalInfo implementation from context without |
| +// any of the filters applied. |
| +func GetGIUnfiltered(c context.Context) GlobalInfo { |
| if f, ok := c.Value(globalInfoKey).(GIFactory); ok && f != nil { |
| return f(c) |
| } |
| return nil |
| } |
| +// GetGI gets gets the GlobalInfo implementation from context. |
| +func GetGI(c context.Context) GlobalInfo { |
| + ret := GetGIUnfiltered(c) |
| + if ret == nil { |
| + return nil |
| + } |
| + for _, f := range getCurGIFilters(c) { |
| + ret = f(c, ret) |
| + } |
| + return ret |
|
Vadim Sh.
2015/07/16 23:39:51
it would have been nice to cache it somehow
iannucci
2015/07/16 23:46:19
yeah, though I think that would make things more c
|
| +} |
| + |
| // SetGIFactory sets the function to produce GlobalInfo instances, as returned |
| // by the GetGI method. |
| func SetGIFactory(c context.Context, gif GIFactory) context.Context { |
| @@ -58,3 +76,23 @@ func SetGIFactory(c context.Context, gif GIFactory) context.Context { |
| func SetGI(c context.Context, gi GlobalInfo) context.Context { |
| return SetGIFactory(c, func(context.Context) GlobalInfo { return gi }) |
| } |
| + |
| +func getCurGIFilters(c context.Context) []GIFilter { |
| + curFiltsI := c.Value(globalInfoFilterKey) |
| + if curFiltsI != nil { |
| + return curFiltsI.([]GIFilter) |
| + } |
| + return nil |
| +} |
| + |
| +// AddGIFilters adds GlobalInfo filters to the context. |
| +func AddGIFilters(c context.Context, filts ...GIFilter) context.Context { |
| + if len(filts) == 0 { |
| + return c |
| + } |
| + cur := getCurGIFilters(c) |
| + newFilts := make([]GIFilter, 0, len(cur)+len(filts)) |
| + newFilts = append(newFilts, getCurGIFilters(c)...) |
| + newFilts = append(newFilts, filts...) |
| + return context.WithValue(c, globalInfoFilterKey, newFilts) |
| +} |