package blobvar
import "gocloud.dev/runtimevar/blobvar"
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.
Index ¶
- func NewVariable(bucket *blob.Bucket, key string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)
- type Options
Examples ¶
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.
Code:play
Output: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
}
// jsonCreds is a fake GCP JSON credentials file.
const jsonCreds = `
{
"type": "service_account",
"project_id": "my-project-id"
}
`
func main() {
ctx := context.Background()
// Create a *blob.Bucket.
// Here, we use an in-memory implementation.
bucket := memblob.OpenBucket(nil)
// 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, "blob name", decoder, nil)
if err != nil {
log.Fatal(err)
}
defer v.Close()
// You can now read the current value of the variable from v.
snapshot, err := v.Watch(ctx)
if err != nil {
// This is expected due to the fake credentials we used above.
fmt.Println("Watch failed due to blob not existing")
return
}
// We'll never get here when running this sample, but the resulting
// runtimevar.Snapshot.Value would be of type MyConfig.
log.Printf("Snapshot.Value: %#v", snapshot.Value.(MyConfig))
}
Watch failed due to blob not existing
Types ¶
type Options ¶
type Options struct { // WaitDuration controls the rate at which the blob is polled. // Defaults to 30 seconds. WaitDuration time.Duration }
Options sets options.
Source Files ¶
blobvar.go
- Version
- v0.9.0
- Published
- Jan 15, 2019
- Platform
- js/wasm
- Imports
- 6 packages
- Last checked
- 3 hours ago –
Tools for package owners.