-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastorefixpls.go
67 lines (54 loc) · 1.38 KB
/
datastorefixpls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package datastorefixpls
import (
"strings"
"time"
"google.golang.org/appengine/datastore"
)
// SaveStruct serializes every time.Time as both with and without dot suffix
func SaveStruct(x interface{}) ([]datastore.Property, error) {
ps, err := datastore.SaveStruct(x)
if err != nil {
return ps, err
}
return denormalize(ps), nil
}
// LoadStruct deserializes every field ending with a dot as both with and without the dot suffix
func LoadStruct(x interface{}, ps []datastore.Property) error {
return datastore.LoadStruct(x, denormalize(ps))
}
func exist(ps []datastore.Property, needle datastore.Property) bool {
for _, p := range ps {
if p.Name == needle.Name {
return true
}
}
return false
}
func denormalizeName(p datastore.Property) (datastore.Property, bool) {
if _, isTime := p.Value.(time.Time); isTime {
if strings.HasSuffix(p.Name, ".Time") {
p.Name = p.Name[:len(p.Name)-len("Time")]
return p, true
}
if strings.HasSuffix(p.Name, ".") {
p.Name = p.Name + "Time"
return p, true
}
}
return datastore.Property{}, false
}
func denormalize(ps []datastore.Property) []datastore.Property {
extra := []datastore.Property{}
for _, p := range ps {
if prop, ok := denormalizeName(p); ok {
extra = append(extra, prop)
}
}
// make sure we dont add duplicates
for _, p := range extra {
if !exist(ps, p) {
ps = append(ps, p)
}
}
return ps
}