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 obj into a PropertyLoadSaver. | 11 // GetPLS resolves obj into a PropertyLoadSaver. |
12 // | 12 // |
13 // obj must be a non-nil pointer to a struct of some sort. | 13 // obj must be a non-nil pointer to a struct of some sort. |
14 // | 14 // |
15 // By default, exported fields will be serialized to/from the datastore. If the | 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. | 16 // field is not exported, it will be skipped by the serialization routines. |
17 // | 17 // |
18 // If a field is of a non-supported type (see Property for the list of supported | 18 // If a field is of a non-supported type (see Property for the list of supported |
19 // property types), the resulting PropertyLoadSaver will have a non-nil | 19 // property types), the resulting PropertyLoadSaver will have a non-nil |
20 // Problem(). Other problems include duplicate field names (due to tagging), | 20 // Problem(). Other problems include duplicate field names (due to tagging), |
21 // recursively defined structs, nested structures with multiple slices (e.g. | 21 // recursively defined structs, nested structures with multiple slices (e.g. |
22 // slices of slices, either directly `[][]type` or indirectly `[]Embedded` where | 22 // slices of slices, either directly `[][]type` or indirectly `[]Embedded` where |
23 // Embedded contains a slice.) | 23 // Embedded contains a slice.) |
24 // | 24 // |
25 // GetPLS supports the following struct tag syntax: | 25 // GetPLS supports the following struct tag syntax: |
26 // `gae:"fieldName[,noindex]"` -- an alternate fieldname for an exportable | 26 // `gae:"fieldName[,noindex]"` -- an alternate fieldname for an exportable |
27 //» field. When the struct is serialized or deserialized, fieldName wil
l be | 27 // field. When the struct is serialized or deserialized, fieldName will be |
28 // associated with the struct field instead of the field's Go name. This is | 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 | 29 // useful when writing Go code which interfaces with appengine code written |
30 // in other languages (like python) which use lowercase as their default | 30 // in other languages (like python) which use lowercase as their default |
31 //» » datastore field names. | 31 // datastore field names. |
32 // | 32 // |
33 // A fieldName of "-" means that gae will ignore the field for all | 33 // A fieldName of "-" means that gae will ignore the field for all |
34 // serialization/deserialization. | 34 // serialization/deserialization. |
35 // | 35 // |
36 // if noindex is specified, then this field will not be indexed in the | 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 | 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 | 38 // blank, and noindex is specifed, then fieldName will default to the |
39 // field's actual name. Note that by default, all fields (with indexable | 39 // field's actual name. Note that by default, all fields (with indexable |
40 // types) are indexed. | 40 // types) are indexed. |
41 // | 41 // |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // ID int64 `gae:"$id"` | 80 // ID int64 `gae:"$id"` |
81 // // "kind" is automatically implied by the struct name: "Comment" | 81 // // "kind" is automatically implied by the struct name: "Comment" |
82 // | 82 // |
83 // // Parent will be enforced by the application to be a User key. | 83 // // Parent will be enforced by the application to be a User key. |
84 // Parent Key `gae:"$parent"` | 84 // Parent Key `gae:"$parent"` |
85 // | 85 // |
86 // // 'Lines' will serialized to the datastore in the field 'Lines' | 86 // // 'Lines' will serialized to the datastore in the field 'Lines' |
87 // Lines []string | 87 // Lines []string |
88 // } | 88 // } |
89 func GetPLS(obj interface{}) PropertyLoadSaver { | 89 func GetPLS(obj interface{}) PropertyLoadSaver { |
90 » v := reflect.ValueOf(obj) | 90 » v, err := getPLSValue(obj) |
91 » if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { | 91 » if err != nil { |
92 » » return &structPLS{c: &structCodec{problem: ErrInvalidEntityType}
} | 92 » » return &structPLS{c: &structCodec{problem: err}} |
93 } | 93 } |
94 if v.IsNil() { | |
95 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType}
} | |
96 } | |
97 v = v.Elem() | |
98 c := getCodec(v.Type()) | 94 c := getCodec(v.Type()) |
99 return &structPLS{v, c} | 95 return &structPLS{v, c} |
100 } | 96 } |
101 | 97 |
| 98 func getPLSValue(obj interface{}) (reflect.Value, error) { |
| 99 v := reflect.ValueOf(obj) |
| 100 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
| 101 return reflect.Value{}, ErrInvalidEntityType |
| 102 } |
| 103 if v.IsNil() { |
| 104 return reflect.Value{}, ErrInvalidEntityType |
| 105 } |
| 106 return v.Elem(), nil |
| 107 } |
| 108 |
102 func getCodec(structType reflect.Type) *structCodec { | 109 func getCodec(structType reflect.Type) *structCodec { |
103 structCodecsMutex.RLock() | 110 structCodecsMutex.RLock() |
104 c, ok := structCodecs[structType] | 111 c, ok := structCodecs[structType] |
105 structCodecsMutex.RUnlock() | 112 structCodecsMutex.RUnlock() |
106 if ok { | 113 if ok { |
107 return c | 114 return c |
108 } | 115 } |
109 | 116 |
110 structCodecsMutex.Lock() | 117 structCodecsMutex.Lock() |
111 defer structCodecsMutex.Unlock() | 118 defer structCodecsMutex.Unlock() |
112 return getStructCodecLocked(structType) | 119 return getStructCodecLocked(structType) |
113 } | 120 } |
OLD | NEW |