/
appenginevm.go
159 lines (127 loc) · 3.25 KB
/
appenginevm.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// +build appenginevm
package pacific
import (
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
"io"
"net/http"
)
type MockConnection struct{}
func (conn MockConnection) Close() {
return
}
type Context struct {
context.Context
Connection MockConnection
}
func (context Context) Infof(format string, args ...interface{}) {
log.Infof(context, format, args...)
}
func (context Context) Errorf(format string, args ...interface{}) {
log.Errorf(context, format, args...)
}
func (context Context) DoRequest(method string, url string, obj io.Reader) (resp *http.Response, err error) {
file_request, _ := http.NewRequest(method, url, obj)
client := context.Client()
return client.Do(file_request)
}
func (context Context) Client() *http.Client {
return urlfetch.Client(context)
}
func NewContext(r ...*http.Request) Context {
context := appengine.BackgroundContext()
return Context{context, MockConnection{}}
}
func Main() {
appengine.Main()
}
func IsDevAppServer() bool {
return appengine.IsDevAppServer()
}
func SupportsWS() bool {
return true
}
type Ancestor struct {
Context Context
Kind string
KeyString string
KeyInt int64
Parent *datastore.Key
PrimaryKey string
}
func (ancestor Ancestor) key() *datastore.Key {
return datastore.NewKey(ancestor.Context, ancestor.Kind, ancestor.KeyString, ancestor.KeyInt, ancestor.Parent)
}
type Query struct {
Kind string
Context Context
Offset int
Limit int
KeyString string
KeyInt int64
Ancestors []Ancestor
Order string
Filters map[string]interface{}
PrimaryKey string
}
func (query Query) Delete() error {
key := query.key()
return query.deleteByKey(key)
}
func (query Query) Get(result interface{}) error {
key := query.key()
return datastore.Get(query.Context, key, result)
}
func (query Query) GetAll(results interface{}) error {
_, err := query.createQuery().GetAll(query.Context, results)
return err
}
func (query Query) Put(entry interface{}) error {
key := query.key()
_, err := datastore.Put(query.Context, key, entry)
return err
}
func (query Query) key() *datastore.Key {
ancestor_key := query.ancestorKey()
return datastore.NewKey(query.Context, query.Kind, query.KeyString, query.KeyInt, ancestor_key)
}
func (query Query) ancestorKey() (parent *datastore.Key) {
if len(query.Ancestors) > 0 {
for _, ancestor := range query.Ancestors {
ancestor.Context = query.Context
ancestor.Parent = parent
parent = ancestor.key()
}
return parent
}
return nil
}
func (query Query) createQuery() (q *datastore.Query) {
q = datastore.NewQuery(query.Kind)
if len(query.Ancestors) > 0 {
ancestor_key := query.ancestorKey()
q = q.Ancestor(ancestor_key)
}
for filter_by, value := range query.Filters {
q = q.Filter(filter_by, value)
}
if query.Limit != 0 {
q = q.Limit(query.Limit)
}
if query.Offset != 0 {
q = q.Offset(query.Offset)
}
if query.Order != "" {
q = q.Order(query.Order)
}
return q
}
func (query Query) deleteByKey(key *datastore.Key) error {
return datastore.Delete(query.Context, key)
}
func AutoMigrate(context Context, kind string, primary_key string, dst interface{}){
//no-op
}