This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
/
entity.go
112 lines (97 loc) · 2.61 KB
/
entity.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2014 Ronoaldo JLP <ronoaldo@gmail.com>
// Licensed under the Apache License, Version 2.0
package aetools
import (
"google.golang.org/appengine/datastore"
)
// Entity is a small wrapper around datastore.PropertyList
// to also hold a *datastore.Key.
type Entity struct {
Key *datastore.Key
Properties datastore.PropertyList
}
// Load decodes all properties into Entity.Propertioes object,
// implementing the datastore.PropertyLoadSaver interface.
func (e *Entity) Load(p []datastore.Property) error {
return e.Properties.Load(p)
}
// Save encodes all properties from Entity.Properties object,
// implmenting the datastore.PropetyLoadSaver interface.
func (e *Entity) Save() ([]datastore.Property, error) {
return e.Properties.Save()
}
// Add append p to the Properties attribute.
func (e *Entity) Add(p datastore.Property) {
e.Properties = append(e.Properties, p)
}
// Get returns the property value of the given name.
// If no property with that name exists, returns nil. It also
// returns nil if the property Value attribute is nil.
func (e *Entity) Get(name string) interface{} {
for _, p := range e.Properties {
if p.Name == name {
return p.Value
}
}
return nil
}
// GetInt returns the int value of the named property,
// and returns the zero value (0) if the property is not found,
// if its value is nil or if its type is not int, int32 or int64.
func (e *Entity) GetInt(name string) int64 {
v := e.Get(name)
if v == nil {
return 0
}
switch v.(type) {
case int, int32, int64:
return v.(int64)
default:
return 0
}
}
// GetFloat returns the string value of the named property,
// and returns the zero value (0.0) if the property is not found,
// if its value is nil or if its type is not float32 or float64.
func (e *Entity) GetFloat(name string) float64 {
v := e.Get(name)
if v == nil {
return 0.0
}
switch v.(type) {
case float32, float64:
return v.(float64)
default:
return 0.0
}
}
// GetString returns the string value of the named property,
// and returns the zero value ("") if the property is not found,
// if its value is nil or if its type is not string.
func (e *Entity) GetString(name string) string {
v := e.Get(name)
if v == nil {
return ""
}
switch v.(type) {
case string:
return v.(string)
default:
return ""
}
}
// GetBool returns the string value of the named property,
// and returns the zero value (false) if the property is not found,
// if its value is nil, or if its type is not bool.
func (e *Entity) GetBool(name string) bool {
v := e.Get(name)
if v == nil {
return false
}
switch v.(type) {
case bool:
return v.(bool)
default:
return false
}
}