OLD | NEW |
| (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 memory | |
6 | |
7 import ( | |
8 "fmt" | |
9 "regexp" | |
10 | |
11 "github.com/luci/gae/impl/dummy" | |
12 "github.com/luci/gae/service/info" | |
13 "golang.org/x/net/context" | |
14 ) | |
15 | |
16 type giContextKeyType int | |
17 | |
18 var giContextKey giContextKeyType | |
19 | |
20 // validNamespace matches valid namespace names. | |
21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) | |
22 | |
23 func curGID(c context.Context) *globalInfoData { | |
24 return c.Value(giContextKey).(*globalInfoData) | |
25 } | |
26 | |
27 // useGI adds a gae.GlobalInfo context, accessible | |
28 // by gae.GetGI(c) | |
29 func useGI(c context.Context) context.Context { | |
30 return info.SetFactory(c, func(ic context.Context) info.Interface { | |
31 return &giImpl{dummy.Info(), curGID(ic), ic} | |
32 }) | |
33 } | |
34 | |
35 // globalAppID is the 'AppID' of everythin returned from this memory | |
36 // implementation (DSKeys, GlobalInfo, etc.). There's no way to modify this | |
37 // value through the API, and there are a couple bits of code where it's hard to | |
38 // route this value through to without making the internal APIs really complex. | |
39 const globalAppID = "dev~app" | |
40 | |
41 type globalInfoData struct { | |
42 namespace string | |
43 } | |
44 | |
45 type giImpl struct { | |
46 info.Interface | |
47 *globalInfoData | |
48 c context.Context | |
49 } | |
50 | |
51 var _ = info.Interface((*giImpl)(nil)) | |
52 | |
53 func (gi *giImpl) GetNamespace() string { | |
54 return gi.namespace | |
55 } | |
56 | |
57 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { | |
58 if !validNamespace.MatchString(ns) { | |
59 return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) | |
60 } | |
61 return context.WithValue(gi.c, giContextKey, &globalInfoData{ns}), nil | |
62 } | |
63 | |
64 func (gi *giImpl) AppID() string { | |
65 return globalAppID | |
66 } | |
OLD | NEW |