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 rawdatastore | 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 o into a PropertyLoadSaver. o must be a pointer to a |
12 // struct of some sort. | 12 // struct of some sort. |
13 func GetPLS(o interface{}) PropertyLoadSaver { | 13 func GetPLS(o interface{}) PropertyLoadSaver { |
14 v := reflect.ValueOf(o) | 14 v := reflect.ValueOf(o) |
15 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { | 15 if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { |
16 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType}
} | 16 return &structPLS{c: &structCodec{problem: ErrInvalidEntityType}
} |
17 } | 17 } |
18 v = v.Elem() | 18 v = v.Elem() |
19 » t := v.Type() | 19 » c := getCodec(v.Type()) |
| 20 » return &structPLS{v, c} |
| 21 } |
20 | 22 |
| 23 func getCodec(structType reflect.Type) *structCodec { |
21 structCodecsMutex.RLock() | 24 structCodecsMutex.RLock() |
22 » if c, ok := structCodecs[t]; ok { | 25 » c, ok := structCodecs[structType] |
23 » » structCodecsMutex.RUnlock() | 26 » structCodecsMutex.RUnlock() |
24 » » return &structPLS{v, c} | 27 » if ok { |
| 28 » » return c |
25 } | 29 } |
26 structCodecsMutex.RUnlock() | |
27 | 30 |
28 structCodecsMutex.Lock() | 31 structCodecsMutex.Lock() |
29 defer structCodecsMutex.Unlock() | 32 defer structCodecsMutex.Unlock() |
30 » return &structPLS{v, getStructCodecLocked(t)} | 33 » return getStructCodecLocked(structType) |
31 } | 34 } |
OLD | NEW |