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

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

Issue 1259593005: Add 'user friendly' datastore API. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: 100% coverage of new code Created 5 years, 4 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
OLDNEW
(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
54 // the GetRaw method.
55 func SetRawFactory(c context.Context, rdsf RawFactory) context.Context {
56 return context.WithValue(c, rawDatastoreKey, rdsf)
57 }
58
59 // SetRaw sets the current Datastore object in the context. Useful for testing w ith
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)
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)...)
82 newFilts = append(newFilts, filts...)
83 return context.WithValue(c, rawDatastoreFilterKey, newFilts)
84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698