From ab8075f02f50d8bd0be3c23b87e63f10828528ed Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 21 Apr 2025 12:31:39 -0600 Subject: refactor: convert Repository to Repository[T Entity] --- app/controllers/sparkles/controller.go | 4 +-- app/controllers/sparkles/controller_test.go | 4 +-- app/init.go | 7 ++--- pkg/db/repository.go | 41 ++++++++++++++++------------- pkg/db/repository_test.go | 4 +-- pkg/domain/sparkle.go | 9 +++++++ 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go index 061b5c6..bc388db 100644 --- a/app/controllers/sparkles/controller.go +++ b/app/controllers/sparkles/controller.go @@ -9,10 +9,10 @@ import ( ) type Controller struct { - db db.Repository + db db.Repository[*domain.Sparkle] } -func New(db db.Repository) *Controller { +func New(db db.Repository[*domain.Sparkle]) *Controller { return &Controller{db: db} } diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go index 17bc338..99fdd69 100644 --- a/app/controllers/sparkles/controller_test.go +++ b/app/controllers/sparkles/controller_test.go @@ -15,7 +15,7 @@ import ( func TestSparkles(t *testing.T) { t.Run("GET /sparkles", func(t *testing.T) { sparkle, _ := domain.NewSparkle("@tanuki for helping me") - store := db.NewRepository() + store := db.NewRepository[*domain.Sparkle]() store.Save(sparkle) mux := http.NewServeMux() @@ -43,7 +43,7 @@ func TestSparkles(t *testing.T) { t.Run("POST /sparkles", func(t *testing.T) { t.Run("saves a new sparkle", func(t *testing.T) { - repository := db.NewRepository() + repository := db.NewRepository[*domain.Sparkle]() mux := http.NewServeMux() controller := New(repository) controller.MountTo(mux) diff --git a/app/init.go b/app/init.go index 41435de..432f761 100644 --- a/app/init.go +++ b/app/init.go @@ -14,6 +14,7 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sessions" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sparkles" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web" "golang.org/x/oauth2" @@ -23,8 +24,8 @@ func init() { ioc.RegisterSingleton[*zerolog.Logger](ioc.Default, func() *zerolog.Logger { return log.New(os.Stdout, log.Fields{}) }) - ioc.RegisterSingleton[db.Repository](ioc.Default, func() db.Repository { - return db.NewRepository() + ioc.RegisterSingleton[db.Repository[*domain.Sparkle]](ioc.Default, func() db.Repository[*domain.Sparkle] { + return db.NewRepository[*domain.Sparkle]() }) ioc.RegisterSingleton[*http.ServeMux](ioc.Default, func() *http.ServeMux { return http.NewServeMux() @@ -33,7 +34,7 @@ func init() { return dashboard.New() }) ioc.Register[*sparkles.Controller](ioc.Default, func() *sparkles.Controller { - return sparkles.New(ioc.MustResolve[db.Repository](ioc.Default)) + return sparkles.New(ioc.MustResolve[db.Repository[*domain.Sparkle]](ioc.Default)) }) ioc.Register[*health.Controller](ioc.Default, func() *health.Controller { return health.New() diff --git a/pkg/db/repository.go b/pkg/db/repository.go index ef4b9fb..79c7ae3 100644 --- a/pkg/db/repository.go +++ b/pkg/db/repository.go @@ -1,38 +1,41 @@ package db -import ( - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" -) - -type Repository interface { - All() []*domain.Sparkle - Save(*domain.Sparkle) error +import "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" + +type Entity interface { + GetID() string + SetID(id string) error + Validate() error +} + +type Repository[T Entity] interface { + All() []T + Save(T) error } -type inMemoryRepository struct { - sparkles []*domain.Sparkle +type inMemoryRepository[T Entity] struct { + items []T } -func NewRepository() Repository { - return &inMemoryRepository{ - sparkles: []*domain.Sparkle{}, +func NewRepository[T Entity]() Repository[T] { + return &inMemoryRepository[T]{ + items: []T{}, } } -func (r *inMemoryRepository) All() []*domain.Sparkle { - return r.sparkles +func (r *inMemoryRepository[T]) All() []T { + return r.items } -func (r *inMemoryRepository) Save(item *domain.Sparkle) error { +func (r *inMemoryRepository[T]) Save(item T) error { if err := item.Validate(); err != nil { return err } - if item.ID == "" { - item.ID = pls.GenerateULID() + if item.GetID() == "" { + item.SetID(pls.GenerateULID()) } - r.sparkles = append(r.sparkles, item) + r.items = append(r.items, item) return nil } diff --git a/pkg/db/repository_test.go b/pkg/db/repository_test.go index 57aee13..bb788d2 100644 --- a/pkg/db/repository_test.go +++ b/pkg/db/repository_test.go @@ -9,13 +9,13 @@ import ( ) func TestRepository(t *testing.T) { - storage := NewRepository() + storage := NewRepository[*domain.Sparkle]() t.Run("Save", func(t *testing.T) { t.Run("an invalid Sparkle", func(t *testing.T) { err := storage.Save(&domain.Sparkle{Reason: "because"}) - assert.NotNil(t, err) + assert.Error(t, err) assert.Equal(t, 0, len(storage.All())) }) diff --git a/pkg/domain/sparkle.go b/pkg/domain/sparkle.go index a139d56..3f8b4f4 100644 --- a/pkg/domain/sparkle.go +++ b/pkg/domain/sparkle.go @@ -39,6 +39,15 @@ func NewSparkle(text string) (*Sparkle, error) { }, nil } +func (s *Sparkle) GetID() string { + return s.ID +} + +func (s *Sparkle) SetID(id string) error { + s.ID = id + return nil +} + func (s *Sparkle) Validate() error { if s.Sparklee == "" { return SparkleeIsRequired -- cgit v1.2.3