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 gae | |
6 | |
7 import ( | |
8 "golang.org/x/net/context" | |
9 | |
10 "appengine/datastore" | |
11 | |
12 "github.com/mjibson/goon" | |
13 | |
14 "infra/gae/libs/wrapper" | |
15 ) | |
16 | |
17 // UseDS adds a wrapper.Datastore implementation to context, accessible | |
18 // by wrapper.DS(c) | |
Vadim Sh.
2015/05/24 19:01:57
wrapper.GetDS(c)?
iannucci
2015/05/24 20:04:31
ah, oops. Fixed.
| |
19 func UseDS(c context.Context) context.Context { | |
20 return wrapper.SetDSFactory(c, func(ci context.Context) wrapper.Datastor e { | |
21 return dsImpl{ctx(ci), ci} | |
Vadim Sh.
2015/05/24 19:01:57
how do you choose when to put (or not to put) & wh
iannucci
2015/05/24 20:04:31
Not sure I understand what you mean?
Vadim Sh.
2015/05/24 20:15:36
I would have written:
return &dsImpl{ctx(ci), ci}
| |
22 }) | |
23 } | |
24 | |
25 ////////// Query | |
26 | |
27 type queryImpl struct{ *datastore.Query } | |
28 | |
29 func (q queryImpl) Distinct() wrapper.DSQuery { return queryImpl{q.Query.Distinc t()} } | |
30 func (q queryImpl) End(c wrapper.DSCursor) wrapper.DSQuery { | |
31 return queryImpl{q.Query.End(c.(datastore.Cursor))} | |
32 } | |
33 func (q queryImpl) EventualConsistency() wrapper.DSQuery { | |
34 return queryImpl{q.Query.EventualConsistency()} | |
35 } | |
36 func (q queryImpl) KeysOnly() wrapper.DSQuery { return queryImpl{q. Query.KeysOnly()} } | |
37 func (q queryImpl) Limit(limit int) wrapper.DSQuery { return queryImpl{q. Query.Limit(limit)} } | |
38 func (q queryImpl) Offset(offset int) wrapper.DSQuery { return queryImpl{q. Query.Offset(offset)} } | |
39 func (q queryImpl) Order(fieldName string) wrapper.DSQuery { return queryImpl{q. Query.Order(fieldName)} } | |
40 func (q queryImpl) Start(c wrapper.DSCursor) wrapper.DSQuery { | |
41 return queryImpl{q.Query.Start(c.(datastore.Cursor))} | |
42 } | |
43 func (q queryImpl) Ancestor(ancestor *datastore.Key) wrapper.DSQuery { | |
44 return queryImpl{q.Query.Ancestor(ancestor)} | |
45 } | |
46 func (q queryImpl) Project(fieldNames ...string) wrapper.DSQuery { | |
47 return queryImpl{q.Query.Project(fieldNames...)} | |
48 } | |
49 func (q queryImpl) Filter(filterStr string, value interface{}) wrapper.DSQuery { | |
50 return queryImpl{q.Query.Filter(filterStr, value)} | |
51 } | |
52 | |
53 ////////// Iterator | |
54 | |
55 type iteratorImpl struct{ *goon.Iterator } | |
56 | |
57 func (i iteratorImpl) Cursor() (wrapper.DSCursor, error) { return i.Iterator.Cur sor() } | |
58 | |
59 ////////// Datastore | |
60 | |
61 type dsImpl struct { | |
62 *goon.Goon | |
63 c context.Context | |
64 } | |
65 | |
66 // logger | |
67 func (g dsImpl) Debugf(format string, args ...interface{}) { g.Goon.Context.De bugf(format, args...) } | |
68 func (g dsImpl) Infof(format string, args ...interface{}) { g.Goon.Context.In fof(format, args...) } | |
69 func (g dsImpl) Warningf(format string, args ...interface{}) { g.Goon.Context.Wa rningf(format, args...) } | |
70 func (g dsImpl) Errorf(format string, args ...interface{}) { g.Goon.Context.Er rorf(format, args...) } | |
71 | |
72 // Kinder | |
73 func (g dsImpl) KindNameResolver() goon.KindNameResolver { return g.Goon.K indNameResolver } | |
74 func (g dsImpl) SetKindNameResolver(knr goon.KindNameResolver) { g.Goon.KindName Resolver = knr } | |
75 | |
76 // NewKeyer | |
77 func (g dsImpl) NewKey(kind, stringID string, intID int64, parent *datastore.Key ) *datastore.Key { | |
78 return datastore.NewKey(g.Goon.Context, kind, stringID, intID, parent) | |
79 } | |
80 func (g dsImpl) NewKeyObj(obj interface{}) *datastore.Key { return g.Key(obj) } | |
81 func (g dsImpl) NewKeyObjError(obj interface{}) (*datastore.Key, error) { return g.KeyError(obj) } | |
82 | |
83 // DSQueryer | |
84 func (g dsImpl) NewQuery(kind string) wrapper.DSQuery { return queryImpl{datasto re.NewQuery(kind)} } | |
85 func (g dsImpl) Run(q wrapper.DSQuery) wrapper.DSIterator { | |
86 return iteratorImpl{g.Goon.Run(q.(queryImpl).Query)} | |
87 } | |
88 func (g dsImpl) Count(q wrapper.DSQuery) (int, error) { return g.Goon.Count(q.(q ueryImpl).Query) } | |
89 func (g dsImpl) GetAll(q wrapper.DSQuery, dst interface{}) ([]*datastore.Key, er ror) { | |
90 return g.Goon.GetAll(q.(queryImpl).Query, dst) | |
91 } | |
92 | |
93 // Transactioner | |
94 func (g dsImpl) RunInTransaction(f func(c context.Context) error, opts *datastor e.TransactionOptions) error { | |
95 return g.Goon.RunInTransaction(func(ig *goon.Goon) error { | |
96 return f(context.WithValue(g.c, goonContextKey, ig)) | |
97 }, (*datastore.TransactionOptions)(opts)) | |
98 } | |
OLD | NEW |