From b2f7d285687beaf5ce9274d39115fa4140c4851f Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Wed, 15 Nov 2023 10:05:23 +0000 Subject: [PATCH] chore(pkg/middlewares): use settings for cache and filter constructors (future proofing for compatibility) --- pkg/middlewares/cache/middleware.go | 13 ++++++++++--- pkg/middlewares/cache/settings.go | 22 ++++++++++++++++++++++ pkg/middlewares/filter/middleware.go | 13 ++++++++++--- pkg/middlewares/filter/settings.go | 22 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 pkg/middlewares/cache/settings.go create mode 100644 pkg/middlewares/filter/settings.go diff --git a/pkg/middlewares/cache/middleware.go b/pkg/middlewares/cache/middleware.go index 3707f53c..4debe740 100644 --- a/pkg/middlewares/cache/middleware.go +++ b/pkg/middlewares/cache/middleware.go @@ -1,6 +1,8 @@ package cache import ( + "fmt" + "github.com/miekg/dns" "github.com/qdm12/dns/v2/internal/stateful" ) @@ -9,10 +11,15 @@ type Middleware struct { cache Cache } -func New(cache Cache) *Middleware { - return &Middleware{ - cache: cache, +func New(settings Settings) (middleware *Middleware, err error) { + err = settings.Validate() + if err != nil { + return nil, fmt.Errorf("settings validation: %w", err) } + + return &Middleware{ + cache: settings.Cache, + }, nil } func (m *Middleware) Wrap(next dns.Handler) dns.Handler { //nolint:ireturn diff --git a/pkg/middlewares/cache/settings.go b/pkg/middlewares/cache/settings.go new file mode 100644 index 00000000..147b3b25 --- /dev/null +++ b/pkg/middlewares/cache/settings.go @@ -0,0 +1,22 @@ +package cache + +import ( + "errors" + "fmt" +) + +type Settings struct { + Cache Cache +} + +var ( + ErrCacheMustBeSet = errors.New("cache must be set") +) + +func (s *Settings) Validate() (err error) { + if s.Cache == nil { + return fmt.Errorf("%w", ErrCacheMustBeSet) + } + + return nil +} diff --git a/pkg/middlewares/filter/middleware.go b/pkg/middlewares/filter/middleware.go index 5c394c65..37167116 100644 --- a/pkg/middlewares/filter/middleware.go +++ b/pkg/middlewares/filter/middleware.go @@ -1,6 +1,8 @@ package filter import ( + "fmt" + "github.com/miekg/dns" "github.com/qdm12/dns/v2/internal/stateful" ) @@ -9,10 +11,15 @@ type Middleware struct { filter Filter } -func New(filter Filter) *Middleware { - return &Middleware{ - filter: filter, +func New(settings Settings) (middleware *Middleware, err error) { + err = settings.Validate() + if err != nil { + return nil, fmt.Errorf("settings validation: %w", err) } + + return &Middleware{ + filter: settings.Filter, + }, nil } func (m *Middleware) Wrap(next dns.Handler) dns.Handler { //nolint:ireturn diff --git a/pkg/middlewares/filter/settings.go b/pkg/middlewares/filter/settings.go new file mode 100644 index 00000000..d6139e2c --- /dev/null +++ b/pkg/middlewares/filter/settings.go @@ -0,0 +1,22 @@ +package filter + +import ( + "errors" + "fmt" +) + +type Settings struct { + Filter Filter +} + +var ( + ErrFilterMustBeSet = errors.New("filter must be set") +) + +func (s *Settings) Validate() (err error) { + if s.Filter == nil { + return fmt.Errorf("%w", ErrFilterMustBeSet) + } + + return nil +}