| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "regexp" | 9 "regexp" |
| 10 | 10 |
| 11 "github.com/luci/gae/impl/dummy" | 11 "github.com/luci/gae/impl/dummy" |
| 12 "github.com/luci/gae/service/info" | 12 "github.com/luci/gae/service/info" |
| 13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 type giContextKeyType int | 16 type giContextKeyType int |
| 17 | 17 |
| 18 var giContextKey giContextKeyType | 18 var giContextKey giContextKeyType |
| 19 | 19 |
| 20 // validNamespace matches valid namespace names. | 20 // validNamespace matches valid namespace names. |
| 21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) | 21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) |
| 22 | 22 |
| 23 var defaultGlobalInfoData = globalInfoData{ |
| 24 // versionID returns X.Y where Y is autogenerated by appengine, and X is |
| 25 // whatever's in app.yaml. |
| 26 versionID: "testVersionID.1", |
| 27 requestID: "test-request-id", |
| 28 } |
| 29 |
| 30 type globalInfoData struct { |
| 31 appid string |
| 32 namespace string |
| 33 versionID string |
| 34 requestID string |
| 35 } |
| 36 |
| 23 func curGID(c context.Context) *globalInfoData { | 37 func curGID(c context.Context) *globalInfoData { |
| 24 » return c.Value(giContextKey).(*globalInfoData) | 38 » if gid, ok := c.Value(giContextKey).(*globalInfoData); ok { |
| 39 » » return gid |
| 40 » } |
| 41 » return &defaultGlobalInfoData |
| 42 } |
| 43 |
| 44 func useGID(c context.Context, f func(mod *globalInfoData)) context.Context { |
| 45 » cur := curGID(c) |
| 46 » if cur == nil { |
| 47 » » cur = &defaultGlobalInfoData |
| 48 » } |
| 49 |
| 50 » clone := *cur |
| 51 » f(&clone) |
| 52 » return context.WithValue(c, giContextKey, &clone) |
| 25 } | 53 } |
| 26 | 54 |
| 27 // useGI adds a gae.GlobalInfo context, accessible | 55 // useGI adds a gae.GlobalInfo context, accessible |
| 28 // by gae.GetGI(c) | 56 // by gae.GetGI(c) |
| 29 func useGI(c context.Context, appID string) context.Context { | 57 func useGI(c context.Context) context.Context { |
| 30 return info.SetFactory(c, func(ic context.Context) info.Interface { | 58 return info.SetFactory(c, func(ic context.Context) info.Interface { |
| 31 return &giImpl{dummy.Info(), curGID(ic), ic} | 59 return &giImpl{dummy.Info(), curGID(ic), ic} |
| 32 }) | 60 }) |
| 33 } | 61 } |
| 34 | 62 |
| 35 type globalInfoData struct { | |
| 36 appid string | |
| 37 namespace string | |
| 38 } | |
| 39 | |
| 40 type giImpl struct { | 63 type giImpl struct { |
| 41 info.Interface | 64 info.Interface |
| 42 *globalInfoData | 65 *globalInfoData |
| 43 c context.Context | 66 c context.Context |
| 44 } | 67 } |
| 45 | 68 |
| 46 var _ = info.Interface((*giImpl)(nil)) | 69 var _ = info.Interface((*giImpl)(nil)) |
| 47 | 70 |
| 48 func (gi *giImpl) GetNamespace() string { | 71 func (gi *giImpl) GetNamespace() string { |
| 49 return gi.namespace | 72 return gi.namespace |
| 50 } | 73 } |
| 51 | 74 |
| 52 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { | 75 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { |
| 53 if !validNamespace.MatchString(ns) { | 76 if !validNamespace.MatchString(ns) { |
| 54 return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) | 77 return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) |
| 55 } | 78 } |
| 56 » return context.WithValue(gi.c, giContextKey, &globalInfoData{gi.appid, n
s}), nil | 79 |
| 80 » return useGID(gi.c, func(mod *globalInfoData) { |
| 81 » » mod.namespace = ns |
| 82 » }), nil |
| 57 } | 83 } |
| 58 | 84 |
| 59 func (gi *giImpl) MustNamespace(ns string) context.Context { | 85 func (gi *giImpl) MustNamespace(ns string) context.Context { |
| 60 ret, err := gi.Namespace(ns) | 86 ret, err := gi.Namespace(ns) |
| 61 if err != nil { | 87 if err != nil { |
| 62 panic(err) | 88 panic(err) |
| 63 } | 89 } |
| 64 return ret | 90 return ret |
| 65 } | 91 } |
| 66 | 92 |
| 67 func (gi *giImpl) AppID() string { | 93 func (gi *giImpl) AppID() string { |
| 68 return gi.appid | 94 return gi.appid |
| 69 } | 95 } |
| 70 | 96 |
| 71 func (gi *giImpl) FullyQualifiedAppID() string { | 97 func (gi *giImpl) FullyQualifiedAppID() string { |
| 72 return gi.appid | 98 return gi.appid |
| 73 } | 99 } |
| 74 | 100 |
| 75 func (gi *giImpl) IsDevAppServer() bool { | 101 func (gi *giImpl) IsDevAppServer() bool { |
| 76 return true | 102 return true |
| 77 } | 103 } |
| 78 | 104 |
| 79 func (gi *giImpl) VersionID() string { | 105 func (gi *giImpl) VersionID() string { |
| 80 » // VersionID returns X.Y where Y is autogenerated by appengine, and X is | 106 » return curGID(gi.c).versionID |
| 81 » // whatever's in app.yaml. | |
| 82 » return "testVersionID.1" | |
| 83 } | 107 } |
| 108 |
| 109 func (gi *giImpl) RequestID() string { |
| 110 return curGID(gi.c).requestID |
| 111 } |
| 112 |
| 113 func (gi *giImpl) Testable() info.Testable { |
| 114 return gi |
| 115 } |
| 116 |
| 117 func (gi *giImpl) SetVersionID(v string) context.Context { |
| 118 return useGID(gi.c, func(mod *globalInfoData) { |
| 119 mod.versionID = v |
| 120 }) |
| 121 } |
| 122 |
| 123 func (gi *giImpl) SetRequestID(v string) context.Context { |
| 124 return useGID(gi.c, func(mod *globalInfoData) { |
| 125 mod.requestID = v |
| 126 }) |
| 127 } |
| OLD | NEW |