| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package backend | 5 package backend |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/url" | 8 "net/url" |
| 9 | 9 |
| 10 "github.com/luci/luci-go/common/config" |
| 11 |
| 10 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
| 11 ) | 13 ) |
| 12 | 14 |
| 13 // FormatSpec is a specification for formatted data. | 15 // FormatSpec is a specification for formatted data. |
| 14 type FormatSpec struct { | 16 type FormatSpec struct { |
| 15 // Formatter is the supported destination Resolver format for this item. | 17 // Formatter is the supported destination Resolver format for this item. |
| 16 // Backends (notably the FormatterBackend) may project the Item into thi
s | 18 // Backends (notably the FormatterBackend) may project the Item into thi
s |
| 17 // format. | 19 // format. |
| 18 // | 20 // |
| 19 // An empty string means the original config service format. | 21 // An empty string means the original config service format. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 ServiceURL(context.Context) url.URL | 58 ServiceURL(context.Context) url.URL |
| 57 | 59 |
| 58 // Get retrieves a single configuration. | 60 // Get retrieves a single configuration. |
| 59 Get(c context.Context, configSet, path string, p Params) (*Item, error) | 61 Get(c context.Context, configSet, path string, p Params) (*Item, error) |
| 60 | 62 |
| 61 // GetAll retrieves all configurations of a given type. | 63 // GetAll retrieves all configurations of a given type. |
| 62 GetAll(c context.Context, t GetAllTarget, path string, p Params) ([]*Ite
m, error) | 64 GetAll(c context.Context, t GetAllTarget, path string, p Params) ([]*Ite
m, error) |
| 63 | 65 |
| 64 // ConfigSetURL returns the URL for the specified config set. | 66 // ConfigSetURL returns the URL for the specified config set. |
| 65 ConfigSetURL(c context.Context, configSet string, p Params) (url.URL, er
ror) | 67 ConfigSetURL(c context.Context, configSet string, p Params) (url.URL, er
ror) |
| 68 |
| 69 // GetConfigInterface returns the raw configuration interface of the bac
kend. |
| 70 GetConfigInterface(c context.Context, a Authority) config.Interface |
| 66 } | 71 } |
| 67 | 72 |
| 68 // Factory is a function that generates a B given a Context. | 73 // Factory is a function that generates a B given a Context. |
| 69 type Factory func(context.Context) B | 74 type Factory func(context.Context) B |
| 70 | 75 |
| 71 // configBackendKey is the Context key for the configuration backend. | 76 // configBackendKey is the Context key for the configuration backend. |
| 72 var configBackendKey = "github.com/luci/luci-go/server/config:backend" | 77 var configBackendKey = "github.com/luci/luci-go/server/config:backend" |
| 73 | 78 |
| 74 // WithBackend returns a derivative Context with the supplied Backend installed. | 79 // WithBackend returns a derivative Context with the supplied Backend installed. |
| 75 func WithBackend(c context.Context, b B) context.Context { | 80 func WithBackend(c context.Context, b B) context.Context { |
| 76 return WithFactory(c, func(context.Context) B { return b }) | 81 return WithFactory(c, func(context.Context) B { return b }) |
| 77 } | 82 } |
| 78 | 83 |
| 79 // WithFactory returns a derivative Context with the supplied BackendFactory | 84 // WithFactory returns a derivative Context with the supplied BackendFactory |
| 80 // installed. | 85 // installed. |
| 81 func WithFactory(c context.Context, f Factory) context.Context { | 86 func WithFactory(c context.Context, f Factory) context.Context { |
| 82 return context.WithValue(c, &configBackendKey, f) | 87 return context.WithValue(c, &configBackendKey, f) |
| 83 } | 88 } |
| 84 | 89 |
| 85 // Get returns the Backend that is installed into the Context. | 90 // Get returns the Backend that is installed into the Context. |
| 86 // | 91 // |
| 87 // If no Backend is installed in the Context, Get will panic. | 92 // If no Backend is installed in the Context, Get will panic. |
| 88 func Get(c context.Context) B { | 93 func Get(c context.Context) B { |
| 89 if f, ok := c.Value(&configBackendKey).(Factory); ok { | 94 if f, ok := c.Value(&configBackendKey).(Factory); ok { |
| 90 return f(c) | 95 return f(c) |
| 91 } | 96 } |
| 92 panic("no Backend factory is installed in the Context") | 97 panic("no Backend factory is installed in the Context") |
| 93 } | 98 } |
| OLD | NEW |