From f7449396e8f3f21ef17837efc4304ba2195287fc Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 15 Jan 2022 13:43:18 +0100 Subject: [PATCH] Cleanup datastore package (#400) * Remove fetch api * Move it to projector, since it is used there --- internal/projector/datastore/alias.go | 9 +++++++ .../projector}/datastore/fetch.go | 25 +++++-------------- .../projector}/datastore/fetch_test.go | 2 +- internal/projector/projector.go | 2 +- internal/projector/projector_test.go | 2 +- internal/projector/slide/agenda.go | 2 +- internal/projector/slide/agenda_test.go | 2 +- internal/projector/slide/assignment.go | 2 +- internal/projector/slide/assignment_test.go | 2 +- internal/projector/slide/list_of_speakers.go | 2 +- .../projector/slide/list_of_speakers_test.go | 2 +- internal/projector/slide/mediafile.go | 2 +- internal/projector/slide/mediafile_test.go | 2 +- internal/projector/slide/meeting.go | 2 +- internal/projector/slide/motion.go | 2 +- internal/projector/slide/motion_block.go | 2 +- internal/projector/slide/motion_block_test.go | 2 +- internal/projector/slide/motion_test.go | 2 +- internal/projector/slide/poll.go | 2 +- internal/projector/slide/poll_test.go | 2 +- internal/projector/slide/projector.go | 2 +- internal/projector/slide/projector_test.go | 2 +- internal/projector/slide/topic.go | 2 +- internal/projector/slide/topic_test.go | 2 +- internal/projector/slide/user.go | 2 +- internal/projector/slide/user_test.go | 2 +- internal/projector/slides.go | 2 +- pkg/datastore/datastore.go | 13 ++++++++++ pkg/datastore/gen_request/value.go.tmpl | 23 ++++++++--------- pkg/datastore/interfaces.go | 9 ------- pkg/datastore/request.go | 7 ++++++ pkg/datastore/request_generated.go | 14 +++++++++++ 32 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 internal/projector/datastore/alias.go rename {pkg => internal/projector}/datastore/fetch.go (88%) rename {pkg => internal/projector}/datastore/fetch_test.go (99%) delete mode 100644 pkg/datastore/interfaces.go diff --git a/internal/projector/datastore/alias.go b/internal/projector/datastore/alias.go new file mode 100644 index 00000000..256cbe3a --- /dev/null +++ b/internal/projector/datastore/alias.go @@ -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 diff --git a/pkg/datastore/fetch.go b/internal/projector/datastore/fetch.go similarity index 88% rename from pkg/datastore/fetch.go rename to internal/projector/datastore/fetch.go index 6358e90a..1a064835 100644 --- a/pkg/datastore/fetch.go +++ b/internal/projector/datastore/fetch.go @@ -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. // @@ -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} } @@ -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 { @@ -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 } @@ -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)) -} diff --git a/pkg/datastore/fetch_test.go b/internal/projector/datastore/fetch_test.go similarity index 99% rename from pkg/datastore/fetch_test.go rename to internal/projector/datastore/fetch_test.go index d73ab1a8..4a53a246 100644 --- a/pkg/datastore/fetch_test.go +++ b/internal/projector/datastore/fetch_test.go @@ -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" diff --git a/internal/projector/projector.go b/internal/projector/projector.go index 4c6c051d..d354007b 100644 --- a/internal/projector/projector.go +++ b/internal/projector/projector.go @@ -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. diff --git a/internal/projector/projector_test.go b/internal/projector/projector_test.go index 0719dea5..3450e2ff 100644 --- a/internal/projector/projector_test.go +++ b/internal/projector/projector_test.go @@ -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" diff --git a/internal/projector/slide/agenda.go b/internal/projector/slide/agenda.go index 590569b5..05c6989c 100644 --- a/internal/projector/slide/agenda.go +++ b/internal/projector/slide/agenda.go @@ -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 { diff --git a/internal/projector/slide/agenda_test.go b/internal/projector/slide/agenda_test.go index 4c284614..bd39a4ab 100644 --- a/internal/projector/slide/agenda_test.go +++ b/internal/projector/slide/agenda_test.go @@ -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" ) diff --git a/internal/projector/slide/assignment.go b/internal/projector/slide/assignment.go index bd6c3df5..f844426f 100644 --- a/internal/projector/slide/assignment.go +++ b/internal/projector/slide/assignment.go @@ -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 { diff --git a/internal/projector/slide/assignment_test.go b/internal/projector/slide/assignment_test.go index 69f5552c..a6299ddf 100644 --- a/internal/projector/slide/assignment_test.go +++ b/internal/projector/slide/assignment_test.go @@ -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" ) diff --git a/internal/projector/slide/list_of_speakers.go b/internal/projector/slide/list_of_speakers.go index c7e3ee80..e2025aa5 100644 --- a/internal/projector/slide/list_of_speakers.go +++ b/internal/projector/slide/list_of_speakers.go @@ -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 { diff --git a/internal/projector/slide/list_of_speakers_test.go b/internal/projector/slide/list_of_speakers_test.go index e7ef2817..1f83e642 100644 --- a/internal/projector/slide/list_of_speakers_test.go +++ b/internal/projector/slide/list_of_speakers_test.go @@ -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" diff --git a/internal/projector/slide/mediafile.go b/internal/projector/slide/mediafile.go index 7d19e240..c85c9889 100644 --- a/internal/projector/slide/mediafile.go +++ b/internal/projector/slide/mediafile.go @@ -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 { diff --git a/internal/projector/slide/mediafile_test.go b/internal/projector/slide/mediafile_test.go index 4679c0c4..a9ca6353 100644 --- a/internal/projector/slide/mediafile_test.go +++ b/internal/projector/slide/mediafile_test.go @@ -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" ) diff --git a/internal/projector/slide/meeting.go b/internal/projector/slide/meeting.go index ef5256f6..c3b0dcba 100644 --- a/internal/projector/slide/meeting.go +++ b/internal/projector/slide/meeting.go @@ -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 { diff --git a/internal/projector/slide/motion.go b/internal/projector/slide/motion.go index 29d9b305..32ccb715 100644 --- a/internal/projector/slide/motion.go +++ b/internal/projector/slide/motion.go @@ -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 { diff --git a/internal/projector/slide/motion_block.go b/internal/projector/slide/motion_block.go index 78100f74..235af9ef 100644 --- a/internal/projector/slide/motion_block.go +++ b/internal/projector/slide/motion_block.go @@ -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 { diff --git a/internal/projector/slide/motion_block_test.go b/internal/projector/slide/motion_block_test.go index d4cb6d51..fdde1566 100644 --- a/internal/projector/slide/motion_block_test.go +++ b/internal/projector/slide/motion_block_test.go @@ -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" ) diff --git a/internal/projector/slide/motion_test.go b/internal/projector/slide/motion_test.go index 89f3583b..0208ef75 100644 --- a/internal/projector/slide/motion_test.go +++ b/internal/projector/slide/motion_test.go @@ -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" ) diff --git a/internal/projector/slide/poll.go b/internal/projector/slide/poll.go index 189a4f37..8904073e 100644 --- a/internal/projector/slide/poll.go +++ b/internal/projector/slide/poll.go @@ -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 { diff --git a/internal/projector/slide/poll_test.go b/internal/projector/slide/poll_test.go index 936df00c..7418ca2a 100644 --- a/internal/projector/slide/poll_test.go +++ b/internal/projector/slide/poll_test.go @@ -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" ) diff --git a/internal/projector/slide/projector.go b/internal/projector/slide/projector.go index f6e4073b..e34150b8 100644 --- a/internal/projector/slide/projector.go +++ b/internal/projector/slide/projector.go @@ -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 { diff --git a/internal/projector/slide/projector_test.go b/internal/projector/slide/projector_test.go index 4f504055..95772699 100644 --- a/internal/projector/slide/projector_test.go +++ b/internal/projector/slide/projector_test.go @@ -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" ) diff --git a/internal/projector/slide/topic.go b/internal/projector/slide/topic.go index 620fa588..51ab5bee 100644 --- a/internal/projector/slide/topic.go +++ b/internal/projector/slide/topic.go @@ -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 { diff --git a/internal/projector/slide/topic_test.go b/internal/projector/slide/topic_test.go index 34ddebfc..13948d03 100644 --- a/internal/projector/slide/topic_test.go +++ b/internal/projector/slide/topic_test.go @@ -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" ) diff --git a/internal/projector/slide/user.go b/internal/projector/slide/user.go index ed17ac14..90407cee 100644 --- a/internal/projector/slide/user.go +++ b/internal/projector/slide/user.go @@ -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 diff --git a/internal/projector/slide/user_test.go b/internal/projector/slide/user_test.go index a4fa443c..008b586e 100644 --- a/internal/projector/slide/user_test.go +++ b/internal/projector/slide/user_test.go @@ -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" ) diff --git a/internal/projector/slides.go b/internal/projector/slides.go index dd4343fb..ede0ef63 100644 --- a/internal/projector/slides.go +++ b/internal/projector/slides.go @@ -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. diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index a27da333..f40b2a0f 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -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(). diff --git a/pkg/datastore/gen_request/value.go.tmpl b/pkg/datastore/gen_request/value.go.tmpl index c654ef20..fb5b5236 100644 --- a/pkg/datastore/gen_request/value.go.tmpl +++ b/pkg/datastore/gen_request/value.go.tmpl @@ -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. @@ -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 diff --git a/pkg/datastore/interfaces.go b/pkg/datastore/interfaces.go deleted file mode 100644 index d4fa04ad..00000000 --- a/pkg/datastore/interfaces.go +++ /dev/null @@ -1,9 +0,0 @@ -package datastore - -import "context" - -// Updater returns keys that have changes. Blocks until there is -// changed data. -type Updater interface { - Update(context.Context) (map[string][]byte, error) -} diff --git a/pkg/datastore/request.go b/pkg/datastore/request.go index b7350546..22136ee7 100644 --- a/pkg/datastore/request.go +++ b/pkg/datastore/request.go @@ -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)) +} diff --git a/pkg/datastore/request_generated.go b/pkg/datastore/request_generated.go index e6888d76..cdeb4b73 100644 --- a/pkg/datastore/request_generated.go +++ b/pkg/datastore/request_generated.go @@ -455,6 +455,13 @@ func (v *ValueMaybeInt) Value(ctx context.Context) (int, bool, error) { return v.value, !v.isNull, nil } +// Lazy sets a value as soon as it es executed. +// +// Make sure to call request.Execute() before using the value. +func (v *ValueMaybeInt) Lazy(value *int) { + v.lazies = append(v.lazies, value) +} + // 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 @@ -517,6 +524,13 @@ func (v *ValueMaybeString) Value(ctx context.Context) (string, bool, error) { return v.value, !v.isNull, nil } +// Lazy sets a value as soon as it es executed. +// +// Make sure to call request.Execute() before using the value. +func (v *ValueMaybeString) Lazy(value *string) { + v.lazies = append(v.lazies, value) +} + // 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