diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-30 11:03:45 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-30 11:03:45 -0600 |
| commit | c1698f896ff343b1b65e57d3961a78d3bb263b7c (patch) | |
| tree | 3eecd594af67f4849ccea1d8ad758004ccfdec9c | |
| parent | 0626bc0cfffa89b73adc2f9576354e8462270eae (diff) | |
refactor: rename repository types
| -rw-r--r-- | app/db/authorization.go (renamed from app/db/secure_repository.go) | 20 | ||||
| -rw-r--r-- | app/db/publisher.go (renamed from app/db/publishing_repository.go) | 6 | ||||
| -rw-r--r-- | app/db/publisher_test.go (renamed from app/db/publishing_repository_test.go) | 2 | ||||
| -rw-r--r-- | app/init.go | 10 |
4 files changed, 20 insertions, 18 deletions
diff --git a/app/db/secure_repository.go b/app/db/authorization.go index 26b85c9..3041f51 100644 --- a/app/db/secure_repository.go +++ b/app/db/authorization.go @@ -12,19 +12,21 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" ) -type SecureRepository[T domain.Entity] struct { +type authorization[T domain.Entity] struct { client *authzed.Client repository domain.Repository[T] } -func NewSecureRepository[T domain.Entity](client *authzed.Client, repository domain.Repository[T]) domain.Repository[T] { - return &SecureRepository[T]{ - client: client, - repository: repository, +func WithAuthorization[T domain.Entity](client *authzed.Client) x.Option[domain.Repository[T]] { + return func(repository domain.Repository[T]) domain.Repository[T] { + return &authorization[T]{ + client: client, + repository: repository, + } } } -func (r *SecureRepository[T]) All(ctx context.Context) []T { +func (r *authorization[T]) All(ctx context.Context) []T { allItems := r.repository.All(ctx) if len(allItems) == 0 { return allItems @@ -56,7 +58,7 @@ func (r *SecureRepository[T]) All(ctx context.Context) []T { return filteredItems } -func (r *SecureRepository[T]) Find(ctx context.Context, id domain.ID) T { +func (r *authorization[T]) Find(ctx context.Context, id domain.ID) T { item := r.repository.Find(ctx, id) response, err := r.client.CheckPermission(ctx, &v1.CheckPermissionRequest{ @@ -76,7 +78,7 @@ func (r *SecureRepository[T]) Find(ctx context.Context, id domain.ID) T { return item } -func (r *SecureRepository[T]) Save(ctx context.Context, item T) error { +func (r *authorization[T]) Save(ctx context.Context, item T) error { currentUser := cfg.CurrentUser.From(ctx) if currentUser == nil { return errors.New("authentication required for creating or updating entities") @@ -104,7 +106,7 @@ func (r *SecureRepository[T]) Save(ctx context.Context, item T) error { return r.repository.Save(ctx, item) } -func (r *SecureRepository[T]) subjectFrom(ctx context.Context) *v1.SubjectReference { +func (r *authorization[T]) subjectFrom(ctx context.Context) *v1.SubjectReference { currentUser := cfg.CurrentUser.From(ctx) if currentUser == nil { return &v1.SubjectReference{ diff --git a/app/db/publishing_repository.go b/app/db/publisher.go index 049129e..ee2a966 100644 --- a/app/db/publishing_repository.go +++ b/app/db/publisher.go @@ -8,21 +8,21 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" ) -type publishingRepository[T domain.Entity] struct { +type publisher[T domain.Entity] struct { aggregator *event.TypedAggregator[T] domain.Repository[T] } func WithPublishing[T domain.Entity](aggregator *event.TypedAggregator[T]) x.Option[domain.Repository[T]] { return func(repository domain.Repository[T]) domain.Repository[T] { - return &publishingRepository[T]{ + return &publisher[T]{ aggregator: aggregator, Repository: repository, } } } -func (r *publishingRepository[T]) Save(ctx context.Context, item T) error { +func (r *publisher[T]) Save(ctx context.Context, item T) error { err := r.Repository.Save(ctx, item) if err == nil { r.aggregator.Publish("after.create", item) diff --git a/app/db/publishing_repository_test.go b/app/db/publisher_test.go index 4a2c05f..eeb912a 100644 --- a/app/db/publishing_repository_test.go +++ b/app/db/publisher_test.go @@ -9,7 +9,7 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" ) -func TestPublishingRepository(t *testing.T) { +func TestWithPublishing(t *testing.T) { aggregator := event.New[*domain.Sparkle]() storage := NewRepository[*domain.Sparkle](WithPublishing(aggregator)) diff --git a/app/init.go b/app/init.go index 3a771f8..398c8a9 100644 --- a/app/init.go +++ b/app/init.go @@ -48,11 +48,11 @@ func init() { }) ioc.RegisterSingleton[domain.Repository[*domain.Sparkle]](c, func() domain.Repository[*domain.Sparkle] { aggregator := ioc.MustResolve[*event.TypedAggregator[*domain.Sparkle]](c) - repo := db.NewRepository[*domain.Sparkle](db.WithPublishing(aggregator)) - // repo := db.NewPublishingRepository[*domain.Sparkle](aggregator, db.NewRepository[*domain.Sparkle]()) - return db.NewSecureRepository[*domain.Sparkle]( - ioc.MustResolve[*authzed.Client](c), - repo, + client := ioc.MustResolve[*authzed.Client](c) + + return db.NewRepository[*domain.Sparkle]( + db.WithAuthorization[*domain.Sparkle](client), + db.WithPublishing(aggregator), ) }) ioc.RegisterSingleton[*http.ServeMux](c, func() *http.ServeMux { |
