Documentation
¶
Overview ¶
Package constantvar provides a runtimevar implementation with Variables that never change. Use New, NewBytes, or NewError to construct a *runtimevar.Variable.
As ¶
constantvar does not support any types for As.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotExist = errors.New("variable does not exist")
ErrNotExist is a sentinel error that can be used with NewError to return an error for which IsNotExist returns true.
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() // Verify the variable value. snapshot, err := v.Watch(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Value: %#v\n", snapshot.Value.(MyConfig)) }
Output: Value: constantvar_test.MyConfig{Server:"foo.com", 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() { const ( // cfgJSON is a JSON string that can be decoded into a MyConfig. cfgJSON = `{"Server": "foo.com", "Port": 80}` // badJSON is a JSON string that cannot be decoded into a MyConfig. badJSON = `{"Server": 42}` ) // Create a decoder for decoding JSON strings into MyConfig. decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode) // Construct a runtimevar.Variable using badJSON. It will return an error // since the JSON doesn't decode successfully. v := constantvar.NewBytes([]byte(badJSON), decoder) _, err := v.Watch(context.Background()) if err == nil { log.Fatal("Expected an error!") } v.Close() // Try again with valid JSON. v = constantvar.NewBytes([]byte(cfgJSON), decoder) defer v.Close() // Verify the variable value. snapshot, err := v.Watch(context.Background()) if err != nil { log.Fatal(err) } fmt.Printf("Value: %#v\n", snapshot.Value.(MyConfig)) }
Output: Value: constantvar_test.MyConfig{Server:"foo.com", 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() { var errFake = errors.New("my error") // Construct a runtimevar.Variable that always returns errFake. v := constantvar.NewError(errFake) defer v.Close() // The variable returns an error. It is wrapped by runtimevar, so it's // not equal to errFake. _, err := v.Watch(context.Background()) if err == nil { log.Fatal("Expected an error!") } fmt.Println(err) }
Output: runtimevar: my error
Types ¶
This section is empty.