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

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

Issue 1398103003: Add Exists and ExistsMulti convenience methods (Closed) Base URL: https://github.com/luci/gae.git@add_version_id
Patch Set: comments Created 5 years, 2 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
« no previous file with comments | « no previous file | service/datastore/datastore_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "fmt" 8 "fmt"
9 "reflect" 9 "reflect"
10 10
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 func isOkType(v reflect.Type) bool { 175 func isOkType(v reflect.Type) bool {
176 if v.Implements(typeOfPropertyLoadSaver) { 176 if v.Implements(typeOfPropertyLoadSaver) {
177 return true 177 return true
178 } 178 }
179 if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct { 179 if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
180 return true 180 return true
181 } 181 }
182 return false 182 return false
183 } 183 }
184 184
185 func (d *datastoreImpl) ExistsMulti(keys []*Key) ([]bool, error) {
186 lme := errors.NewLazyMultiError(len(keys))
187 ret := make([]bool, len(keys))
188 i := 0
189 err := d.RawInterface.GetMulti(keys, nil, func(_ PropertyMap, err error) {
190 if err == nil {
191 ret[i] = true
192 } else if err != ErrNoSuchEntity {
193 lme.Assign(i, err)
194 }
195 i++
196 })
197 if err != nil {
198 return ret, err
199 }
200 return ret, lme.Get()
201 }
202
203 func (d *datastoreImpl) Exists(k *Key) (bool, error) {
204 ret, err := d.ExistsMulti([]*Key{k})
205 return ret[0], errors.SingleError(err)
206 }
207
185 func (d *datastoreImpl) Get(dst interface{}) (err error) { 208 func (d *datastoreImpl) Get(dst interface{}) (err error) {
186 if !isOkType(reflect.TypeOf(dst)) { 209 if !isOkType(reflect.TypeOf(dst)) {
187 return fmt.Errorf("invalid Get input type: %T", dst) 210 return fmt.Errorf("invalid Get input type: %T", dst)
188 } 211 }
189 return errors.SingleError(d.GetMulti([]interface{}{dst})) 212 return errors.SingleError(d.GetMulti([]interface{}{dst}))
190 } 213 }
191 214
192 func (d *datastoreImpl) Put(src interface{}) (err error) { 215 func (d *datastoreImpl) Put(src interface{}) (err error) {
193 if !isOkType(reflect.TypeOf(src)) { 216 if !isOkType(reflect.TypeOf(src)) {
194 return fmt.Errorf("invalid Put input type: %T", src) 217 return fmt.Errorf("invalid Put input type: %T", src)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 err = lme.Get() 289 err = lme.Get()
267 if err == nil { 290 if err == nil {
268 err = extErr 291 err = extErr
269 } 292 }
270 return 293 return
271 } 294 }
272 295
273 func (d *datastoreImpl) Raw() RawInterface { 296 func (d *datastoreImpl) Raw() RawInterface {
274 return d.RawInterface 297 return d.RawInterface
275 } 298 }
OLDNEW
« no previous file with comments | « no previous file | service/datastore/datastore_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698