OLD | NEW |
---|---|
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 "reflect" | 8 "reflect" |
9 ) | 9 ) |
10 | 10 |
11 // GetPLS resolves o into a PropertyLoadSaver. o must be a pointer to a | 11 // GetPLS resolves obj into a PropertyLoadSaver. |
12 // struct of some sort. | 12 // |
13 func GetPLS(o interface{}) PropertyLoadSaver { | 13 // obj must be a non-nil pointer to a struct of some sort. |
14 » v := reflect.ValueOf(o) | 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 | |
Vadim Sh.
2015/08/03 21:25:12
typo: the the
iannucci
2015/08/03 21:51:48
Done.
| |
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 | |
Vadim Sh.
2015/08/03 21:25:13
nit: clarify that fields are indexed by default
iannucci
2015/08/03 21:51:48
Done.
| |
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 | |
Vadim Sh.
2015/08/03 21:25:13
typo: can be used to control?
iannucci
2015/08/03 21:51:48
Done.
| |
43 // the Interface.KeyForObject* methods. The supported field types are: | |
Vadim Sh.
2015/08/03 21:25:13
KeyForObj?
iannucci
2015/08/03 21:51:48
yep, Done.
| |
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) | |
15 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { | 90 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
16 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType} } | 91 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType} } |
17 } | 92 } |
93 if v.IsNil() { | |
94 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType} } | |
95 } | |
18 v = v.Elem() | 96 v = v.Elem() |
19 » t := v.Type() | 97 » c := getCodec(v.Type()) |
98 » return &structPLS{v, c} | |
99 } | |
20 | 100 |
101 func getCodec(structType reflect.Type) *structCodec { | |
21 structCodecsMutex.RLock() | 102 structCodecsMutex.RLock() |
22 » if c, ok := structCodecs[t]; ok { | 103 » c, ok := structCodecs[structType] |
23 » » structCodecsMutex.RUnlock() | 104 » structCodecsMutex.RUnlock() |
24 » » return &structPLS{v, c} | 105 » if ok { |
106 » » return c | |
25 } | 107 } |
26 structCodecsMutex.RUnlock() | |
27 | 108 |
28 structCodecsMutex.Lock() | 109 structCodecsMutex.Lock() |
29 defer structCodecsMutex.Unlock() | 110 defer structCodecsMutex.Unlock() |
30 » return &structPLS{v, getStructCodecLocked(t)} | 111 » return getStructCodecLocked(structType) |
31 } | 112 } |
OLD | NEW |