Skip to content

Commit

Permalink
Cleanup datastore package (#400)
Browse files Browse the repository at this point in the history
* Remove fetch api
* Move it to projector, since it is used there
  • Loading branch information
ostcar authored Jan 15, 2022
1 parent 7079d96 commit f744939
Show file tree
Hide file tree
Showing 32 changed files with 85 additions and 65 deletions.
9 changes: 9 additions & 0 deletions internal/projector/datastore/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package datastore

import "github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"

// NewRecorder from datastore.NewRecorder
var NewRecorder = datastore.NewRecorder

// DoesNotExistError is a type alias from datastore.DoesNotExistError
type DoesNotExistError = datastore.DoesNotExistError
25 changes: 6 additions & 19 deletions pkg/datastore/fetch.go → internal/projector/datastore/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ import (
"encoding/json"
"fmt"
"strings"
)

// Getter can get values from keys.
//
// The Datastore object implements this interface.
type Getter interface {
Get(ctx context.Context, keys ...string) (map[string][]byte, error)
}
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
)

// Fetcher is a helper to fetch many keys from the datastore.
//
Expand All @@ -24,12 +19,12 @@ type Getter interface {
//
// Make sure to call Fetcher.Err() at the end to see, if an error happend.
type Fetcher struct {
getter Getter
getter datastore.Getter
err error
}

// NewFetcher initializes a Fetcher object.
func NewFetcher(getter Getter) *Fetcher {
func NewFetcher(getter datastore.Getter) *Fetcher {
return &Fetcher{getter: getter}
}

Expand Down Expand Up @@ -85,7 +80,7 @@ func (f *Fetcher) FetchIfExist(ctx context.Context, value interface{}, keyFmt st
}

if fields[idField] == nil {
f.err = DoesNotExistError(fqid)
f.err = datastore.DoesNotExistError(fqid)
return
}
if fields[fqfield] == nil {
Expand Down Expand Up @@ -121,7 +116,7 @@ func (f *Fetcher) Object(ctx context.Context, fqID string, fields ...string) map
}

if vals[fqID+"/id"] == nil {
f.err = DoesNotExistError(fqID)
f.err = datastore.DoesNotExistError(fqID)
return nil
}

Expand Down Expand Up @@ -163,11 +158,3 @@ func String(ctx context.Context, fetch FetchFunc, keyFmt string, a ...interface{
fetch(ctx, &value, keyFmt, a...)
return value
}

// DoesNotExistError is thrown by the methods of a Fetcher when an object does
// not exist.
type DoesNotExistError string

func (e DoesNotExistError) Error() string {
return fmt.Sprintf("%s does not exist.", string(e))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/projector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"strings"

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

// Datastore gets values for keys and informs, if they change.
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/projector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/agenda.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

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

type dbAgendaItem struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/agenda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sort"

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

type dbAssignment struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/list_of_speakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

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

type dbListOfSpeakers struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/list_of_speakers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

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

type dbMediafile struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/mediafile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/meeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"

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

type dbMeeting struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/motion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

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

type dbMotionState struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/motion_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

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

type dbMotionBlock struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/motion_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/motion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

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

type optionRepr struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/poll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/projector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

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

type dbProjectorCountdown struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/projector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"

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

type dbTopic struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

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

// DbUser is the class with methods to get needed User Informations
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slide/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/OpenSlides/openslides-autoupdate-service/internal/projector"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/internal/projector/slide"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/projector/slides.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"

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

// Slider knows how to create a slide.
Expand Down
13 changes: 13 additions & 0 deletions pkg/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ const (
httpTimeout = 3 * time.Second
)

// Updater returns keys that have changes. Blocks until there is
// changed data.
type Updater interface {
Update(context.Context) (map[string][]byte, error)
}

// Getter can get values from keys.
//
// The Datastore object implements this interface.
type Getter interface {
Get(ctx context.Context, keys ...string) (map[string][]byte, error)
}

// Datastore can be used to get values from the datastore-service.
//
// Has to be created with datastore.New().
Expand Down
23 changes: 11 additions & 12 deletions pkg/datastore/gen_request/value.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ type {{.TypeName}} struct {
}
{{end}}

{{if not .MaybeType}}
// Lazy sets a value as soon as it es executed.
//
// Make sure to call request.Execute() before using the value.
func (v *{{.TypeName}}) Lazy(value *{{.GoType}}) {
v.lazies = append(v.lazies, value)
}
{{end}}

// Lazy sets a value as soon as it es executed.
//
// Make sure to call request.Execute() before using the value.
func (v *{{.TypeName}}) Lazy(value *{{.GoType}}) {
v.lazies = append(v.lazies, value)
}

{{if not .MaybeType}}
// ErrorLater is like Value but does not return an error.
Expand All @@ -67,10 +66,10 @@ type {{.TypeName}} struct {
return v.value
}
{{else}}
// ErrorLater is like Value but does not return an error.
//
// If an error happs, it is saved internaly. Make sure to call request.Err() later to
// access it.
// ErrorLater is like Value but does not return an error.
//
// If an error happs, it is saved internaly. Make sure to call request.Err() later to
// access it.
func (v *{{.TypeName}}) ErrorLater(ctx context.Context) ({{.GoType}}, bool) {
if v.request.err != nil {
return {{.Zero}}, false
Expand Down
9 changes: 0 additions & 9 deletions pkg/datastore/interfaces.go

This file was deleted.

7 changes: 7 additions & 0 deletions pkg/datastore/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,10 @@ func (v *ValueRequiredInt) execute(p []byte) error {
v.executed = true
return nil
}

// DoesNotExistError is thrown when an object does not exist.
type DoesNotExistError string

func (e DoesNotExistError) Error() string {
return fmt.Sprintf("%s does not exist.", string(e))
}
Loading

0 comments on commit f744939

Please sign in to comment.