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 wrapper | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 | |
| 10 "golang.org/x/net/context" | |
| 11 | |
| 12 "appengine/datastore" | |
| 13 | |
| 14 "github.com/mjibson/goon" | |
| 15 ) | |
| 16 | |
| 17 /// Kinds + Keys | |
| 18 | |
| 19 // DSKinder simply allows you to resolve the datastore 'kind' of an object. | |
| 20 // See goon.Goon.Kind(). | |
| 21 type DSKinder interface { | |
| 22 Kind(src interface{}) string | |
| 23 } | |
| 24 | |
| 25 // DSKindSetter allows you to manipulate the current Kind resolution function. | |
| 26 // See goon.Goon.KindNameResolver. | |
| 27 type DSKindSetter interface { | |
| 28 KindNameResolver() goon.KindNameResolver | |
| 29 SetKindNameResolver(goon.KindNameResolver) | |
| 30 } | |
| 31 | |
| 32 // DSNewKeyer allows you to generate a new *datastore.Key in a couple ways. | |
|
Vadim Sh.
2015/05/24 19:01:56
how about aliasing datastore.Key with something to
iannucci
2015/05/24 20:04:31
Ah, yes. So I want to do that :) But it's still br
| |
| 33 // See goon.Goon.Key* for the NewKeyObj* methods, as well as datastore.NewKey. | |
| 34 type DSNewKeyer interface { | |
| 35 NewKey(kind, stringID string, intID int64, parent *datastore.Key) *datas tore.Key | |
| 36 NewKeyObj(src interface{}) *datastore.Key | |
| 37 NewKeyObjError(src interface{}) (*datastore.Key, error) | |
| 38 } | |
| 39 | |
| 40 /// Read + Write | |
| 41 | |
| 42 // DSSingleReadWriter allows you to read and write a single datastore object. | |
| 43 // See goon.Goon for more detail on the same-named functions. | |
| 44 type DSSingleReadWriter interface { | |
| 45 Put(src interface{}) (*datastore.Key, error) | |
| 46 Get(dst interface{}) error | |
| 47 Delete(key *datastore.Key) error | |
| 48 } | |
| 49 | |
| 50 // DSMultiReadWriter allows you to read and write a multiple datastore objects | |
| 51 // in a non-atomic batch. See goon.Goon for more detail on the same-named. | |
| 52 // functions. Also implies DSSingleReadWriter. | |
| 53 type DSMultiReadWriter interface { | |
| 54 DSSingleReadWriter | |
| 55 DeleteMulti(keys []*datastore.Key) error | |
| 56 GetMulti(dst interface{}) error | |
| 57 PutMulti(src interface{}) ([]*datastore.Key, error) | |
| 58 } | |
| 59 | |
| 60 /// Queries | |
| 61 | |
| 62 // DSCursor wraps datastore.Cursor. | |
|
Vadim Sh.
2015/05/24 19:01:56
I don't see it wrapping it...
iannucci
2015/05/24 20:04:31
wraps meaning that it serves the function of (and
| |
| 63 type DSCursor interface { | |
| 64 fmt.Stringer | |
| 65 } | |
| 66 | |
| 67 // DSIterator wraps datastore.Iterator. | |
|
Vadim Sh.
2015/05/24 19:01:56
how about moving all these interfaces to package w
iannucci
2015/05/24 20:04:31
Datastore, TaskQueue and Memcache are all intertwi
| |
| 68 type DSIterator interface { | |
| 69 Cursor() (DSCursor, error) | |
| 70 Next(dst interface{}) (*datastore.Key, error) | |
| 71 } | |
| 72 | |
| 73 // DSQuery wraps datastore.Query. | |
| 74 type DSQuery interface { | |
| 75 Ancestor(ancestor *datastore.Key) DSQuery | |
| 76 Distinct() DSQuery | |
| 77 End(c DSCursor) DSQuery | |
| 78 EventualConsistency() DSQuery | |
| 79 Filter(filterStr string, value interface{}) DSQuery | |
| 80 KeysOnly() DSQuery | |
| 81 Limit(limit int) DSQuery | |
| 82 Offset(offset int) DSQuery | |
| 83 Order(fieldName string) DSQuery | |
| 84 Project(fieldNames ...string) DSQuery | |
| 85 Start(c DSCursor) DSQuery | |
| 86 } | |
| 87 | |
| 88 // DSQueryer implements all query-related functionality of goon.Goon/datastore. | |
| 89 type DSQueryer interface { | |
| 90 NewQuery(kind string) DSQuery | |
| 91 Run(q DSQuery) DSIterator | |
| 92 GetAll(q DSQuery, dst interface{}) ([]*datastore.Key, error) | |
| 93 Count(q DSQuery) (int, error) | |
| 94 } | |
| 95 | |
| 96 /// Transactions | |
| 97 | |
| 98 // DSTransactioner implements the function for entering a transaction with | |
| 99 // datastore. The function recieves a context without the ability to enter a | |
| 100 // transaction again (since recursive transactions are not implemented in | |
| 101 // datastore). | |
| 102 type DSTransactioner interface { | |
| 103 RunInTransaction(f func(c context.Context) error, opts *datastore.Transa ctionOptions) error | |
| 104 } | |
| 105 | |
| 106 // Datastore implements the full datastore functionality. | |
| 107 type Datastore interface { | |
| 108 DSKinder | |
| 109 DSNewKeyer | |
| 110 DSMultiReadWriter | |
| 111 DSQueryer | |
| 112 DSKindSetter | |
| 113 DSTransactioner | |
| 114 } | |
| 115 | |
| 116 // DSFactory is the function signature for factory methods compatible with | |
| 117 // SetDSFactory. | |
| 118 type DSFactory func(context.Context) Datastore | |
| 119 | |
| 120 // GetDS gets the Datastore implementation from context. | |
| 121 func GetDS(c context.Context) Datastore { | |
| 122 obj := c.Value(datastoreKey) | |
| 123 if obj == nil || obj.(DSFactory) == nil { | |
| 124 return nil | |
| 125 } | |
| 126 return obj.(DSFactory)(c) | |
| 127 } | |
| 128 | |
| 129 // SetDSFactory sets the function to produce Datastore instances, as returned by | |
| 130 // the GetDS method. | |
| 131 func SetDSFactory(c context.Context, dsf DSFactory) context.Context { | |
| 132 return context.WithValue(c, datastoreKey, dsf) | |
| 133 } | |
| 134 | |
| 135 // SetDS sets the current Datastore object in the context. Useful for testing | |
| 136 // with a quick mock. This is just a shorthand SetDSFactory invocation to set | |
| 137 // a factory which always returns the same object. | |
| 138 func SetDS(c context.Context, ds Datastore) context.Context { | |
| 139 return SetDSFactory(c, func(context.Context) Datastore { return ds }) | |
| 140 } | |
| OLD | NEW |