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

Unified Diff: service/info/context.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint 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 | « service/blobstore/types.go ('k') | service/info/interface.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/info/context.go
diff --git a/service/info/context.go b/service/info/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..a8eeeaab531d5705f5d0aa6d6ee1c687464e3925
--- /dev/null
+++ b/service/info/context.go
@@ -0,0 +1,79 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package info
+
+import (
+ "golang.org/x/net/context"
+)
+
+type key int
+
+var (
+ infoKey key
+ infoFilterKey key = 1
+)
+
+// Factory is the function signature for factory methods compatible with
+// SetFactory.
+type Factory func(context.Context) Interface
+
+// Filter 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 Filter func(context.Context, Interface) Interface
+
+// GetUnfiltered gets gets the Interface implementation from context without
+// any of the filters applied.
+func GetUnfiltered(c context.Context) Interface {
+ if f, ok := c.Value(infoKey).(Factory); ok && f != nil {
+ return f(c)
+ }
+ return nil
+}
+
+// Get gets gets the Interface implementation from context.
+func Get(c context.Context) Interface {
+ ret := GetUnfiltered(c)
+ if ret == nil {
+ return nil
+ }
+ for _, f := range getCurFilters(c) {
+ ret = f(c, ret)
+ }
+ return ret
+}
+
+// SetFactory sets the function to produce Interface instances, as returned
+// by the Get method.
+func SetFactory(c context.Context, gif Factory) context.Context {
+ return context.WithValue(c, infoKey, gif)
+}
+
+// Set sets the current Interface object in the context. Useful for testing
+// with a quick mock. This is just a shorthand SetFactory invocation to set
+// a factory which always returns the same object.
+func Set(c context.Context, gi Interface) context.Context {
+ return SetFactory(c, func(context.Context) Interface { return gi })
+}
+
+func getCurFilters(c context.Context) []Filter {
+ curFiltsI := c.Value(infoFilterKey)
+ if curFiltsI != nil {
+ return curFiltsI.([]Filter)
+ }
+ return nil
+}
+
+// AddFilters adds Interface filters to the context.
+func AddFilters(c context.Context, filts ...Filter) context.Context {
+ if len(filts) == 0 {
+ return c
+ }
+ cur := getCurFilters(c)
+ newFilts := make([]Filter, 0, len(cur)+len(filts))
+ newFilts = append(newFilts, getCurFilters(c)...)
+ newFilts = append(newFilts, filts...)
+ return context.WithValue(c, infoFilterKey, newFilts)
+}
« no previous file with comments | « service/blobstore/types.go ('k') | service/info/interface.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698