Documentation ¶
Overview ¶
Package blobvar provides a runtimevar implementation with variables read from a blob.Bucket. Use NewVariable to construct a *runtimevar.Variable.
As ¶
blobvar exposes the following types for As:
- Snapshot: Not supported.
- Error: error, which can be passed to blob.ErrorAs.
Example ¶
package main import ( "context" "fmt" "log" "gocloud.dev/blob/memblob" "gocloud.dev/runtimevar" "gocloud.dev/runtimevar/blobvar" ) // MyConfig is a sample configuration struct. type MyConfig struct { Server string Port int } func main() { // Create a *blob.Bucket. // Here, we use an in-memory implementation and write a sample // configuration value. bucket := memblob.OpenBucket(nil) ctx := context.Background() err := bucket.WriteAll(ctx, "cfg-variable-name", []byte(`{"Server": "foo.com", "Port": 80}`), nil) if err != nil { log.Fatal(err) } // Create a decoder for decoding JSON strings into MyConfig. decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode) // Construct a *runtimevar.Variable that watches the blob. v, err := blobvar.NewVariable(bucket, "cfg-variable-name", decoder, nil) if err != nil { log.Fatal(err) } defer v.Close() // We can now read the current value of the variable from v. snapshot, err := v.Watch(ctx) if err != nil { log.Fatal(err) } // runtimevar.Snapshot.Value is decoded to type MyConfig. cfg := snapshot.Value.(MyConfig) fmt.Printf("%s running on port %d", cfg.Server, cfg.Port) }
Output: foo.com running on port 80
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewVariable ¶
func NewVariable(bucket *blob.Bucket, key string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)
NewVariable constructs a *runtimevar.Variable backed by the referenced blob. Reads of the blob return raw bytes; provide a decoder to decode the raw bytes into the appropriate type for runtimevar.Snapshot.Value. See the runtimevar package documentation for examples of decoders.
Types ¶
Click to show internal directories.
Click to hide internal directories.