gocloud.dev: gocloud.dev/runtimevar/constantvar Index | Examples | Files

package constantvar

import "gocloud.dev/runtimevar/constantvar"

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.

Code:

// 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

Index

Examples

Package Files

constantvar.go

Constants

const Scheme = "constant"

Scheme is the URL scheme constantvar registers its URLOpener under on blob.DefaultMux.

func New Uses

func New(value interface{}) *runtimevar.Variable

New constructs a *runtimevar.Variable holding value.

Code:

// 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 Uses

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.

Code:

// 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 Uses

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.

Code:

// 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

type URLOpener Uses

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 Uses

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens the variable at the URL's path. See the package doc for more details.

Package constantvar imports 8 packages (graph) and is imported by 1 packages. Updated 2019-05-16. Refresh now. Tools for package owners.