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 "fmt" | 8 "fmt" |
9 "reflect" | 9 "reflect" |
10 | 10 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 getPM: func(slot reflect.Value) (PropertyMap, error) { | 92 getPM: func(slot reflect.Value) (PropertyMap, error) { |
93 return slot.Addr().Interface().(PropertyLoadSaver).Save(
true) | 93 return slot.Addr().Interface().(PropertyLoadSaver).Save(
true) |
94 }, | 94 }, |
95 getMetaPM: func(slot reflect.Value) PropertyMap { | 95 getMetaPM: func(slot reflect.Value) PropertyMap { |
96 return getMGS(slot.Addr().Interface()).GetAllMeta() | 96 return getMGS(slot.Addr().Interface()).GetAllMeta() |
97 }, | 97 }, |
98 setPM: func(slot reflect.Value, pm PropertyMap) error { | 98 setPM: func(slot reflect.Value, pm PropertyMap) error { |
99 return slot.Addr().Interface().(PropertyLoadSaver).Load(
pm) | 99 return slot.Addr().Interface().(PropertyLoadSaver).Load(
pm) |
100 }, | 100 }, |
101 setKey: func(slot reflect.Value, k *Key) { | 101 setKey: func(slot reflect.Value, k *Key) { |
102 » » » setKey(slot.Addr().Interface(), k) | 102 » » » PopulateKey(slot.Addr().Interface(), k) |
103 }, | 103 }, |
104 } | 104 } |
105 if et.Kind() == reflect.Map { | 105 if et.Kind() == reflect.Map { |
106 ret.newElem = func() reflect.Value { | 106 ret.newElem = func() reflect.Value { |
107 // Create a *map so that way slot.Addr() works above whe
n this is | 107 // Create a *map so that way slot.Addr() works above whe
n this is |
108 // called from Run(). Otherwise the map is 'unaddressabl
e' according | 108 // called from Run(). Otherwise the map is 'unaddressabl
e' according |
109 // to reflect. ¯\_(ツ)_/¯ | 109 // to reflect. ¯\_(ツ)_/¯ |
110 ptr := reflect.New(et) | 110 ptr := reflect.New(et) |
111 ptr.Elem().Set(reflect.MakeMap(et)) | 111 ptr.Elem().Set(reflect.MakeMap(et)) |
112 return ptr.Elem() | 112 return ptr.Elem() |
(...skipping 16 matching lines...) Expand all Loading... |
129 getPM: func(slot reflect.Value) (PropertyMap, error) { | 129 getPM: func(slot reflect.Value) (PropertyMap, error) { |
130 return slot.Interface().(PropertyLoadSaver).Save(true) | 130 return slot.Interface().(PropertyLoadSaver).Save(true) |
131 }, | 131 }, |
132 getMetaPM: func(slot reflect.Value) PropertyMap { | 132 getMetaPM: func(slot reflect.Value) PropertyMap { |
133 return getMGS(slot.Interface()).GetAllMeta() | 133 return getMGS(slot.Interface()).GetAllMeta() |
134 }, | 134 }, |
135 setPM: func(slot reflect.Value, pm PropertyMap) error { | 135 setPM: func(slot reflect.Value, pm PropertyMap) error { |
136 return slot.Interface().(PropertyLoadSaver).Load(pm) | 136 return slot.Interface().(PropertyLoadSaver).Load(pm) |
137 }, | 137 }, |
138 setKey: func(slot reflect.Value, k *Key) { | 138 setKey: func(slot reflect.Value, k *Key) { |
139 » » » setKey(slot.Interface(), k) | 139 » » » PopulateKey(slot.Interface(), k) |
140 }, | 140 }, |
141 } | 141 } |
142 if et.Kind() == reflect.Map { | 142 if et.Kind() == reflect.Map { |
143 ret.newElem = func() reflect.Value { | 143 ret.newElem = func() reflect.Value { |
144 ptr := reflect.New(et) | 144 ptr := reflect.New(et) |
145 ptr.Elem().Set(reflect.MakeMap(et)) | 145 ptr.Elem().Set(reflect.MakeMap(et)) |
146 return ptr | 146 return ptr |
147 } | 147 } |
148 } else { | 148 } else { |
149 ret.newElem = func() reflect.Value { return reflect.New(et) } | 149 ret.newElem = func() reflect.Value { return reflect.New(et) } |
(...skipping 17 matching lines...) Expand all Loading... |
167 getMetaPM: func(slot reflect.Value) PropertyMap { | 167 getMetaPM: func(slot reflect.Value) PropertyMap { |
168 if slot.Type().Implements(typeOfMGS) { | 168 if slot.Type().Implements(typeOfMGS) { |
169 return slot.Interface().(MetaGetterSetter).GetAl
lMeta() | 169 return slot.Interface().(MetaGetterSetter).GetAl
lMeta() |
170 } | 170 } |
171 return toPLS(slot).GetAllMeta() | 171 return toPLS(slot).GetAllMeta() |
172 }, | 172 }, |
173 setPM: func(slot reflect.Value, pm PropertyMap) error { | 173 setPM: func(slot reflect.Value, pm PropertyMap) error { |
174 return toPLS(slot).Load(pm) | 174 return toPLS(slot).Load(pm) |
175 }, | 175 }, |
176 setKey: func(slot reflect.Value, k *Key) { | 176 setKey: func(slot reflect.Value, k *Key) { |
177 » » » setKey(toPLS(slot), k) | 177 » » » PopulateKey(toPLS(slot), k) |
178 }, | 178 }, |
179 newElem: func() reflect.Value { | 179 newElem: func() reflect.Value { |
180 return reflect.New(et).Elem() | 180 return reflect.New(et).Elem() |
181 }, | 181 }, |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 // multiArgTypeStructPtr == []*S | 185 // multiArgTypeStructPtr == []*S |
186 func multiArgTypeStructPtr(et reflect.Type) multiArgType { | 186 func multiArgTypeStructPtr(et reflect.Type) multiArgType { |
187 cdc := getCodec(et) | 187 cdc := getCodec(et) |
(...skipping 10 matching lines...) Expand all Loading... |
198 getMetaPM: func(slot reflect.Value) PropertyMap { | 198 getMetaPM: func(slot reflect.Value) PropertyMap { |
199 if slot.Elem().Type().Implements(typeOfMGS) { | 199 if slot.Elem().Type().Implements(typeOfMGS) { |
200 return getMGS(slot.Interface()).GetAllMeta() | 200 return getMGS(slot.Interface()).GetAllMeta() |
201 } | 201 } |
202 return toPLS(slot).GetAllMeta() | 202 return toPLS(slot).GetAllMeta() |
203 }, | 203 }, |
204 setPM: func(slot reflect.Value, pm PropertyMap) error { | 204 setPM: func(slot reflect.Value, pm PropertyMap) error { |
205 return toPLS(slot).Load(pm) | 205 return toPLS(slot).Load(pm) |
206 }, | 206 }, |
207 setKey: func(slot reflect.Value, k *Key) { | 207 setKey: func(slot reflect.Value, k *Key) { |
208 » » » setKey(toPLS(slot), k) | 208 » » » PopulateKey(toPLS(slot), k) |
209 }, | 209 }, |
210 newElem: func() reflect.Value { | 210 newElem: func() reflect.Value { |
211 return reflect.New(et) | 211 return reflect.New(et) |
212 }, | 212 }, |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 // multiArgTypeInterface == []I | 216 // multiArgTypeInterface == []I |
217 func multiArgTypeInterface() multiArgType { | 217 func multiArgTypeInterface() multiArgType { |
218 return multiArgType{ | 218 return multiArgType{ |
219 getKey: func(aid, ns string, slot reflect.Value) (*Key, error) { | 219 getKey: func(aid, ns string, slot reflect.Value) (*Key, error) { |
220 return newKeyObjErr(aid, ns, slot.Elem().Interface()) | 220 return newKeyObjErr(aid, ns, slot.Elem().Interface()) |
221 }, | 221 }, |
222 getPM: func(slot reflect.Value) (PropertyMap, error) { | 222 getPM: func(slot reflect.Value) (PropertyMap, error) { |
223 return mkPLS(slot.Elem().Interface()).Save(true) | 223 return mkPLS(slot.Elem().Interface()).Save(true) |
224 }, | 224 }, |
225 getMetaPM: func(slot reflect.Value) PropertyMap { | 225 getMetaPM: func(slot reflect.Value) PropertyMap { |
226 return getMGS(slot.Elem().Interface()).GetAllMeta() | 226 return getMGS(slot.Elem().Interface()).GetAllMeta() |
227 }, | 227 }, |
228 setPM: func(slot reflect.Value, pm PropertyMap) error { | 228 setPM: func(slot reflect.Value, pm PropertyMap) error { |
229 return mkPLS(slot.Elem().Interface()).Load(pm) | 229 return mkPLS(slot.Elem().Interface()).Load(pm) |
230 }, | 230 }, |
231 setKey: func(slot reflect.Value, k *Key) { | 231 setKey: func(slot reflect.Value, k *Key) { |
232 » » » setKey(slot.Elem().Interface(), k) | 232 » » » PopulateKey(slot.Elem().Interface(), k) |
233 }, | 233 }, |
234 } | 234 } |
235 } | 235 } |
236 | 236 |
237 func newKeyObjErr(aid, ns string, src interface{}) (*Key, error) { | 237 func newKeyObjErr(aid, ns string, src interface{}) (*Key, error) { |
238 pls := getMGS(src) | 238 pls := getMGS(src) |
239 if key, _ := GetMetaDefault(pls, "key", nil).(*Key); key != nil { | 239 if key, _ := GetMetaDefault(pls, "key", nil).(*Key); key != nil { |
240 return key, nil | 240 return key, nil |
241 } | 241 } |
242 | 242 |
243 // get kind | 243 // get kind |
244 kind := GetMetaDefault(pls, "kind", "").(string) | 244 kind := GetMetaDefault(pls, "kind", "").(string) |
245 if kind == "" { | 245 if kind == "" { |
246 return nil, fmt.Errorf("unable to extract $kind from %T", src) | 246 return nil, fmt.Errorf("unable to extract $kind from %T", src) |
247 } | 247 } |
248 | 248 |
249 // get id - allow both to be default for default keys | 249 // get id - allow both to be default for default keys |
250 sid := GetMetaDefault(pls, "id", "").(string) | 250 sid := GetMetaDefault(pls, "id", "").(string) |
251 iid := GetMetaDefault(pls, "id", 0).(int64) | 251 iid := GetMetaDefault(pls, "id", 0).(int64) |
252 | 252 |
253 // get parent | 253 // get parent |
254 par, _ := GetMetaDefault(pls, "parent", nil).(*Key) | 254 par, _ := GetMetaDefault(pls, "parent", nil).(*Key) |
255 | 255 |
256 return NewKey(aid, ns, kind, sid, iid, par), nil | 256 return NewKey(aid, ns, kind, sid, iid, par), nil |
257 } | 257 } |
258 | 258 |
259 func setKey(src interface{}, key *Key) { | |
260 pls := getMGS(src) | |
261 if !pls.SetMeta("key", key) { | |
262 lst := key.LastTok() | |
263 if lst.StringID != "" { | |
264 pls.SetMeta("id", lst.StringID) | |
265 } else { | |
266 pls.SetMeta("id", lst.IntID) | |
267 } | |
268 pls.SetMeta("kind", lst.Kind) | |
269 pls.SetMeta("parent", key.Parent()) | |
270 } | |
271 } | |
272 | |
273 func mkPLS(o interface{}) PropertyLoadSaver { | 259 func mkPLS(o interface{}) PropertyLoadSaver { |
274 if pls, ok := o.(PropertyLoadSaver); ok { | 260 if pls, ok := o.(PropertyLoadSaver); ok { |
275 return pls | 261 return pls |
276 } | 262 } |
277 return GetPLS(o) | 263 return GetPLS(o) |
278 } | 264 } |
OLD | NEW |