Skip to content

Commit

Permalink
refactor: extract adapter interfaces into subpackage and assert them
Browse files Browse the repository at this point in the history
The optional interfaces that can be implemented by an adapter are now
located in the adapter/connector package. This allows the adapter
implementations to import and assert that those interfaces are
implemented.
  • Loading branch information
MichaelEischer committed Jan 13, 2024
1 parent 51f456d commit cb38329
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
20 changes: 0 additions & 20 deletions internal/adapter/adapter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package adapter

import (
"context"

"github.com/charmbracelet/log"
"github.com/inovex/CalendarSync/internal/auth"

"github.com/inovex/CalendarSync/internal/config"
)

Expand All @@ -17,21 +12,6 @@ const (
OutlookHttpCalendarType Type = "outlook_http"
)

// LogSetter can be implemented by a struct to allows injection of a logger instance
type LogSetter interface {
SetLogger(logger *log.Logger)
}

// Configurable is an interface which defines how arbitrary configuration data can be passed
// to a struct which implements this interface. Clients should be configurable.
type Configurable interface {
Initialize(ctx context.Context, config map[string]interface{}) error
}

type OAuth2Adapter interface {
SetupOauth2(credentials auth.Credentials, storage auth.Storage, bindPort uint) error
}

// ConfigReader provides an interface for adapters to load their own configuration map.
// It's the adapter's responsibility to validate that the map is valid.
type ConfigReader interface {
Expand Down
24 changes: 24 additions & 0 deletions internal/adapter/connector/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package connector

import (
"context"

"github.com/charmbracelet/log"

"github.com/inovex/CalendarSync/internal/auth"
)

// LogSetter can be implemented by a struct to allows injection of a logger instance
type LogSetter interface {
SetLogger(logger *log.Logger)
}

// Configurable is an interface which defines how arbitrary configuration data can be passed
// to a struct which implements this interface. Clients should be configurable.
type Configurable interface {
Initialize(ctx context.Context, config map[string]interface{}) error
}

type OAuth2Adapter interface {
SetupOauth2(credentials auth.Credentials, storage auth.Storage, bindPort uint) error
}
6 changes: 6 additions & 0 deletions internal/adapter/google/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/inovex/CalendarSync/internal/adapter/connector"
"github.com/inovex/CalendarSync/internal/models"
"github.com/pkg/browser"

Expand Down Expand Up @@ -46,6 +47,11 @@ type CalendarAPI struct {
storage auth.Storage
}

// Assert that the expected interfaces are implemented
var _ connector.Configurable = &CalendarAPI{}
var _ connector.LogSetter = &CalendarAPI{}
var _ connector.OAuth2Adapter = &CalendarAPI{}

func (c *CalendarAPI) SetupOauth2(credentials auth.Credentials, storage auth.Storage, bindPort uint) error {
// Google Adapter does not need the tenantId
switch {
Expand Down
6 changes: 6 additions & 0 deletions internal/adapter/outlook_http/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/browser"
"golang.org/x/oauth2"

"github.com/inovex/CalendarSync/internal/adapter/connector"
"github.com/inovex/CalendarSync/internal/auth"
"github.com/inovex/CalendarSync/internal/models"
)
Expand Down Expand Up @@ -43,6 +44,11 @@ type CalendarAPI struct {
storage auth.Storage
}

// Assert that the expected interfaces are implemented
var _ connector.Configurable = &CalendarAPI{}
var _ connector.LogSetter = &CalendarAPI{}
var _ connector.OAuth2Adapter = &CalendarAPI{}

func (c *CalendarAPI) SetupOauth2(credentials auth.Credentials, storage auth.Storage, bindPort uint) error {
// Outlook Adapter does not need the clientKey
switch {
Expand Down
7 changes: 4 additions & 3 deletions internal/adapter/sink_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/charmbracelet/log"

"github.com/inovex/CalendarSync/internal/adapter/connector"
"github.com/inovex/CalendarSync/internal/adapter/google"
outlook "github.com/inovex/CalendarSync/internal/adapter/outlook_http"
"github.com/inovex/CalendarSync/internal/sync"
Expand Down Expand Up @@ -40,11 +41,11 @@ func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, config ConfigR
return nil, err
}

if c, ok := client.(LogSetter); ok {
if c, ok := client.(connector.LogSetter); ok {
c.SetLogger(logger)
}

if c, ok := client.(OAuth2Adapter); ok {
if c, ok := client.(connector.OAuth2Adapter); ok {
if err := c.SetupOauth2(
auth.Credentials{
Client: auth.Client{
Expand All @@ -64,7 +65,7 @@ func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, config ConfigR
}

// configure adapter client if possible
if c, ok := client.(Configurable); ok {
if c, ok := client.(connector.Configurable); ok {
if err := c.Initialize(ctx, config.Adapter().Config); err != nil {
return nil, fmt.Errorf("unable to Initialize adapter %s: %w", config.Adapter().Type, err)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/adapter/source_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/inovex/CalendarSync/internal/auth"
"github.com/inovex/CalendarSync/internal/models"

"github.com/inovex/CalendarSync/internal/adapter/connector"
outlook "github.com/inovex/CalendarSync/internal/adapter/outlook_http"

"github.com/inovex/CalendarSync/internal/adapter/google"
Expand Down Expand Up @@ -44,11 +45,11 @@ func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, config Confi
return nil, err
}

if c, ok := client.(LogSetter); ok {
if c, ok := client.(connector.LogSetter); ok {
c.SetLogger(logger)
}

if c, ok := client.(OAuth2Adapter); ok {
if c, ok := client.(connector.OAuth2Adapter); ok {
if err := c.SetupOauth2(
auth.Credentials{
Client: auth.Client{
Expand All @@ -68,7 +69,7 @@ func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, config Confi
}

// configure adapter client if possible
if c, ok := client.(Configurable); ok {
if c, ok := client.(connector.Configurable); ok {
if err := c.Initialize(ctx, config.Adapter().Config); err != nil {
return nil, fmt.Errorf("unable to initialize adapter %s: %w", config.Adapter().Type, err)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/adapter/zep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/inovex/CalendarSync/internal/adapter/connector"
"github.com/inovex/CalendarSync/internal/models"

"github.com/charmbracelet/log"
Expand Down Expand Up @@ -36,6 +37,9 @@ type CalendarAPI struct {
homeSet string
}

// Assert that the expected interfaces are implemented
var _ connector.Configurable = &CalendarAPI{}

func (zep *CalendarAPI) GetCalendarID() string {
return zep.generateCalendarID()
}
Expand Down

0 comments on commit cb38329

Please sign in to comment.