| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package constantvar provides a runtimevar implementation with Variables |
| 16 |
// that never change. Use New, NewBytes, NewFromEnv, or NewError to construct a |
| 17 |
// *runtimevar.Variable. |
| 18 |
// |
| 19 |
// # URLs |
| 20 |
// |
| 21 |
// For runtimevar.OpenVariable, constantvar registers for the scheme "constant". |
| 22 |
// For more details on the URL format, see URLOpener. |
| 23 |
// See https://gocloud.dev/concepts/urls/ for background information. |
| 24 |
// |
| 25 |
// # As |
| 26 |
// |
| 27 |
// constantvar does not support any types for As. |
| 28 |
package constantvar // import "gocloud.dev/runtimevar/constantvar" |
| 29 |
|
| 30 |
import ( |
| 31 |
"context" |
| 32 |
"errors" |
| 33 |
"fmt" |
| 34 |
"net/url" |
| 35 |
"os" |
| 36 |
"time" |
| 37 |
|
| 38 |
"gocloud.dev/gcerrors" |
| 39 |
"gocloud.dev/runtimevar" |
| 40 |
"gocloud.dev/runtimevar/driver" |
| 41 |
) |
| 42 |
|
| 43 |
func init() { |
| 44 |
runtimevar.DefaultURLMux().RegisterVariable(Scheme, &URLOpener{}) |
| 45 |
} |
| 46 |
|
| 47 |
// Scheme is the URL scheme constantvar registers its URLOpener under on blob.DefaultMux. |
| 48 |
const Scheme = "constant" |
| 49 |
|
| 50 |
// URLOpener opens constantvar URLs like "constant://?val=foo&decoder=string". |
| 51 |
// |
| 52 |
// The host and path are ignored. |
| 53 |
// |
| 54 |
// The following URL parameters are supported: |
| 55 |
// - val: The value to use for the constant Variable. The bytes from val |
| 56 |
// are passed to NewBytes. |
| 57 |
// - envvar: The name of an environment variable to read the value from. |
| 58 |
// - err: The error to use for the constant Variable. A new error is created |
| 59 |
// using errors.New and passed to NewError. |
| 60 |
// - decoder: The decoder to use. Defaults to runtimevar.BytesDecoder. |
| 61 |
// See runtimevar.DecoderByName for supported values. |
| 62 |
// |
| 63 |
// If multiple of "val", "envvar", or "err" are provided, "err" wins, then "envvar", |
| 64 |
// then "val". |
| 65 |
type URLOpener struct { |
| 66 |
// Decoder specifies the decoder to use if one is not specified in the URL. |
| 67 |
// Defaults to runtimevar.BytesDecoder. |
| 68 |
Decoder *runtimevar.Decoder |
| 69 |
} |
| 70 |
|
| 71 |
// OpenVariableURL opens the variable at the URL's path. See the package doc |
| 72 |
// for more details. |
| 73 |
func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error) { |
| 74 |
q := u.Query() |
| 75 |
|
| 76 |
val := q.Get("val") |
| 77 |
q.Del("val") |
| 78 |
|
| 79 |
envvar := q.Get("envvar") |
| 80 |
q.Del("envvar") |
| 81 |
|
| 82 |
errVal := q.Get("err") |
| 83 |
q.Del("err") |
| 84 |
|
| 85 |
decoderName := q.Get("decoder") |
| 86 |
q.Del("decoder") |
| 87 |
decoder, err := runtimevar.DecoderByName(ctx, decoderName, o.Decoder) |
| 88 |
if err != nil { |
| 89 |
return nil, fmt.Errorf("open variable %v: invalid decoder: %v", u, err) |
| 90 |
} |
| 91 |
|
| 92 |
for param := range q { |
| 93 |
return nil, fmt.Errorf("open variable %v: invalid query parameter %q", u, param) |
| 94 |
} |
| 95 |
if errVal != "" { |
| 96 |
return NewError(errors.New(errVal)), nil |
| 97 |
} |
| 98 |
if envvar != "" { |
| 99 |
return NewFromEnv(envvar, decoder), nil |
| 100 |
} |
| 101 |
return NewBytes([]byte(val), decoder), nil |
| 102 |
} |
| 103 |
|
| 104 |
var errNotExist = errors.New("variable does not exist") |
| 105 |
|
| 106 |
// New constructs a *runtimevar.Variable holding value. |
| 107 |
func New(value any) *runtimevar.Variable { |
| 108 |
return runtimevar.New(&watcher{value: value, t: time.Now()}) |
| 109 |
} |
| 110 |
|
| 111 |
// NewBytes uses decoder to decode b. If the decode succeeds, it constructs |
| 112 |
// a *runtimevar.Variable holding the decoded value. If the decode fails, it |
| 113 |
// constructs a runtimevar.Variable that always fails with the error. |
| 114 |
func NewBytes(b []byte, decoder *runtimevar.Decoder) *runtimevar.Variable { |
| 115 |
value, err := decoder.Decode(context.Background(), b) |
| 116 |
if err != nil { |
| 117 |
return NewError(err) |
| 118 |
} |
| 119 |
return New(value) |
| 120 |
} |
| 121 |
|
| 122 |
// NewFromEnv reads an environment variable and uses decoder to decode it. |
| 123 |
// If the decode succeeds, it constructs a *runtimevar.Variable holding the |
| 124 |
// decoded value. If the decode fails, it constructs a runtimevar.Variable |
| 125 |
// that always fails with the error. |
| 126 |
// Note that the value of the constantvar is frozen at initialization time; |
| 127 |
// it does not get a new value if the underlying environment variable value |
| 128 |
// changes. |
| 129 |
func NewFromEnv(envVarName string, decoder *runtimevar.Decoder) *runtimevar.Variable { |
| 130 |
val := os.Getenv(envVarName) |
| 131 |
value, err := decoder.Decode(context.Background(), []byte(val)) |
| 132 |
if err != nil { |
| 133 |
return NewError(err) |
| 134 |
} |
| 135 |
return New(value) |
| 136 |
} |
| 137 |
|
| 138 |
// NewError constructs a *runtimevar.Variable that always fails. Runtimevar |
| 139 |
// wraps errors returned by drivers, so the error returned |
| 140 |
// by runtimevar will not equal err. |
| 141 |
func NewError(err error) *runtimevar.Variable { |
| 142 |
return runtimevar.New(&watcher{err: err}) |
| 143 |
} |
| 144 |
|
| 145 |
// watcher implements driver.Watcher and driver.State. |
| 146 |
type watcher struct { |
| 147 |
value any |
| 148 |
err error |
| 149 |
t time.Time |
| 150 |
} |
| 151 |
|
| 152 |
// Value implements driver.State.Value. |
| 153 |
func (w *watcher) Value() (any, error) { |
| 154 |
return w.value, w.err |
| 155 |
} |
| 156 |
|
| 157 |
// UpdateTime implements driver.State.UpdateTime. |
| 158 |
func (w *watcher) UpdateTime() time.Time { |
| 159 |
return w.t |
| 160 |
} |
| 161 |
|
| 162 |
// As implements driver.State.As. |
| 163 |
func (w *watcher) As(i any) bool { |
| 164 |
return false |
| 165 |
} |
| 166 |
|
| 167 |
// WatchVariable implements driver.WatchVariable. |
| 168 |
func (w *watcher) WatchVariable(ctx context.Context, prev driver.State) (driver.State, time.Duration) { |
| 169 |
// The first time this is called, return the constant value. |
| 170 |
if prev == nil { |
| 171 |
return w, 0 |
| 172 |
} |
| 173 |
// On subsequent calls, block forever as the value will never change. |
| 174 |
<-ctx.Done() |
| 175 |
w.err = ctx.Err() |
| 176 |
return w, 0 |
| 177 |
} |
| 178 |
|
| 179 |
// Close implements driver.Close. |
| 180 |
func (*watcher) Close() error { return nil } |
| 181 |
|
| 182 |
// ErrorAs implements driver.ErrorAs. |
| 183 |
func (*watcher) ErrorAs(err error, i any) bool { return false } |
| 184 |
|
| 185 |
// ErrorCode implements driver.ErrorCode |
| 186 |
func (*watcher) ErrorCode(err error) gcerrors.ErrorCode { |
| 187 |
if err == errNotExist { |
| 188 |
return gcerrors.NotFound |
| 189 |
} |
| 190 |
return gcerrors.Unknown |
| 191 |
} |