datastore

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var NewRecorder = datastore.NewRecorder

NewRecorder from datastore.NewRecorder

Functions

func Int

func Int(ctx context.Context, fetch FetchFunc, keyFmt string, a ...interface{}) int

Int fetches an integer from the datastore.

Example
package main

import (
	"context"
	"fmt"

	"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
	"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

func main() {
	shutdownCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	fetch := datastore.NewFetcher(dsmock.NewMockDatastore(shutdownCtx.Done(), map[string][]byte{
		"testmodel/1/id": []byte("1"),
	}))

	i := datastore.Int(context.Background(), fetch.Fetch, "testmodel/%d/id", 1)

	fmt.Println(i)
	fmt.Println(fetch.Err())
}
Output:

1
<nil>
Example (DoesNotExist)
package main

import (
	"context"
	"fmt"

	"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
	"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

func main() {
	shutdownCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	fetch := datastore.NewFetcher(dsmock.NewMockDatastore(shutdownCtx.Done(), nil))

	i := datastore.Int(context.Background(), fetch.Fetch, "testmodel/%d/number", 1)

	fmt.Println(i)
	fmt.Println(fetch.Err())
}
Output:

0
<nil>
Example (IfExist)
package main

import (
	"context"
	"fmt"

	"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
	"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

func main() {
	shutdownCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	fetch := datastore.NewFetcher(dsmock.NewMockDatastore(shutdownCtx.Done(), nil))

	i := datastore.Int(context.Background(), fetch.FetchIfExist, "testmodel/%d/number", 1)

	fmt.Println(i)
	fmt.Println(fetch.Err())
}
Output:

0
testmodel/1 does not exist.
Example (WrongType)
package main

import (
	"context"
	"fmt"

	"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
	"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

func main() {
	shutdownCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	fetch := datastore.NewFetcher(dsmock.NewMockDatastore(shutdownCtx.Done(), map[string][]byte{
		"testmodel/1/id": []byte(`"a string"`),
	}))

	i := datastore.Int(context.Background(), fetch.Fetch, "testmodel/%d/id", 1)

	fmt.Println(i)
	fmt.Println(fetch.Err() == nil)
}
Output:

0
false

func Ints

func Ints(ctx context.Context, fetch FetchFunc, keyFmt string, a ...interface{}) []int

Ints fetches an int slice from the datastore.

Example
package main

import (
	"context"
	"fmt"

	"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
	"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

func main() {
	shutdownCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	fetch := datastore.NewFetcher(dsmock.NewMockDatastore(shutdownCtx.Done(), map[string][]byte{
		"testmodel/1/ids": []byte("[1,2,3]"),
	}))

	ints := datastore.Ints(context.Background(), fetch.Fetch, "testmodel/%d/ids", 1)

	fmt.Println(ints)
	fmt.Println(fetch.Err())
}
Output:

[1 2 3]
<nil>

func String

func String(ctx context.Context, fetch FetchFunc, keyFmt string, a ...interface{}) string

String fetches a string from the datastore.

Example
package main

import (
	"context"
	"fmt"

	"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
	"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

func main() {
	shutdownCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	fetch := datastore.NewFetcher(dsmock.NewMockDatastore(shutdownCtx.Done(), map[string][]byte{
		"testmodel/1/name": []byte(`"hugo"`),
	}))

	str := datastore.String(context.Background(), fetch.Fetch, "testmodel/%d/name", 1)

	fmt.Println(str)
	fmt.Println(fetch.Err())
}
Output:

hugo
<nil>

Types

type DoesNotExistError

type DoesNotExistError = datastore.DoesNotExistError

DoesNotExistError is a type alias from datastore.DoesNotExistError

type FetchFunc

type FetchFunc func(ctx context.Context, value interface{}, keyFmt string, a ...interface{})

FetchFunc is a function that fetches a value. It has the signature of fetch.Fetch() or fetch.FetchIfExist().

type Fetcher

type Fetcher struct {
	// contains filtered or unexported fields
}

Fetcher is a helper to fetch many keys from the datastore.

The methods do not return an error. If an error happens, it is saved internaly. As soon, as an error happens, all later calls to methods of that fetcher are noops.

The method Fetcher.Err() can be used to get the error.

Make sure to call Fetcher.Err() at the end to see, if an error happend.

func NewFetcher

func NewFetcher(getter datastore.Getter) *Fetcher

NewFetcher initializes a Fetcher object.

func (*Fetcher) Err

func (f *Fetcher) Err() error

Err returns the error that happend at a method call. If no error happend, then Err() returns nil.

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(ctx context.Context, value interface{}, keyFmt string, a ...interface{})

Fetch gets a value from the datastore and saves it into the argument `value`.

If the object, that the key belongs to does not exist, no error is thrown.

To get the error, call f.Err().

func (*Fetcher) FetchIfExist

func (f *Fetcher) FetchIfExist(ctx context.Context, value interface{}, keyFmt string, a ...interface{})

FetchIfExist is like Fetch but if the element that the key belongs to does not exist, then a DoesNotExistError is returned.

func (*Fetcher) Object

func (f *Fetcher) Object(ctx context.Context, fqID string, fields ...string) map[string]json.RawMessage

Object returns a json object for the given fqid with all given fields.

If one field does not exist in the datastore, then it is returned as nil.

If the object does not exist, then a DoesNotExistError is thrown.

Jump to

Keyboard shortcuts

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