Skip to content

Commit

Permalink
Remove database mocks, replace Service interface indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
dstotijn committed Jan 3, 2025
1 parent d23b4ed commit 24c2ecf
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 1,294 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/dgraph-io/badger/v3 v3.2103.2
github.com/google/go-cmp v0.5.6
github.com/gorilla/mux v1.7.4
github.com/matryer/moq v0.2.5
github.com/mitchellh/go-homedir v1.1.0
github.com/oklog/ulid v1.3.1
github.com/peterbourgon/ff/v3 v3.1.2
Expand Down Expand Up @@ -40,6 +39,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matryer/moq v0.2.5 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ var revHTTPProtocolMap = map[HTTPProtocol]string{
}

type Resolver struct {
ProjectService proj.Service
RequestLogService reqlog.Service
ProjectService *proj.Service
RequestLogService *reqlog.Service
InterceptService *intercept.Service
SenderService sender.Service
SenderService *sender.Service
}

type (
Expand Down Expand Up @@ -865,7 +865,7 @@ func parseInterceptItem(item intercept.Item) (req HTTPRequest, err error) {
return req, nil
}

func parseProject(projSvc proj.Service, p proj.Project) Project {
func parseProject(projSvc *proj.Service, p proj.Project) Project {
project := Project{
ID: p.ID,
Name: p.Name,
Expand Down
54 changes: 19 additions & 35 deletions pkg/proj/proj.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,11 @@ import (
//nolint:gosec
var ulidEntropy = rand.New(rand.NewSource(time.Now().UnixNano()))

// Service is used for managing projects.
type Service interface {
CreateProject(ctx context.Context, name string) (Project, error)
OpenProject(ctx context.Context, projectID ulid.ULID) (Project, error)
CloseProject() error
DeleteProject(ctx context.Context, projectID ulid.ULID) error
ActiveProject(ctx context.Context) (Project, error)
IsProjectActive(projectID ulid.ULID) bool
Projects(ctx context.Context) ([]Project, error)
Scope() *scope.Scope
SetScopeRules(ctx context.Context, rules []scope.Rule) error
SetRequestLogFindFilter(ctx context.Context, filter reqlog.FindRequestsFilter) error
SetSenderRequestFindFilter(ctx context.Context, filter sender.FindRequestsFilter) error
UpdateInterceptSettings(ctx context.Context, settings intercept.Settings) error
}

type service struct {
type Service struct {
repo Repository
interceptSvc *intercept.Service
reqLogSvc reqlog.Service
senderSvc sender.Service
reqLogSvc *reqlog.Service
senderSvc *sender.Service
scope *scope.Scope
activeProjectID ulid.ULID
mu sync.RWMutex
Expand Down Expand Up @@ -87,14 +71,14 @@ var nameRegexp = regexp.MustCompile(`^[\w\d\s]+$`)
type Config struct {
Repository Repository
InterceptService *intercept.Service
ReqLogService reqlog.Service
SenderService sender.Service
ReqLogService *reqlog.Service
SenderService *sender.Service
Scope *scope.Scope
}

// NewService returns a new Service.
func NewService(cfg Config) (Service, error) {
return &service{
func NewService(cfg Config) (*Service, error) {
return &Service{
repo: cfg.Repository,
interceptSvc: cfg.InterceptService,
reqLogSvc: cfg.ReqLogService,
Expand All @@ -103,7 +87,7 @@ func NewService(cfg Config) (Service, error) {
}, nil
}

func (svc *service) CreateProject(ctx context.Context, name string) (Project, error) {
func (svc *Service) CreateProject(ctx context.Context, name string) (Project, error) {
if !nameRegexp.MatchString(name) {
return Project{}, ErrInvalidName
}
Expand All @@ -122,7 +106,7 @@ func (svc *service) CreateProject(ctx context.Context, name string) (Project, er
}

// CloseProject closes the currently open project (if there is one).
func (svc *service) CloseProject() error {
func (svc *Service) CloseProject() error {
svc.mu.Lock()
defer svc.mu.Unlock()

Expand All @@ -148,7 +132,7 @@ func (svc *service) CloseProject() error {
}

// DeleteProject removes a project from the repository.
func (svc *service) DeleteProject(ctx context.Context, projectID ulid.ULID) error {
func (svc *Service) DeleteProject(ctx context.Context, projectID ulid.ULID) error {
if svc.activeProjectID.Compare(projectID) == 0 {
return fmt.Errorf("proj: project (%v) is active", projectID.String())
}
Expand All @@ -161,7 +145,7 @@ func (svc *service) DeleteProject(ctx context.Context, projectID ulid.ULID) erro
}

// OpenProject sets a project as the currently active project.
func (svc *service) OpenProject(ctx context.Context, projectID ulid.ULID) (Project, error) {
func (svc *Service) OpenProject(ctx context.Context, projectID ulid.ULID) (Project, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

Expand Down Expand Up @@ -203,7 +187,7 @@ func (svc *service) OpenProject(ctx context.Context, projectID ulid.ULID) (Proje
return project, nil
}

func (svc *service) ActiveProject(ctx context.Context) (Project, error) {
func (svc *Service) ActiveProject(ctx context.Context) (Project, error) {
activeProjectID := svc.activeProjectID
if activeProjectID.Compare(ulid.ULID{}) == 0 {
return Project{}, ErrNoProject
Expand All @@ -219,7 +203,7 @@ func (svc *service) ActiveProject(ctx context.Context) (Project, error) {
return project, nil
}

func (svc *service) Projects(ctx context.Context) ([]Project, error) {
func (svc *Service) Projects(ctx context.Context) ([]Project, error) {
projects, err := svc.repo.Projects(ctx)
if err != nil {
return nil, fmt.Errorf("proj: could not get projects: %w", err)
Expand All @@ -228,11 +212,11 @@ func (svc *service) Projects(ctx context.Context) ([]Project, error) {
return projects, nil
}

func (svc *service) Scope() *scope.Scope {
func (svc *Service) Scope() *scope.Scope {
return svc.scope
}

func (svc *service) SetScopeRules(ctx context.Context, rules []scope.Rule) error {
func (svc *Service) SetScopeRules(ctx context.Context, rules []scope.Rule) error {
project, err := svc.ActiveProject(ctx)
if err != nil {
return err
Expand All @@ -250,7 +234,7 @@ func (svc *service) SetScopeRules(ctx context.Context, rules []scope.Rule) error
return nil
}

func (svc *service) SetRequestLogFindFilter(ctx context.Context, filter reqlog.FindRequestsFilter) error {
func (svc *Service) SetRequestLogFindFilter(ctx context.Context, filter reqlog.FindRequestsFilter) error {
project, err := svc.ActiveProject(ctx)
if err != nil {
return err
Expand All @@ -271,7 +255,7 @@ func (svc *service) SetRequestLogFindFilter(ctx context.Context, filter reqlog.F
return nil
}

func (svc *service) SetSenderRequestFindFilter(ctx context.Context, filter sender.FindRequestsFilter) error {
func (svc *Service) SetSenderRequestFindFilter(ctx context.Context, filter sender.FindRequestsFilter) error {
project, err := svc.ActiveProject(ctx)
if err != nil {
return err
Expand All @@ -292,11 +276,11 @@ func (svc *service) SetSenderRequestFindFilter(ctx context.Context, filter sende
return nil
}

func (svc *service) IsProjectActive(projectID ulid.ULID) bool {
func (svc *Service) IsProjectActive(projectID ulid.ULID) bool {
return projectID.Compare(svc.activeProjectID) == 0
}

func (svc *service) UpdateInterceptSettings(ctx context.Context, settings intercept.Settings) error {
func (svc *Service) UpdateInterceptSettings(ctx context.Context, settings intercept.Settings) error {
project, err := svc.ActiveProject(ctx)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 24c2ecf

Please sign in to comment.