Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: impl/dummy/dummy.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « impl/dummy/doc.go ('k') | impl/dummy/dummy_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package dummy
6
7 import (
8 "fmt"
9 "runtime"
10 "strings"
11 "time"
12
13 "github.com/luci/gae/service/info"
14 "github.com/luci/gae/service/memcache"
15 "github.com/luci/gae/service/rawdatastore"
16 "github.com/luci/gae/service/taskqueue"
17 "golang.org/x/net/context"
18 )
19
20 const niFmtStr = "dummy: method %s.%s is not implemented"
21
22 // ni returns an error whose message is an appropriate expansion of niFmtStr.
23 //
24 // It walks the stack to find out what interface and method it's being
25 // called from. For example, it might return a message which looks like:
26 // dummy: method RawDatastore.Get is not implemented
27 //
28 // This allows the various dummy objects below to have clear boilerplate which
29 // avoids copy+paste errors (such as if each one of them filled in the template
30 // manually).
31 //
32 // If this function is somehow called from something other than one of the dummy
33 // objects in this package, it will substitute the string UNKNOWN for the
34 // interface and/or the method in the niFmtStr template.
35 func ni() error {
36 iface := "UNKNOWN"
37 funcName := "UNKNOWN"
38
39 if ptr, _, _, ok := runtime.Caller(1); ok {
40 f := runtime.FuncForPC(ptr)
41 n := f.Name()
42 if n != "" {
43 parts := strings.Split(n, ".")
44 if len(parts) > 2 {
45 switch parts[len(parts)-2] {
46 case "rds":
47 iface = "RawDatastore"
48 case "mc":
49 iface = "Memcache"
50 case "tq":
51 iface = "TaskQueue"
52 case "i":
53 iface = "Info"
54 }
55 funcName = parts[len(parts)-1]
56 }
57 }
58 }
59
60 return fmt.Errorf(niFmtStr, iface, funcName)
61 }
62
63 /////////////////////////////////// rds ////////////////////////////////////
64
65 type rds struct{}
66
67 func (rds) NewKey(string, string, int64, rawdatastore.Key) rawdatastore.Key { panic(ni()) }
68 func (rds) DecodeKey(string) (rawdatastore.Key, error) { panic(ni()) }
69 func (rds) KeyFromTokens(a, n string, t []rawdatastore.KeyTok) (rawdatastore.Key , error) { panic(ni()) }
70 func (rds) Put(rawdatastore.Key, rawdatastore.PropertyLoadSaver) (rawdatastore.K ey, error) {
71 panic(ni())
72 }
73 func (rds) Get(rawdatastore.Key, rawdatastore.PropertyLoadSaver) error { panic(n i()) }
74 func (rds) Delete(rawdatastore.Key) error { panic(n i()) }
75 func (rds) PutMulti([]rawdatastore.Key, []rawdatastore.PropertyLoadSaver) ([]raw datastore.Key, error) {
76 panic(ni())
77 }
78 func (rds) GetMulti([]rawdatastore.Key, []rawdatastore.PropertyLoadSaver) error { panic(ni()) }
79 func (rds) DeleteMulti([]rawdatastore.Key) error { panic(ni()) }
80 func (rds) NewQuery(string) rawdatastore.Query { panic(ni()) }
81 func (rds) Run(rawdatastore.Query) rawdatastore.Iterator { panic(ni()) }
82 func (rds) GetAll(rawdatastore.Query, *[]rawdatastore.PropertyMap) ([]rawdatasto re.Key, error) {
83 panic(ni())
84 }
85 func (rds) Count(rawdatastore.Query) (int, error) { panic(ni()) }
86 func (rds) RunInTransaction(func(context.Context) error, *rawdatastore.Transacti onOptions) error {
87 panic(ni())
88 }
89
90 var dummyRDSInst = rds{}
91
92 // RawDatastore returns a dummy rawdatastore.Interface implementation suitable
93 // for embedding. Every method panics with a message containing the name of the
94 // method which was unimplemented.
95 func RawDatastore() rawdatastore.Interface { return dummyRDSInst }
96
97 /////////////////////////////////// mc ////////////////////////////////////
98
99 type mc struct{}
100
101 func (mc) Add(memcache.Item) error { panic(ni()) }
102 func (mc) NewItem(key string) memcache.Item { panic(ni()) }
103 func (mc) Set(memcache.Item) error { panic(ni()) }
104 func (mc) Get(string) (memcache.Item, error) { panic(ni()) }
105 func (mc) Delete(string) error { panic(ni()) }
106 func (mc) CompareAndSwap(memcache.Item) error { panic(ni()) }
107 func (mc) AddMulti([]memcache.Item) error { panic(ni()) }
108 func (mc) SetMulti([]memcache.Item) error { panic(ni()) }
109 func (mc) GetMulti([]string) (map[string]memcache.Item, error) { panic(ni()) }
110 func (mc) DeleteMulti([]string) error { panic(ni()) }
111 func (mc) CompareAndSwapMulti([]memcache.Item) error { panic(ni()) }
112 func (mc) Increment(string, int64, uint64) (uint64, error) { panic(ni()) }
113 func (mc) IncrementExisting(string, int64) (uint64, error) { panic(ni()) }
114 func (mc) Flush() error { panic(ni()) }
115 func (mc) Stats() (*memcache.Statistics, error) { panic(ni()) }
116
117 var dummyMCInst = mc{}
118
119 // Memcache returns a dummy memcache.Interface implementation suitable for
120 // embedding. Every method panics with a message containing the name of the
121 // method which was unimplemented.
122 func Memcache() memcache.Interface { return dummyMCInst }
123
124 /////////////////////////////////// tq ////////////////////////////////////
125
126 type tq struct{}
127
128 func (tq) Add(*taskqueue.Task, string) (*taskqueue.Task, error) { pani c(ni()) }
129 func (tq) Delete(*taskqueue.Task, string) error { pani c(ni()) }
130 func (tq) AddMulti([]*taskqueue.Task, string) ([]*taskqueue.Task, error) { pani c(ni()) }
131 func (tq) DeleteMulti([]*taskqueue.Task, string) error { pani c(ni()) }
132 func (tq) Lease(int, string, int) ([]*taskqueue.Task, error) { pani c(ni()) }
133 func (tq) LeaseByTag(int, string, int, string) ([]*taskqueue.Task, error) { pani c(ni()) }
134 func (tq) ModifyLease(*taskqueue.Task, string, int) error { pani c(ni()) }
135 func (tq) Purge(string) error { pani c(ni()) }
136 func (tq) QueueStats([]string) ([]taskqueue.Statistics, error) { pani c(ni()) }
137
138 var dummyTQInst = tq{}
139
140 // TaskQueue returns a dummy taskqueue.Interface implementation suitable for
141 // embedding. Every method panics with a message containing the name of the
142 // method which was unimplemented.
143 func TaskQueue() taskqueue.Interface { return dummyTQInst }
144
145 /////////////////////////////////// i ////////////////////////////////////
146
147 type i struct{}
148
149 func (i) AccessToken(scopes ...string) (token string, expiry time.Time, err erro r) { panic(ni()) }
150 func (i) AppID() string { panic(ni()) }
151 func (i) ModuleHostname(module, version, instance string) (string, error) { panic(ni()) }
152 func (i) ModuleName() string { panic(ni()) }
153 func (i) DefaultVersionHostname() string { panic(ni()) }
154 func (i) PublicCertificates() ([]info.Certificate, error) { panic(ni()) }
155 func (i) RequestID() string { panic(ni()) }
156 func (i) ServiceAccount() (string, error) { panic(ni()) }
157 func (i) SignBytes(bytes []byte) (keyName string, signature []byte, err error) { panic(ni()) }
158 func (i) VersionID() string { panic(ni()) }
159 func (i) Namespace(namespace string) (context.Context, error) { panic(ni()) }
160 func (i) Datacenter() string { panic(ni()) }
161 func (i) InstanceID() string { panic(ni()) }
162 func (i) IsDevAppServer() bool { panic(ni()) }
163 func (i) ServerSoftware() string { panic(ni()) }
164 func (i) IsCapabilityDisabled(err error) bool { panic(ni()) }
165 func (i) IsOverQuota(err error) bool { panic(ni()) }
166 func (i) IsTimeoutError(err error) bool { panic(ni()) }
167
168 var dummyInfoInst = i{}
169
170 // Info returns a dummy info.Interface implementation suitable for embedding.
171 // Every method panics with a message containing the name of the method which
172 // was unimplemented.
173 func Info() info.Interface { return dummyInfoInst }
OLDNEW
« no previous file with comments | « impl/dummy/doc.go ('k') | impl/dummy/dummy_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698