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() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/constantvar"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.

	v, err := runtimevar.OpenVariable(ctx, "constant://?val=hello+world&decoder=string")
	if err != nil {
		log.Fatal(err)
	}
	defer v.Close()
	// PRAGMA: On gocloud.dev, hide the rest of the function.
	snapshot, err := v.Latest(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(snapshot.Value.(string))

	// Output
	// hello world
}
Output:

Index

Examples

Constants

View Source
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"
)

func main() {
	// Construct a *runtimevar.Variable that always returns "hello world".
	v := constantvar.New("hello world")
	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)
	}
	fmt.Println(snapshot.Value.(string))

}
Output:

hello world

func NewBytes

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"
)

func main() {
	// Construct a *runtimevar.Variable with a []byte.
	v := constantvar.NewBytes([]byte(`hello world`), runtimevar.BytesDecoder)
	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)
	}
	fmt.Printf("byte slice of length %d\n", len(snapshot.Value.([]byte)))

}
Output:

byte slice of length 11

func NewError

func NewError(err error) *runtimevar.Variable

NewError constructs a *runtimevar.Variable that always fails. Runtimevar wraps errors returned by drivers, 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

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

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.

Source Files