datastore

package
v0.0.0-...-5f133c6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 7 Imported by: 0

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

View Source
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.

View Source
var KindApp = "app"
View Source
var KindEl = "el"
View Source
var KindNs = "ns"

Functions

This section is empty.

Types

type ContextGenerator

type ContextGenerator func() context.Context

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
}

func New

func New(mods ...Modifier) (*Datastore, error)

func (*Datastore) Open

func (ds *Datastore) Open(app string, namespaces ...string) (config.Store, error)

type KeyGenerator

type KeyGenerator func(key string) (*datastore.Key, error)

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

func WithProject(project string) Modifier

WithProject specifies the datastore project name.

type Modifiers

type Modifiers []Modifier

func (Modifiers) Apply

func (mods Modifiers) Apply(opt *options) error

type Storer

type Storer struct {
	Parent          *Datastore
	GenerateKey     KeyGenerator
	GenerateContext ContextGenerator
}

func (*Storer) Delete

func (s *Storer) Delete(descriptor config.Descriptor) error

func (*Storer) List

func (s *Storer) List() ([]string, error)

func (*Storer) Marshal

func (s *Storer) Marshal(descriptor config.Descriptor, value interface{}) error

func (*Storer) Unmarshal

func (s *Storer) Unmarshal(name string, value interface{}) (config.Descriptor, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL