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 // HEAVILY adapted from github.com/golang/appengine/datastore | 5 // HEAVILY adapted from github.com/golang/appengine/datastore |
6 | 6 |
7 package helper | 7 package helper |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | |
11 "reflect" | 10 "reflect" |
12 | 11 |
13 "infra/gae/libs/gae" | 12 "infra/gae/libs/gae" |
14 ) | 13 ) |
15 | 14 |
16 // GetStructPLS resolves o into a gae.DSStructPLS. o must be a pointer to a | 15 // GetPLS resolves o into a gae.DSStructPLS. o must be a pointer to a |
17 // struct of some sort. | 16 // struct of some sort. |
18 func GetStructPLS(o interface{}) gae.DSStructPLS { | 17 func GetPLS(o interface{}) gae.DSPropertyLoadSaver { |
iannucci
2015/07/14 18:15:43
To simplify things, I've merged the 'DSStructPLS'
| |
19 v := reflect.ValueOf(o) | 18 v := reflect.ValueOf(o) |
20 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { | 19 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
21 return &structPLS{c: &structCodec{problem: gae.ErrDSInvalidEntit yType}} | 20 return &structPLS{c: &structCodec{problem: gae.ErrDSInvalidEntit yType}} |
22 } | 21 } |
23 v = v.Elem() | 22 v = v.Elem() |
24 t := v.Type() | 23 t := v.Type() |
25 | 24 |
26 structCodecsMutex.RLock() | 25 structCodecsMutex.RLock() |
27 if c, ok := structCodecs[t]; ok { | 26 if c, ok := structCodecs[t]; ok { |
28 structCodecsMutex.RUnlock() | 27 structCodecsMutex.RUnlock() |
29 return &structPLS{v, c} | 28 return &structPLS{v, c} |
30 } | 29 } |
31 structCodecsMutex.RUnlock() | 30 structCodecsMutex.RUnlock() |
32 | 31 |
33 structCodecsMutex.Lock() | 32 structCodecsMutex.Lock() |
34 defer structCodecsMutex.Unlock() | 33 defer structCodecsMutex.Unlock() |
35 return &structPLS{v, getStructCodecLocked(t)} | 34 return &structPLS{v, getStructCodecLocked(t)} |
36 } | 35 } |
37 | |
38 // GetPLS resolves o into a gae.DSPropertyLoadSaver. If o implements the | |
39 // gae.DSPropertyLoadSaver interface already, it's returned unchanged. Otherwise | |
40 // this calls GetStructPLS and returns the result. | |
41 func GetPLS(o interface{}) (gae.DSPropertyLoadSaver, error) { | |
42 if pls, ok := o.(gae.DSPropertyLoadSaver); ok { | |
43 return pls, nil | |
44 } | |
45 pls := GetStructPLS(o) | |
46 if err := pls.Problem(); err != nil { | |
47 return nil, err | |
48 } | |
49 return pls, nil | |
50 } | |
51 | |
52 // MultiGetPLS resolves os to a []gae.DSPropertyLoadSaver. os must be a slice of | |
53 // something. If os is already a []gae.DSPropertyLoadSaver, it's returned | |
54 // unchanged. Otherwise this calls GetPLS on each item, and composes the | |
55 // resulting slice that way. | |
56 func MultiGetPLS(os interface{}) ([]gae.DSPropertyLoadSaver, error) { | |
57 if plss, ok := os.([]gae.DSPropertyLoadSaver); ok { | |
58 return plss, nil | |
59 } | |
60 | |
61 v := reflect.ValueOf(os) | |
62 if v.Kind() != reflect.Slice { | |
63 return nil, fmt.Errorf("gae/helper: bad type in MultiObjToPLS: % T", os) | |
64 } | |
65 | |
66 // TODO(riannucci): define a higher-level type DSMultiPropertyLoadSaver | |
67 // to avoid this slice? | |
68 ret := make([]gae.DSPropertyLoadSaver, v.Len()) | |
69 for i := 0; i < v.Len(); i++ { | |
70 pls, err := GetPLS(v.Index(i).Interface()) | |
71 if err != nil { | |
72 return nil, err | |
73 } | |
74 ret[i] = pls | |
75 } | |
76 return ret, nil | |
77 } | |
OLD | NEW |