Documentation
¶
Overview ¶
Package constantvar provides a runtimevar implementation with Variables that never change. Use New, NewBytes, or NewError to construct a *runtimevar.Variable.
URLs ¶
For runtimevar.OpenVariable, constantvar registers for the scheme "constant". For more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.
As ¶
constantvar does not support any types for As.
Example (OpenVariableFromURL) ¶
package main
import (
"context"
"fmt"
"log"
"gocloud.dev/runtimevar"
)
func main() {
// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
ctx := context.Background()
v, err := runtimevar.OpenVariable(ctx, "constant://?val=hello+world&decoder=string")
if err != nil {
log.Fatal(err)
}
defer v.Close()
snapshot, err := v.Latest(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(snapshot.Value.(string))
// Output
// hello world
}
Output:
Index ¶
Examples ¶
Constants ¶
const Scheme = "constant"
Scheme is the URL scheme constantvar registers its URLOpener under on blob.DefaultMux.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(value interface{}) *runtimevar.Variable
New constructs a *runtimevar.Variable holding value.
Example ¶
package main
import (
"context"
"fmt"
"log"
"gocloud.dev/runtimevar/constantvar"
)
// MyConfig is a sample configuration struct.
type MyConfig struct {
Server string
Port int
}
func main() {
// cfg is our sample config.
cfg := MyConfig{Server: "foo.com", Port: 80}
// Construct a *runtimevar.Variable that always returns cfg.
v := constantvar.New(cfg)
defer v.Close()
// We can now read the current value of the variable from v.
snapshot, err := v.Latest(context.Background())
if err != nil {
log.Fatal(err)
}
cfg = snapshot.Value.(MyConfig)
fmt.Printf("%s running on port %d", cfg.Server, cfg.Port)
}
Output: foo.com running on port 80
func NewBytes ¶ added in v0.9.0
func NewBytes(b []byte, decoder *runtimevar.Decoder) *runtimevar.Variable
NewBytes uses decoder to decode b. If the decode succeeds, it constructs a *runtimevar.Variable holding the decoded value. If the decode fails, it constructs a runtimevar.Variable that always fails with the error.
Example ¶
package main
import (
"context"
"fmt"
"log"
"gocloud.dev/runtimevar"
"gocloud.dev/runtimevar/constantvar"
)
// MyConfig is a sample configuration struct.
type MyConfig struct {
Server string
Port int
}
func main() {
// Create a decoder for decoding JSON strings into MyConfig.
decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode)
// Construct a *runtimevar.Variable based on a JSON string.
v := constantvar.NewBytes([]byte(`{"Server": "foo.com", "Port": 80}`), decoder)
defer v.Close()
// We can now read the current value of the variable from v.
snapshot, err := v.Latest(context.Background())
if err != nil {
log.Fatal(err)
}
cfg := snapshot.Value.(MyConfig)
fmt.Printf("%s running on port %d", cfg.Server, cfg.Port)
}
Output: foo.com running on port 80
func NewError ¶
func NewError(err error) *runtimevar.Variable
NewError constructs a *runtimevar.Variable that always fails. Runtimevar wraps errors returned by provider implementations, so the error returned by runtimevar will not equal err.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"log"
"gocloud.dev/runtimevar/constantvar"
)
func main() {
// Construct a runtimevar.Variable that always returns errFake.
var errFake = errors.New("my error")
v := constantvar.NewError(errFake)
defer v.Close()
// We can now use Watch to read the current value of the variable
// from v. Note that Latest would block here since it waits for
// a "good" value, and v will never get one.
_, err := v.Watch(context.Background())
if err == nil {
log.Fatal("Expected an error!")
}
fmt.Println(err)
}
Output: runtimevar (code=Unknown): my error
Types ¶
type URLOpener ¶ added in v0.12.0
type URLOpener struct {
// Decoder specifies the decoder to use if one is not specified in the URL.
// Defaults to runtimevar.BytesDecoder.
Decoder *runtimevar.Decoder
}
URLOpener opens constantvar URLs like "constant://?val=foo&decoder=string".
The host and path are ignored.
The following URL parameters are supported:
- val: The value to use for the constant Variable. The bytes from val are passed to NewBytes.
- err: The error to use for the constant Variable. A new error is created using errors.New and passed to NewError.
- decoder: The decoder to use. Defaults to runtimevar.BytesDecoder. See runtimevar.DecoderByName for supported values.
If both "err" and "val" are provided, "val" is ignored.
func (*URLOpener) OpenVariableURL ¶ added in v0.12.0
OpenVariableURL opens the variable at the URL's path. See the package doc for more details.