| 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 // Factory is the function signature for factory methods compatible with | 18 // RawFactory is the function signature for factory methods compatible with |
| 19 // SetFactory. | 19 // SetRawFactory. |
| 20 type Factory func(context.Context) Interface | 20 type RawFactory func(context.Context) RawInterface |
| 21 | 21 |
| 22 // Filter is the function signature for a filter RDS implementation. It | 22 // RawFilter is the function signature for a filter RDS 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 Filter func(context.Context, Interface) Interface | 25 type RawFilter 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(rawDatastoreKey).(Factory); ok && f != nil { | 30 » if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { |
| 31 return f(c) | 31 return f(c) |
| 32 } | 32 } |
| 33 return nil | 33 return nil |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Get gets the Interface implementation from context. | 36 // GetRaw gets the RawInterface implementation from context. |
| 37 func Get(c context.Context) Interface { | 37 func GetRaw(c context.Context) RawInterface { |
| 38 ret := getUnfiltered(c) | 38 ret := getUnfiltered(c) |
| 39 if ret == nil { | 39 if ret == nil { |
| 40 return nil | 40 return nil |
| 41 } | 41 } |
| 42 for _, f := range getCurFilters(c) { | 42 for _, f := range getCurFilters(c) { |
| 43 ret = f(c, ret) | 43 ret = f(c, ret) |
| 44 } | 44 } |
| 45 return applyCheckFilter(c, ret) | 45 return applyCheckFilter(c, ret) |
| 46 } | 46 } |
| 47 | 47 |
| 48 // SetFactory sets the function to produce Datastore instances, as returned by | 48 // Get gets the Interface implementation from context. |
| 49 // the Get method. | 49 func Get(c context.Context) Interface { |
| 50 func SetFactory(c context.Context, rdsf Factory) context.Context { | 50 » return &datastoreImpl{GetRaw(c)} |
| 51 } |
| 52 |
| 53 // SetRawFactory sets the function to produce Datastore instances, as returned b
y |
| 54 // the GetRaw method. |
| 55 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { |
| 51 return context.WithValue(c, rawDatastoreKey, rdsf) | 56 return context.WithValue(c, rawDatastoreKey, rdsf) |
| 52 } | 57 } |
| 53 | 58 |
| 54 // Set sets the current Datastore object in the context. Useful for testing with | 59 // SetRaw sets the current Datastore object in the context. Useful for testing w
ith |
| 55 // a quick mock. This is just a shorthand SetFactory invocation to set a factory | 60 // a quick mock. This is just a shorthand SetRawFactory invocation to set a fact
ory |
| 56 // which always returns the same object. | 61 // which always returns the same object. |
| 57 func Set(c context.Context, rds Interface) context.Context { | 62 func SetRaw(c context.Context, rds RawInterface) context.Context { |
| 58 » return SetFactory(c, func(context.Context) Interface { return rds }) | 63 » return SetRawFactory(c, func(context.Context) RawInterface { return rds
}) |
| 59 } | 64 } |
| 60 | 65 |
| 61 func getCurFilters(c context.Context) []Filter { | 66 func getCurFilters(c context.Context) []RawFilter { |
| 62 curFiltsI := c.Value(rawDatastoreFilterKey) | 67 curFiltsI := c.Value(rawDatastoreFilterKey) |
| 63 if curFiltsI != nil { | 68 if curFiltsI != nil { |
| 64 » » return curFiltsI.([]Filter) | 69 » » return curFiltsI.([]RawFilter) |
| 65 } | 70 } |
| 66 return nil | 71 return nil |
| 67 } | 72 } |
| 68 | 73 |
| 69 // AddFilters adds Interface filters to the context. | 74 // AddRawFilters adds RawInterface filters to the context. |
| 70 func AddFilters(c context.Context, filts ...Filter) context.Context { | 75 func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { |
| 71 if len(filts) == 0 { | 76 if len(filts) == 0 { |
| 72 return c | 77 return c |
| 73 } | 78 } |
| 74 cur := getCurFilters(c) | 79 cur := getCurFilters(c) |
| 75 » newFilts := make([]Filter, 0, len(cur)+len(filts)) | 80 » newFilts := make([]RawFilter, 0, len(cur)+len(filts)) |
| 76 newFilts = append(newFilts, getCurFilters(c)...) | 81 newFilts = append(newFilts, getCurFilters(c)...) |
| 77 newFilts = append(newFilts, filts...) | 82 newFilts = append(newFilts, filts...) |
| 78 return context.WithValue(c, rawDatastoreFilterKey, newFilts) | 83 return context.WithValue(c, rawDatastoreFilterKey, newFilts) |
| 79 } | 84 } |
| OLD | NEW |