OLD | NEW |
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 datastore | 5 package datastore |
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 rawDatastoreKey key | 14 rawDatastoreKey key |
15 rawDatastoreFilterKey key = 1 | 15 rawDatastoreFilterKey key = 1 |
16 ) | 16 ) |
17 | 17 |
18 // RawFactory is the function signature for factory methods compatible with | 18 // RawFactory is the function signature for factory methods compatible with |
19 // SetRawFactory. | 19 // SetRawFactory. |
20 type RawFactory func(context.Context) RawInterface | 20 type RawFactory func(context.Context) RawInterface |
21 | 21 |
22 // RawFilter is the function signature for a filter RDS implementation. It | 22 // RawFilter is the function signature for a RawFilter implementation. It |
23 // gets the current RDS implementation, and returns a new RDS implementation | 23 // gets the current RDS implementation, and returns a new RDS implementation |
24 // backed by the one passed in. | 24 // backed by the one passed in. |
25 type RawFilter func(context.Context, RawInterface) RawInterface | 25 type RawFilter func(context.Context, RawInterface) RawInterface |
26 | 26 |
27 // getUnfiltered gets gets the RawInterface 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) RawInterface { | 29 func getUnfiltered(c context.Context) RawInterface { |
30 if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { | 30 if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { |
31 return f(c) | 31 return f(c) |
32 } | 32 } |
(...skipping 16 matching lines...) Expand all Loading... |
49 func Get(c context.Context) Interface { | 49 func Get(c context.Context) Interface { |
50 return &datastoreImpl{GetRaw(c)} | 50 return &datastoreImpl{GetRaw(c)} |
51 } | 51 } |
52 | 52 |
53 // SetRawFactory sets the function to produce Datastore instances, as returned b
y | 53 // SetRawFactory sets the function to produce Datastore instances, as returned b
y |
54 // the GetRaw method. | 54 // the GetRaw method. |
55 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { | 55 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { |
56 return context.WithValue(c, rawDatastoreKey, rdsf) | 56 return context.WithValue(c, rawDatastoreKey, rdsf) |
57 } | 57 } |
58 | 58 |
59 // SetRaw sets the current Datastore object in the context. Useful for testing w
ith | 59 // SetRaw sets the current Datastore object in the context. Useful for testing |
60 // a quick mock. This is just a shorthand SetRawFactory invocation to set a fact
ory | 60 // with a quick mock. This is just a shorthand SetRawFactory invocation to set |
61 // which always returns the same object. | 61 // a factory which always returns the same object. |
62 func SetRaw(c context.Context, rds RawInterface) context.Context { | 62 func SetRaw(c context.Context, rds RawInterface) context.Context { |
63 return SetRawFactory(c, func(context.Context) RawInterface { return rds
}) | 63 return SetRawFactory(c, func(context.Context) RawInterface { return rds
}) |
64 } | 64 } |
65 | 65 |
66 func getCurFilters(c context.Context) []RawFilter { | 66 func getCurFilters(c context.Context) []RawFilter { |
67 curFiltsI := c.Value(rawDatastoreFilterKey) | 67 curFiltsI := c.Value(rawDatastoreFilterKey) |
68 if curFiltsI != nil { | 68 if curFiltsI != nil { |
69 return curFiltsI.([]RawFilter) | 69 return curFiltsI.([]RawFilter) |
70 } | 70 } |
71 return nil | 71 return nil |
72 } | 72 } |
73 | 73 |
74 // AddRawFilters adds RawInterface filters to the context. | 74 // AddRawFilters adds RawInterface filters to the context. |
75 func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { | 75 func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { |
76 if len(filts) == 0 { | 76 if len(filts) == 0 { |
77 return c | 77 return c |
78 } | 78 } |
79 cur := getCurFilters(c) | 79 cur := getCurFilters(c) |
80 newFilts := make([]RawFilter, 0, len(cur)+len(filts)) | 80 newFilts := make([]RawFilter, 0, len(cur)+len(filts)) |
81 newFilts = append(newFilts, getCurFilters(c)...) | 81 newFilts = append(newFilts, getCurFilters(c)...) |
82 newFilts = append(newFilts, filts...) | 82 newFilts = append(newFilts, filts...) |
83 return context.WithValue(c, rawDatastoreFilterKey, newFilts) | 83 return context.WithValue(c, rawDatastoreFilterKey, newFilts) |
84 } | 84 } |
OLD | NEW |