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

Side by Side Diff: globalinfo.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 | « filters/featureBreaker/tq.go ('k') | helper/datastore.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 gae
6
7 import (
8 "time"
9
10 "golang.org/x/net/context"
11 )
12
13 // GlobalInfo is the interface for all of the package methods which normally
14 // would be in the 'appengine' package.
15 type GlobalInfo interface {
16 AppID() string
17 Datacenter() string
18 DefaultVersionHostname() string
19 InstanceID() string
20 IsDevAppServer() bool
21 IsOverQuota(err error) bool
22 IsTimeoutError(err error) bool
23 ModuleHostname(module, version, instance string) (string, error)
24 ModuleName() string
25 RequestID() string
26 ServerSoftware() string
27 ServiceAccount() (string, error)
28 VersionID() string
29
30 Namespace(namespace string) (context.Context, error)
31
32 AccessToken(scopes ...string) (token string, expiry time.Time, err error )
33 PublicCertificates() ([]GICertificate, error)
34 SignBytes(bytes []byte) (keyName string, signature []byte, err error)
35 }
36
37 // GIFactory is the function signature for factory methods compatible with
38 // SetGIFactory.
39 type GIFactory func(context.Context) GlobalInfo
40
41 // GIFilter is the function signature for a filter GI implementation. It
42 // gets the current GI implementation, and returns a new GI implementation
43 // backed by the one passed in.
44 type GIFilter func(context.Context, GlobalInfo) GlobalInfo
45
46 // GetGIUnfiltered gets gets the GlobalInfo implementation from context without
47 // any of the filters applied.
48 func GetGIUnfiltered(c context.Context) GlobalInfo {
49 if f, ok := c.Value(globalInfoKey).(GIFactory); ok && f != nil {
50 return f(c)
51 }
52 return nil
53 }
54
55 // GetGI gets gets the GlobalInfo implementation from context.
56 func GetGI(c context.Context) GlobalInfo {
57 ret := GetGIUnfiltered(c)
58 if ret == nil {
59 return nil
60 }
61 for _, f := range getCurGIFilters(c) {
62 ret = f(c, ret)
63 }
64 return ret
65 }
66
67 // SetGIFactory sets the function to produce GlobalInfo instances, as returned
68 // by the GetGI method.
69 func SetGIFactory(c context.Context, gif GIFactory) context.Context {
70 return context.WithValue(c, globalInfoKey, gif)
71 }
72
73 // SetGI sets the current GlobalInfo object in the context. Useful for testing
74 // with a quick mock. This is just a shorthand SetGIFactory invocation to set
75 // a factory which always returns the same object.
76 func SetGI(c context.Context, gi GlobalInfo) context.Context {
77 return SetGIFactory(c, func(context.Context) GlobalInfo { return gi })
78 }
79
80 func getCurGIFilters(c context.Context) []GIFilter {
81 curFiltsI := c.Value(globalInfoFilterKey)
82 if curFiltsI != nil {
83 return curFiltsI.([]GIFilter)
84 }
85 return nil
86 }
87
88 // AddGIFilters adds GlobalInfo filters to the context.
89 func AddGIFilters(c context.Context, filts ...GIFilter) context.Context {
90 if len(filts) == 0 {
91 return c
92 }
93 cur := getCurGIFilters(c)
94 newFilts := make([]GIFilter, 0, len(cur)+len(filts))
95 newFilts = append(newFilts, getCurGIFilters(c)...)
96 newFilts = append(newFilts, filts...)
97 return context.WithValue(c, globalInfoFilterKey, newFilts)
98 }
OLDNEW
« no previous file with comments | « filters/featureBreaker/tq.go ('k') | helper/datastore.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698