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

Unified Diff: service/user/context.go

Issue 1500433002: Add User service. (Closed) Base URL: https://github.com/luci/gae.git@inner_ctx
Patch Set: Created 5 years 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
Index: service/user/context.go
diff --git a/service/user/context.go b/service/user/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..c86a830b7c2da737c6b1a654324e62c5da11bb63
--- /dev/null
+++ b/service/user/context.go
@@ -0,0 +1,39 @@
+// 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 user
+
+import (
+ "golang.org/x/net/context"
+)
+
+type key int
+
+var serviceKey key
+
+// Factory is the function signature for factory methods compatible with
+// SetFactory.
+type Factory func(context.Context) Interface
+
+// Get pulls the user service implementation from context or nil if it
+// wasn't set.
+func Get(c context.Context) Interface {
+ if f, ok := c.Value(serviceKey).(Factory); ok && f != nil {
+ return f(c)
+ }
+ return nil
+}
+
+// SetFactory sets the function to produce user.Interface instances,
+// as returned by the Get method.
+func SetFactory(c context.Context, f Factory) context.Context {
+ return context.WithValue(c, serviceKey, f)
+}
+
+// Set sets the user service in this 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, u Interface) context.Context {
+ return SetFactory(c, func(context.Context) Interface { return u })
+}

Powered by Google App Engine
This is Rietveld 408576698