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

Side by Side Diff: service/datastore/pls.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 "reflect"
9 )
10
11 // GetPLS resolves obj into a PropertyLoadSaver.
iannucci 2015/08/03 04:00:34 Decided to give this a proper docstring
12 //
13 // obj must be a non-nil pointer to a struct of some sort.
14 //
15 // By default, exported fields will be serialized to/from the datastore. If the
16 // field is not exported, it will be skipped by the serialization routines.
17 //
18 // If a field is of a non-supported type (see Property for the list of supported
19 // property types), the the resulting PropertyLoadSaver will have a non-nil
20 // Problem(). Other problems include duplicate field names (due to tagging),
21 // recursively defined structs, nested structures with multiple slices (e.g.
22 // slices of slices, either directly `[][]type` or indirectly `[]Embedded` where
23 // Embedded contains a slice.)
24 //
25 // GetPLS supports the following struct tag syntax:
26 // `gae:"fieldName[,noindex]"` -- an alternate fieldname for an exportable
27 // field. When the struct is serialized or deserialized, fieldName wil l be
28 // associated with the struct field instead of the field's Go name. This is
29 // useful when writing Go code which interfaces with appengine code written
30 // in other languages (like python) which use lowercase as their default
31 // datastore field names.
32 //
33 // A fieldName of "-" means that gae will ignore the field for all
34 // serialization/deserialization.
35 //
36 // if noindex is specified, then this field will not be indexed in the
37 // datastore, even if it was an otherwise indexable type. If fieldName is
38 // blank, and noindex is specifed, then fieldName will default to the
39 // field's actual name.
40 //
41 // `gae:"$metaKey[,<value>]` -- indicates a field is metadata. Metadata
42 // can be to control filter behavior, or to store key data when using
43 // the Interface.KeyForObject* methods. The supported field types are:
44 // - Key
45 // - int64
46 // - string
47 // - Toggle (GetMeta and SetMeta treat the field as if it were bool)
48 // in addition, int64, string and Toggle allow setting a default value
49 // in the struct field tag (the "<value>" portion).
50 //
51 // Only exported fields allow SetMeta, but all fields of appropriate type
52 // allow tagged defaults. See Examples.
53 //
54 // Example "special" structure. This is supposed to be some sort of datastore
55 // singleton object.
56 // struct secretFoo {
57 // // _id and _kind are not exported, so setting their values will not be
58 // // reflected by GetMeta.
59 // _id int64 `gae:"$id,1"`
60 // _kind string `gae:"$kind,InternalFooSingleton"`
61 //
62 // // Value is exported, so can be read and written by the PropertyLoadSaver ,
63 // // but secretFoo is shared with a python appengine module which has
64 // // stored this field as 'value' instead of 'Value'.
65 // Value int64 `gae:"value"`
66 // }
67 //
68 // Example "normal" structure that you might use in a go-only appengine app.
69 // struct User {
70 // ID string `gae:"$id"`
71 // // "kind" is automatically implied by the struct name: "User"
72 // // "parent" is nil... Users are root entities
73 //
74 // // 'Name' will serialized to the datastore in the field 'Name'
75 // Name string
76 // }
77 //
78 // struct Comment {
79 // ID int64 `gae:"$id"`
80 // // "kind" is automatically implied by the struct name: "Comment"
81 //
82 // // Parent will be enforced by the application to be a User key.
83 // Parent Key `gae:"$parent"`
84 //
85 // // 'Lines' will serialized to the datastore in the field 'Lines'
86 // Lines []string
87 // }
88 func GetPLS(obj interface{}) PropertyLoadSaver {
89 v := reflect.ValueOf(obj)
90 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
91 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType} }
92 }
93 if v.IsNil() {
94 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType} }
95 }
96 v = v.Elem()
97 c := getCodec(v.Type())
98 return &structPLS{v, c}
99 }
100
101 func getCodec(structType reflect.Type) *structCodec {
102 structCodecsMutex.RLock()
103 c, ok := structCodecs[structType]
104 structCodecsMutex.RUnlock()
105 if ok {
106 return c
107 }
108
109 structCodecsMutex.Lock()
110 defer structCodecsMutex.Unlock()
111 return getStructCodecLocked(structType)
112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698