Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Unified Diff: go/src/infra/gae/libs/gae/globalinfo.go

Issue 1222983002: Add filters for RawDatastore (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@abstract
Patch Set: remove memory diff Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/gae/libs/gae/gae.infra_testing ('k') | go/src/infra/gae/libs/gae/memcache.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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)
+}
« no previous file with comments | « go/src/infra/gae/libs/gae/gae.infra_testing ('k') | go/src/infra/gae/libs/gae/memcache.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698