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

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

Issue 1259593005: Add 'user friendly' datastore API. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: more docs 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
« no previous file with comments | « service/datastore/pls_impl.go ('k') | service/datastore/raw_interface.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 "errors" 8 "errors"
9 "fmt" 9 "fmt"
10 "math" 10 "math"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // handle the error normally, use SetValue(..., NoIndex) instead. 63 // handle the error normally, use SetValue(..., NoIndex) instead.
64 func MkPropertyNI(val interface{}) Property { 64 func MkPropertyNI(val interface{}) Property {
65 ret := Property{} 65 ret := Property{}
66 if err := ret.SetValue(val, NoIndex); err != nil { 66 if err := ret.SetValue(val, NoIndex); err != nil {
67 panic(err) 67 panic(err)
68 } 68 }
69 return ret 69 return ret
70 } 70 }
71 71
72 // PropertyConverter may be implemented by the pointer-to a struct field which 72 // PropertyConverter may be implemented by the pointer-to a struct field which
73 // is serialized by datastore. Its ToProperty will be called on save, and 73 // is serialized by the struct PropertyLoadSaver from GetPLS. Its ToProperty
74 // it's FromProperty will be called on load (from datastore). The method may 74 // will be called on save, and it's FromProperty will be called on load (from
75 // do arbitrary computation, and if it encounters an error, may return it. This 75 // datastore). The method may do arbitrary computation, and if it encounters an
76 // error will be a fatal error (as defined by PropertyLoadSaver) for the 76 // error, may return it. This error will be a fatal error (as defined by
77 // struct conversion. 77 // PropertyLoadSaver) for the struct conversion.
78 // 78 //
79 // Example: 79 // Example:
80 // type Complex complex 80 // type Complex complex
81 // func (c *Complex) ToProperty() (ret Property, err error) { 81 // func (c *Complex) ToProperty() (ret Property, err error) {
82 // // something like: 82 // // something like:
83 // err = ret.SetValue(fmt.Sprint(*c), true) 83 // err = ret.SetValue(fmt.Sprint(*c), true)
84 // return 84 // return
85 // } 85 // }
86 // func (c *Complex) FromProperty(p Property) (err error) { 86 // func (c *Complex) FromProperty(p Property) (err error) {
87 // ... load *c from p ... 87 // ... load *c from p ...
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 } 307 }
308 p.propType = pt 308 p.propType = pt
309 p.value = value 309 p.value = value
310 p.indexSetting = is 310 p.indexSetting = is
311 if t == typeOfByteSlice { 311 if t == typeOfByteSlice {
312 p.indexSetting = NoIndex 312 p.indexSetting = NoIndex
313 } 313 }
314 return 314 return
315 } 315 }
316 316
317 // PropertyLoadSaver may be implemented by a user type, and datastore will 317 // PropertyLoadSaver may be implemented by a user type, and Interface will
318 // use this interface to serialize the type instead of trying to automatically 318 // use this interface to serialize the type instead of trying to automatically
319 // create a serialization codec for it with helper.GetPLS. 319 // create a serialization codec for it with helper.GetPLS.
320 type PropertyLoadSaver interface { 320 type PropertyLoadSaver interface {
321 // Load takes the values from the given map and attempts to save them in to 321 // Load takes the values from the given map and attempts to save them in to
322 // the underlying object (usually a struct or a PropertyMap). If a fatal 322 // the underlying object (usually a struct or a PropertyMap). If a fatal
323 // error occurs, it's returned via error. If non-fatal conversion errors 323 // error occurs, it's returned via error. If non-fatal conversion errors
324 // occur, error will be a MultiError containing one or more ErrFieldMism atch 324 // occur, error will be a MultiError containing one or more ErrFieldMism atch
325 // objects. 325 // objects.
326 Load(PropertyMap) error 326 Load(PropertyMap) error
327 327
328 // Save returns the current property as a PropertyMap. if withMeta is tr ue, 328 // Save returns the current property as a PropertyMap. if withMeta is tr ue,
329 // then the PropertyMap contains all the metadata (e.g. '$meta' fields) 329 // then the PropertyMap contains all the metadata (e.g. '$meta' fields)
330 // which was held by this PropertyLoadSaver. 330 // which was held by this PropertyLoadSaver.
331 Save(withMeta bool) (PropertyMap, error) 331 Save(withMeta bool) (PropertyMap, error)
332 332
333 // GetMeta will get information about the field which has the struct tag in 333 // GetMeta will get information about the field which has the struct tag in
334 » // the form of `gae:"$<key>[,<value>]?"`. 334 » // the form of `gae:"$<key>[,<default>]?"`.
335 // 335 //
336 » // string and int64 fields will return the <value> in the struct tag, 336 » // Supported metadata types are:
337 » // converted to the appropriate type, if the field has the zero value. 337 » // int64 - may have default (ascii encoded base-10)
338 » // string - may have default
339 » // Toggle - MUST have default ("true" or "false")
340 » // Key - NO default allowed
338 // 341 //
342 // Struct fields of type Toggle (which is an Auto/On/Off) require you to
343 // specify a value of 'true' or 'false' for the default value of the str uct
344 // tag, and GetMeta will return the combined value as a regular boolean true
345 // or false value.
339 // Example: 346 // Example:
340 // type MyStruct struct { 347 // type MyStruct struct {
341 // CoolField int64 `gae:"$id,1"` 348 // CoolField int64 `gae:"$id,1"`
342 // } 349 // }
343 // val, err := helper.GetPLS(&MyStruct{}).GetMeta("id") 350 // val, err := helper.GetPLS(&MyStruct{}).GetMeta("id")
344 // // val == 1 351 // // val == 1
345 // // err == nil 352 // // err == nil
346 // 353 //
347 // val, err := helper.GetPLS(&MyStruct{10}).GetMeta("id") 354 // val, err := helper.GetPLS(&MyStruct{10}).GetMeta("id")
348 // // val == 10 355 // // val == 10
349 // // err == nil 356 // // err == nil
350 // 357 //
351 // Struct fields of type Toggle (which is an Auto/On/Off) allow you to
352 // specify a value of 'true' or 'false' for the default value of the str uct
353 // tag, and GetMeta will return the combined value as a regular boolean true
354 // or false value. If a field is Toggle, a <value> MUST be specified.
355 //
356 // Example:
357 // type MyStruct struct { 358 // type MyStruct struct {
358 // TFlag Toggle `gae:"$flag1,true"` // defaults to true 359 // TFlag Toggle `gae:"$flag1,true"` // defaults to true
359 // FFlag Toggle `gae:"$flag2,false"` // defaults to false 360 // FFlag Toggle `gae:"$flag2,false"` // defaults to false
360 // // BadFlag Toggle `gae:"$flag3"` // ILLEGAL 361 // // BadFlag Toggle `gae:"$flag3"` // ILLEGAL
361 // } 362 // }
362 GetMeta(key string) (interface{}, error) 363 GetMeta(key string) (interface{}, error)
363 364
364 // SetMeta allows you to set the current value of the meta-keyed field. 365 // SetMeta allows you to set the current value of the meta-keyed field.
365 SetMeta(key string, val interface{}) error 366 SetMeta(key string, val interface{}) error
366 367
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 return err 443 return err
443 } 444 }
444 pm["$"+key] = []Property{prop} 445 pm["$"+key] = []Property{prop}
445 return nil 446 return nil
446 } 447 }
447 448
448 // Problem implements PropertyLoadSaver.Problem. It ALWAYS returns nil. 449 // Problem implements PropertyLoadSaver.Problem. It ALWAYS returns nil.
449 func (pm PropertyMap) Problem() error { 450 func (pm PropertyMap) Problem() error {
450 return nil 451 return nil
451 } 452 }
OLDNEW
« no previous file with comments | « service/datastore/pls_impl.go ('k') | service/datastore/raw_interface.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698