Documentation ¶
Overview ¶
Package datastore provides a Store capable of storing configs on datastore.
The entry point is the New function, that allows you to create a Datastore object.
The Datastore object can then be used to open as many config stores as necessary via the Open function, that can be used anywhere a store.Opener is accepted.
The Open function returns a proper Store, implementing the Marshal, Unmarshal, and Delete methods.
For example:
ds, err := datastore.New() if err != nil { ... } ids, err := ds.Open("myapp1", "identities") if err != nil { ... err, _ := ids.Marshal("carlo@enfabrica.net", credentials) if err != nil { ... err, _ := ids.Marshal("default", credentials) if err != nil { ... epts, err := ds.Open("myapp1", "endpoints") if err != nil { ... err, _ := epts.Marshal("server1", parameters) err, _ := epts.Marshal("server2", parameters)
There are two main optional parameters that can be passed to datastore.New: a ContextGenerator, and a KeyInitializer.
A ContextGenerator returns a new context.Context every time it is invoked. It can be used to set timeouts for operations, implement cancellations, or simply change the context used at run time.
A KeyInitializer generates a datastore.Key based on the parameters passed to Open and Marshal. It can be used to map Marshal and Unmarshal operations to arbitrary objects in the datastore tree.
To pass google options, a KeyInitializer, or ContextGenerator, you can use one of the functional setters with datastore.New(). For example:
ds, err := datastore.New(WithGoogleOptions(opts), WithKeyInitializer(myfunc), WithContextGenerator(mybar))
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultKeyGenerator = func(root *datastore.Key) KeyGenerator { return func(element string) (*datastore.Key, error) { return datastore.NameKey(KindEl, element, root), nil } }
DefaultKeyGenerator generates a key by appending "el=key" to the specified root.
var KindApp = "app"
var KindEl = "el"
var KindNs = "ns"
Functions ¶
This section is empty.
Types ¶
type ContextGenerator ¶
ContextGenerator is a function capable of initializing or generating a context.
var DefaultContextGenerator ContextGenerator = func() context.Context { return context.Background() }
DefaultContextGenerator returns a context with no deadline and no cancellation.
type Datastore ¶
type Datastore struct { Client *datastore.Client InitializeKey KeyInitializer GenerateContext ContextGenerator }
type KeyGenerator ¶
KeyGenerator is a function capable of generating a datastore key from a Marshal key.
type KeyInitializer ¶
type KeyInitializer func(app string, namespaces ...string) (KeyGenerator, error)
KeyInitializer is a function capable of creating a KeyGenerator from Opener parameters.
var DefaultKeyInitializer KeyInitializer = func(app string, namespaces ...string) (KeyGenerator, error) { root := datastore.NameKey(KindApp, app, nil) for _, ns := range namespaces { root = datastore.NameKey(KindNs, ns, root) } return DefaultKeyGenerator(root), nil }
DefaultKeyInitializer returns a KeyGenerator using "app=myapp,ns=namespace,ns=namespace,..." as root based on Open() parameters.
type Modifier ¶
type Modifier func(opt *options) error
func WithContextGenerator ¶
func WithContextGenerator(cgenerator ContextGenerator) Modifier
func WithGoogleOptions ¶
func WithGoogleOptions(option ...option.ClientOption) Modifier
func WithKeyInitializer ¶
func WithKeyInitializer(ki KeyInitializer) Modifier
func WithProject ¶
WithProject specifies the datastore project name.
type Storer ¶
type Storer struct { Parent *Datastore GenerateKey KeyGenerator GenerateContext ContextGenerator }