Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package datastore | |
| 6 | |
| 7 import ( | |
| 8 "golang.org/x/net/context" | |
| 9 ) | |
| 10 | |
| 11 type key int | |
| 12 | |
| 13 var ( | |
| 14 rawDatastoreKey key | |
| 15 rawDatastoreFilterKey key = 1 | |
| 16 ) | |
| 17 | |
| 18 // RawFactory is the function signature for factory methods compatible with | |
| 19 // SetRawFactory. | |
| 20 type RawFactory func(context.Context) RawInterface | |
| 21 | |
| 22 // RawFilter is the function signature for a filter RDS implementation. It | |
| 23 // gets the current RDS implementation, and returns a new RDS implementation | |
| 24 // backed by the one passed in. | |
| 25 type RawFilter func(context.Context, RawInterface) RawInterface | |
| 26 | |
| 27 // getUnfiltered gets gets the RawInterface implementation from context without | |
| 28 // any of the filters applied. | |
| 29 func getUnfiltered(c context.Context) RawInterface { | |
| 30 if f, ok := c.Value(rawDatastoreKey).(RawFactory); ok && f != nil { | |
| 31 return f(c) | |
| 32 } | |
| 33 return nil | |
| 34 } | |
| 35 | |
| 36 // GetRaw gets the RawInterface implementation from context. | |
| 37 func GetRaw(c context.Context) RawInterface { | |
| 38 ret := getUnfiltered(c) | |
| 39 if ret == nil { | |
| 40 return nil | |
| 41 } | |
| 42 for _, f := range getCurFilters(c) { | |
| 43 ret = f(c, ret) | |
| 44 } | |
| 45 return applyCheckFilter(c, ret) | |
| 46 } | |
| 47 | |
| 48 // Get gets the Interface implementation from context. | |
| 49 func Get(c context.Context) Interface { | |
| 50 return &datastoreImpl{GetRaw(c)} | |
| 51 } | |
| 52 | |
| 53 // SetRawFactory sets the function to produce Datastore instances, as returned b y | |
|
dnj (Google)
2015/08/03 18:00:18
80-character wrapping for comments.
| |
| 54 // the GetRaw method. | |
| 55 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context { | |
|
dnj (Google)
2015/08/03 18:00:18
rdsfrdsfrdsfrdsfrdsf
| |
| 56 return context.WithValue(c, rawDatastoreKey, rdsf) | |
| 57 } | |
| 58 | |
| 59 // SetRaw sets the current Datastore object in the context. Useful for testing w ith | |
|
dnj (Google)
2015/08/03 18:00:18
80 character wrapping for comments.
| |
| 60 // a quick mock. This is just a shorthand SetRawFactory invocation to set a fact ory | |
| 61 // which always returns the same object. | |
| 62 func SetRaw(c context.Context, rds RawInterface) context.Context { | |
| 63 return SetRawFactory(c, func(context.Context) RawInterface { return rds }) | |
| 64 } | |
| 65 | |
| 66 func getCurFilters(c context.Context) []RawFilter { | |
| 67 curFiltsI := c.Value(rawDatastoreFilterKey) | |
|
dnj (Google)
2015/08/03 18:00:18
Why not:
if curFiltsI, ok := c.Value(...).([]RawFi
| |
| 68 if curFiltsI != nil { | |
| 69 return curFiltsI.([]RawFilter) | |
| 70 } | |
| 71 return nil | |
| 72 } | |
| 73 | |
| 74 // AddRawFilters adds RawInterface filters to the context. | |
| 75 func AddRawFilters(c context.Context, filts ...RawFilter) context.Context { | |
| 76 if len(filts) == 0 { | |
| 77 return c | |
| 78 } | |
| 79 cur := getCurFilters(c) | |
| 80 newFilts := make([]RawFilter, 0, len(cur)+len(filts)) | |
| 81 newFilts = append(newFilts, getCurFilters(c)...) | |
|
dnj (Google)
2015/08/03 18:00:18
Use "cur" here, instead of calling getCurFilters a
| |
| 82 newFilts = append(newFilts, filts...) | |
| 83 return context.WithValue(c, rawDatastoreFilterKey, newFilts) | |
| 84 } | |
| OLD | NEW |