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

Side by Side Diff: service/user/context.go

Issue 2302743002: Interface update, per-method Contexts. (Closed)
Patch Set: Lightning talk licenses. Created 4 years, 3 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 unified diff | Download patch
« no previous file with comments | « service/taskqueue/taskqueue.go ('k') | service/user/interface.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 user 5 package user
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 serviceKey key 14 serviceKey key
15 serviceFilterKey key = 1 15 serviceFilterKey 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 user implementation. It 22 // Filter is the function signature for a filter user implementation. It
23 // gets the current user implementation, and returns a new user implementation 23 // gets the current user implementation, and returns a new user 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(serviceKey).(Factory); ok && f != nil { 30 if f, ok := c.Value(serviceKey).(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 func getCurFilters(c context.Context) []Filter { 36 func getCurFilters(c context.Context) []Filter {
37 curFiltsI := c.Value(serviceFilterKey) 37 curFiltsI := c.Value(serviceFilterKey)
38 if curFiltsI != nil { 38 if curFiltsI != nil {
39 return curFiltsI.([]Filter) 39 return curFiltsI.([]Filter)
40 } 40 }
41 return nil 41 return nil
42 } 42 }
43 43
44 // Get pulls the user service implementation from context or nil if it 44 // Raw pulls the user service implementation from context or nil if it
45 // wasn't set. 45 // wasn't set.
46 func Get(c context.Context) Interface { 46 func Raw(c context.Context) RawInterface {
47 ret := getUnfiltered(c) 47 ret := getUnfiltered(c)
48 if ret == nil { 48 if ret == nil {
49 return nil 49 return nil
50 } 50 }
51 for _, f := range getCurFilters(c) { 51 for _, f := range getCurFilters(c) {
52 ret = f(c, ret) 52 ret = f(c, ret)
53 } 53 }
54 return ret 54 return ret
55 } 55 }
56 56
57 // SetFactory sets the function to produce user.Interface instances, 57 // SetFactory sets the function to produce user.RawInterface instances,
58 // as returned by the Get method. 58 // as returned by the Get method.
59 func SetFactory(c context.Context, f Factory) context.Context { 59 func SetFactory(c context.Context, f Factory) context.Context {
60 return context.WithValue(c, serviceKey, f) 60 return context.WithValue(c, serviceKey, f)
61 } 61 }
62 62
63 // Set sets the user service in this context. Useful for testing with a quick 63 // Set sets the user service in this context. Useful for testing with a quick
64 // mock. This is just a shorthand SetFactory invocation to set a factory which 64 // mock. This is just a shorthand SetFactory invocation to set a factory which
65 // always returns the same object. 65 // always returns the same object.
66 func Set(c context.Context, u Interface) context.Context { 66 func Set(c context.Context, u RawInterface) context.Context {
67 » return SetFactory(c, func(context.Context) Interface { return u }) 67 » return SetFactory(c, func(context.Context) RawInterface { return u })
68 } 68 }
69 69
70 // AddFilters adds Interface filters to the context. 70 // AddFilters adds RawInterface filters to the context.
71 func AddFilters(c context.Context, filts ...Filter) context.Context { 71 func AddFilters(c context.Context, filts ...Filter) context.Context {
72 if len(filts) == 0 { 72 if len(filts) == 0 {
73 return c 73 return c
74 } 74 }
75 cur := getCurFilters(c) 75 cur := getCurFilters(c)
76 newFilts := make([]Filter, 0, len(cur)+len(filts)) 76 newFilts := make([]Filter, 0, len(cur)+len(filts))
77 newFilts = append(newFilts, getCurFilters(c)...) 77 newFilts = append(newFilts, getCurFilters(c)...)
78 newFilts = append(newFilts, filts...) 78 newFilts = append(newFilts, filts...)
79 return context.WithValue(c, serviceFilterKey, newFilts) 79 return context.WithValue(c, serviceFilterKey, newFilts)
80 } 80 }
OLDNEW
« no previous file with comments | « service/taskqueue/taskqueue.go ('k') | service/user/interface.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698