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

Side by Side Diff: common/config/context.go

Issue 2575383002: Add server/cache support to gaeconfig. (Closed)
Patch Set: Un-collapse. Created 3 years, 11 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 | « appengine/gaemiddleware/context.go ('k') | common/config/filters/caching/config.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 LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package config
6
7 import (
8 "errors"
9 "net/url"
10
11 "golang.org/x/net/context"
12 )
13
14 var errNoImpl = errors.New("config: the context doesn't have Interface installed , use SetImplementation")
15
16 type contextKey int
17
18 // SetImplementation sets the current Interface implementation in the context.
19 //
20 // By keeping Interface in the context we allow high level libraries to use
21 // a config client implementation without explicitly passing it through all
22 // method calls.
23 //
24 // This implementation is also used by package level functions declared below
25 // (see GetConfig, etc).
26 func SetImplementation(c context.Context, ri Interface) context.Context {
27 return context.WithValue(c, contextKey(0), ri)
28 }
29
30 // GetImplementation returns the Interface object from context.
31 //
32 // It should be installed there by 'SetImplementation' already.
33 //
34 // Returns nil if it's not there.
35 func GetImplementation(c context.Context) Interface {
36 ret, _ := c.Value(contextKey(0)).(Interface)
37 return ret
38 }
39
40 ////////////////////////////////////////////////////////////////////////////////
41 // Functions below mimic Interface API.
42 //
43 // They forward calls to an Interface implementation in the context or panic if
44 // it isn't installed.
45
46 // ServiceURL returns the URL of the config service.
47 //
48 // The function just forwards the call to corresponding method of Interface
49 // implementation installed in the context with SetImplementation.
50 func ServiceURL(ctx context.Context) url.URL {
51 impl := GetImplementation(ctx)
52 if impl == nil {
53 panic(errNoImpl)
54 }
55 return impl.ServiceURL(ctx)
56 }
57
58 // GetConfig returns a config at a path in a config set or ErrNoConfig
59 // if missing. If hashOnly is true, returned Config struct has Content set
60 // to "" (and the call is faster).
61 //
62 // The function just forwards the call to corresponding method of Interface
63 // implementation installed in the context with SetImplementation.
64 func GetConfig(ctx context.Context, configSet, path string, hashOnly bool) (*Con fig, error) {
65 impl := GetImplementation(ctx)
66 if impl == nil {
67 panic(errNoImpl)
68 }
69 return impl.GetConfig(ctx, configSet, path, hashOnly)
70 }
71
72 // GetConfigByHash returns the contents of a config, as identified by its
73 // content hash, or ErrNoConfig if missing.
74 //
75 // The function just forwards the call to corresponding method of Interface
76 // implementation installed in the context with SetImplementation.
77 func GetConfigByHash(ctx context.Context, contentHash string) (string, error) {
78 impl := GetImplementation(ctx)
79 if impl == nil {
80 panic(errNoImpl)
81 }
82 return impl.GetConfigByHash(ctx, contentHash)
83 }
84
85 // GetConfigSetLocation returns the URL location of a config set.
86 //
87 // The function just forwards the call to corresponding method of Interface
88 // implementation installed in the context with SetImplementation.
89 func GetConfigSetLocation(ctx context.Context, configSet string) (*url.URL, erro r) {
90 impl := GetImplementation(ctx)
91 if impl == nil {
92 panic(errNoImpl)
93 }
94 return impl.GetConfigSetLocation(ctx, configSet)
95 }
96
97 // GetProjectConfigs returns all the configs at the given path in all
98 // projects that have such config. If hashesOnly is true, returned Config
99 // structs have Content set to "" (and the call is faster).
100 //
101 // The function just forwards the call to corresponding method of Interface
102 // implementation installed in the context with SetImplementation.
103 func GetProjectConfigs(ctx context.Context, path string, hashesOnly bool) ([]Con fig, error) {
104 impl := GetImplementation(ctx)
105 if impl == nil {
106 panic(errNoImpl)
107 }
108 return impl.GetProjectConfigs(ctx, path, hashesOnly)
109 }
110
111 // GetProjects returns all the registered projects in the configuration
112 // service.
113 //
114 // The function just forwards the call to corresponding method of Interface
115 // implementation installed in the context with SetImplementation.
116 func GetProjects(ctx context.Context) ([]Project, error) {
117 impl := GetImplementation(ctx)
118 if impl == nil {
119 panic(errNoImpl)
120 }
121 return impl.GetProjects(ctx)
122 }
123
124 // GetRefConfigs returns the config at the given path in all refs of all
125 // projects that have such config. If hashesOnly is true, returned Config
126 // structs have Content set to "" (and the call is faster).
127 //
128 // The function just forwards the call to corresponding method of Interface
129 // implementation installed in the context with SetImplementation.
130 func GetRefConfigs(ctx context.Context, path string, hashesOnly bool) ([]Config, error) {
131 impl := GetImplementation(ctx)
132 if impl == nil {
133 panic(errNoImpl)
134 }
135 return impl.GetRefConfigs(ctx, path, hashesOnly)
136 }
137
138 // GetRefs returns the list of refs for a project.
139 //
140 // The function just forwards the call to corresponding method of Interface
141 // implementation installed in the context with SetImplementation.
142 func GetRefs(ctx context.Context, projectID string) ([]string, error) {
143 impl := GetImplementation(ctx)
144 if impl == nil {
145 panic(errNoImpl)
146 }
147 return impl.GetRefs(ctx, projectID)
148 }
OLDNEW
« no previous file with comments | « appengine/gaemiddleware/context.go ('k') | common/config/filters/caching/config.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698