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

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

Issue 1525923002: Implement Mail service. (Closed) Base URL: https://github.com/luci/gae.git@filter_user
Patch Set: add tests 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package user 5 package mail
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) Interface
21 21
22 // Filter is the function signature for a filter user implementation. It 22 // Filter is the function signature for a filter mail implementation. It
23 // gets the current user implementation, and returns a new user implementation 23 // gets the current mail implementation, and returns a new mail 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, Interface) Interface
26 26
27 // getUnfiltered gets gets the Interface implementation from context without 27 // getUnfiltered gets gets the Interface 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) Interface {
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 // Get pulls the mail 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 Get(c context.Context) Interface {
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 mail.Interface 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 mail 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 Interface) context.Context {
67 return SetFactory(c, func(context.Context) Interface { return u }) 67 return SetFactory(c, func(context.Context) Interface { return u })
68 } 68 }
69 69
70 // AddFilters adds Interface filters to the context. 70 // AddFilters adds Interface 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

Powered by Google App Engine
This is Rietveld 408576698