Skip to content

Commit

Permalink
ECOM-12329 fix: lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cvejk committed Aug 19, 2024
1 parent 64f5fc4 commit 7f5dd85
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 65 deletions.
10 changes: 4 additions & 6 deletions domain/redirectdefinition/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func NewAPI(
updateSignal *redirectnats.UpdateSignal,
opts ...Option,
) (*API, error) {

inst := &API{
l: l,
repo: repo,
Expand Down Expand Up @@ -79,20 +78,19 @@ func NewAPI(
// ~ Public methods
// ------------------------------------------------------------------------------------------------

func (a *API) CreateRedirects(ctx context.Context, cmd redirectcommand.CreateRedirects) (err error) {
func (a *API) CreateRedirects(ctx context.Context, cmd redirectcommand.CreateRedirects) error {
return a.cmd.CreateRedirects(ctx, a.l, cmd)
}

func (a *API) CreateRedirect(ctx context.Context, cmd redirectcommand.CreateRedirect) (err error) {
func (a *API) CreateRedirect(ctx context.Context, cmd redirectcommand.CreateRedirect) error {
return a.cmd.CreateRedirect(ctx, a.l, cmd)

}

func (a *API) UpdateRedirect(ctx context.Context, cmd redirectcommand.UpdateRedirect) (err error) {
func (a *API) UpdateRedirect(ctx context.Context, cmd redirectcommand.UpdateRedirect) error {
return a.cmd.UpdateRedirect(ctx, a.l, cmd)
}

func (a *API) DeleteRedirect(ctx context.Context, cmd redirectcommand.DeleteRedirect) (err error) {
func (a *API) DeleteRedirect(ctx context.Context, cmd redirectcommand.DeleteRedirect) error {
return a.cmd.DeleteRedirect(ctx, a.l, cmd)
}

Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/command/createredirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (

// CreateRedirectHandler ...
func CreateRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) CreateRedirectHandlerFn {
return func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error {
return func(ctx context.Context, _ *zap.Logger, cmd CreateRedirect) error {
return repo.Insert(ctx, cmd.RedirectDefinition)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func softDeleteStrategy(
idsToDelete []redirectstore.EntityID,
newRedirects []*redirectstore.RedirectDefinition,
currentDefinitions map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition,
) (redirectsToUpsert []*redirectstore.RedirectDefinition) {
) []*redirectstore.RedirectDefinition {
additionalRedirects := []*redirectstore.RedirectDefinition{}
for _, id := range idsToDelete {
for _, def := range newRedirects {
Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/command/deleteredirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (

// DeleteRedirectHandler ...
func DeleteRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) DeleteRedirectHandlerFn {
return func(ctx context.Context, l *zap.Logger, cmd DeleteRedirect) error {
return func(ctx context.Context, _ *zap.Logger, cmd DeleteRedirect) error {
return repo.Delete(ctx, cmd.ID)
}
}
Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/command/updateredirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (

// UpdateRedirectHandler ...
func UpdateRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) UpdateRedirectHandlerFn {
return func(ctx context.Context, l *zap.Logger, cmd UpdateRedirect) error {
return func(ctx context.Context, _ *zap.Logger, cmd UpdateRedirect) error {
return repo.Update(ctx, cmd.RedirectDefinition)
}
}
Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/query/getredirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (

// GetRedirectsHandler ...
func GetRedirectsHandler(repo redirectrepository.RedirectsDefinitionRepository) GetRedirectsHandlerFn {
return func(ctx context.Context, l *zap.Logger) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return func(ctx context.Context, _ *zap.Logger) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return repo.FindAll(ctx, true)
}
}
Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/query/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type (

// SearchHandler ...
func SearchHandler(repo redirectrepository.RedirectsDefinitionRepository) SearchHandlerFn {
return func(ctx context.Context, l *zap.Logger, qry Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return func(ctx context.Context, _ *zap.Logger, qry Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return repo.FindMany(ctx, string(qry.Source), string(qry.Dimension), qry.OnlyActive)
}
}
Expand Down
6 changes: 2 additions & 4 deletions domain/redirectdefinition/repository/redirectdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewBaseRedirectsDefinitionRepository(l *zap.Logger, persistor *keelmongo.Pe
),
)

_, err = collection.Col().Indexes().DropOne(context.TODO(), "source_1")
_, _ = collection.Col().Indexes().DropOne(context.TODO(), "source_1")

if cErr != nil {
return nil, cErr
Expand Down Expand Up @@ -123,7 +123,7 @@ func (rs BaseRedirectsDefinitionRepository) FindAll(ctx context.Context, onlyAct

func (rs BaseRedirectsDefinitionRepository) Insert(ctx context.Context, def *redirectstore.RedirectDefinition) error {
if def.ID == "" {
def.ID = redirectstore.EntityID(redirectstore.NewEntityID())
def.ID = redirectstore.NewEntityID()
}
_, err := rs.collection.Col().InsertOne(ctx, def)
return err
Expand All @@ -135,12 +135,10 @@ func (rs BaseRedirectsDefinitionRepository) Update(ctx context.Context, def *red

_, err := rs.collection.Col().UpdateOne(ctx, filter, update)
return err

}

// maybe will be needed for migrating manual redirections?
func (rs BaseRedirectsDefinitionRepository) UpsertMany(ctx context.Context, defs []*redirectstore.RedirectDefinition) error {

var operations []mongo.WriteModel

Check failure on line 142 in domain/redirectdefinition/repository/redirectdefinition.go

View workflow job for this annotation

GitHub Actions / test

Consider pre-allocating `operations` (prealloc)

for _, def := range defs {
Expand Down
1 change: 0 additions & 1 deletion domain/redirectdefinition/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func TestGetAllRedirects(t *testing.T) {
redirectDefinitions, err := repo.FindAll(context.Background(), false)
assert.Equal(t, 1, len(redirectDefinitions))
assert.NoError(t, err)

}
func TestGenerateAutoRedirects(t *testing.T) {
l := zap.L()
Expand Down
8 changes: 4 additions & 4 deletions domain/redirectdefinition/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func NewService(l *zap.Logger, api *API, options ...ServiceOption) *Service {
func (rs *Service) CreateRedirectsFromContentserverexport(
_ http.ResponseWriter,
r *http.Request,
old,
new map[string]*content.RepoNode,
oldState,
newState map[string]*content.RepoNode,
) error {
rs.l.Info("CreateRedirectsFromContentserverexport called ")
if !rs.enableCreationOfAutomaticRedirects() {
Expand All @@ -47,8 +47,8 @@ func (rs *Service) CreateRedirectsFromContentserverexport(
}
return rs.api.CreateRedirects(r.Context(),
redirectcommand.CreateRedirects{
OldState: old,
NewState: new,
OldState: oldState,
NewState: newState,
})
}

Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ type AdminService interface {
// the service is responsible for the internal endpoints
// will not be exposed only to other backend services
type InternalService interface {
CreateRedirectsFromContentserverexport(w http.ResponseWriter, r *http.Request, old, new map[string]*content.RepoNode) error
CreateRedirectsFromContentserverexport(w http.ResponseWriter, r *http.Request, oldState, newState map[string]*content.RepoNode) error
GetRedirects(w http.ResponseWriter, r *http.Request) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// AutoCreateRedirectDefinitions generates automatic redirects based on the difference between the old and new content tree.
// find new.ID to old.ID and check if the URI is different, if it is different, create a redirect
func AutoCreateRedirectDefinitions(
l *zap.Logger,
_ *zap.Logger,
oldMap, newMap map[string]*content.RepoNode,
dimension redirectstore.Dimension,
) ([]*redirectstore.RedirectDefinition, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package redirectdefinitionutils
package redirectdefinitionutils_test

import (
_ "embed"
Expand All @@ -7,6 +7,7 @@ import (
"testing"

"github.com/foomo/contentserver/content"
rdutils "github.com/foomo/redirects/domain/redirectdefinition/utils"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
Expand All @@ -30,10 +31,10 @@ func Test_AutoCreateRedirectDefinitionsParse(t *testing.T) {
if err != nil {
fmt.Println(err)
}
redirects, err := AutoCreateRedirectDefinitions(
redirects, err := rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(p["de"], make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(pChanged["de"], make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(p["de"], make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(pChanged["de"], make(map[string]*content.RepoNode)),
"HMD-de",
)
assert.NoError(t, err)
Expand Down Expand Up @@ -131,9 +132,9 @@ func Test_AutoCreateRedirectDefinitionsCoverAll(t *testing.T) {
},
},
}
redirects, err := AutoCreateRedirectDefinitions(zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
redirects, err := rdutils.AutoCreateRedirectDefinitions(zap.L(),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
assert.NoError(t, err)
Expand Down Expand Up @@ -199,10 +200,10 @@ func Test_AutoCreateRedirectDefinitions(t *testing.T) {
},
},
}
redirects, err := AutoCreateRedirectDefinitions(
redirects, err := rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
if err != nil {
Expand Down Expand Up @@ -291,10 +292,10 @@ func Test_AutoCreateRedirectDefinitionsExg1(t *testing.T) {
},
},
}
redirects, err := AutoCreateRedirectDefinitions(
redirects, err := rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
if err != nil {
Expand Down Expand Up @@ -383,10 +384,10 @@ func Test_AutoCreateRedirectDefinitionsExg2(t *testing.T) {
},
},
}
redirects, err := AutoCreateRedirectDefinitions(
redirects, err := rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
if err != nil {
Expand Down Expand Up @@ -475,10 +476,10 @@ func Test_AutoCreateRedirectDefinitionsExg3(t *testing.T) {
},
},
}
redirects, err := AutoCreateRedirectDefinitions(
redirects, err := rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
if err != nil {
Expand All @@ -491,10 +492,10 @@ func Test_AutoCreateRedirectDefinitionsExg3(t *testing.T) {
func Test_AutoCreateRedirectDefinitionsEmptyAndNilArgs(t *testing.T) {
oldNodes := &content.RepoNode{}
newNodes := &content.RepoNode{}
redirects, err := AutoCreateRedirectDefinitions(
redirects, err := rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
if err != nil {
Expand All @@ -504,10 +505,10 @@ func Test_AutoCreateRedirectDefinitionsEmptyAndNilArgs(t *testing.T) {
assert.Equal(t, len(redirects), 0)
oldNodes = nil
newNodes = nil
redirects, err = AutoCreateRedirectDefinitions(
redirects, err = rdutils.AutoCreateRedirectDefinitions(
zap.L(),
CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)),
rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)),
"HMD-de",
)
assert.Error(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (
// * Making list for deleting for definitions with empty target id
// * If target of one is source to another one, consolidate those into one definition to prevent multiple redirections
func ConsolidateRedirectDefinitions(
l *zap.Logger,
new []*redirectstore.RedirectDefinition,
_ *zap.Logger,
newDefinitions []*redirectstore.RedirectDefinition,
current redirectstore.RedirectDefinitions,
newNodeMap map[string]*content.RepoNode,
) ([]*redirectstore.RedirectDefinition, []redirectstore.EntityID) {

upsertRedirectDefinitions := []*redirectstore.RedirectDefinition{}
deletedIDs := []redirectstore.EntityID{}

Expand All @@ -33,7 +32,7 @@ func ConsolidateRedirectDefinitions(
}

// iterate over the incoming redirects and add the new redirects to the list that should be upserted
for _, redirectDefinition := range new {
for _, redirectDefinition := range newDefinitions {
upsertRedirectDefinitions = append(upsertRedirectDefinitions, redirectDefinition)
// check if the ID of the new redirect is already in the current list of redirects
currentDefinitions, ok := currentRedirectsByID[redirectDefinition.ContentID]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package redirectdefinitionutils
package redirectdefinitionutils_test

import (
"testing"

"github.com/foomo/contentserver/content"
redirectstore "github.com/foomo/redirects/domain/redirectdefinition/store"
rdutils "github.com/foomo/redirects/domain/redirectdefinition/utils"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
Expand All @@ -29,7 +30,7 @@ func Test_ConsolidateRedirectDefinitions(t *testing.T) {

deletedExpected := []redirectstore.RedirectSource{}

updatedDefs, deletedSources := ConsolidateRedirectDefinitions(
updatedDefs, deletedSources := rdutils.ConsolidateRedirectDefinitions(
zap.L(),
newRedirects,
oldRedirects,
Expand All @@ -38,7 +39,7 @@ func Test_ConsolidateRedirectDefinitions(t *testing.T) {
assert.Equal(t, len(updatedExpected), len(updatedDefs))
assert.Equal(t, len(deletedExpected), len(deletedSources))

//make sure that consolidated definitions exist in expected
// make sure that consolidated definitions exist in expected
for source, definition := range updatedDefs {
assert.NotNil(t, updatedDefs[source])
assert.Equal(t, definition, updatedDefs[source])
Expand Down
4 changes: 2 additions & 2 deletions pkg/middleware/redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (

keellog "github.com/foomo/keel/log"
"github.com/foomo/keel/net/http/middleware"
"github.com/foomo/redirects/pkg/provider"
redirectprovider "github.com/foomo/redirects/pkg/provider"
"go.uber.org/zap"
)

// Redirects middleware
func Redirects(provider redirectprovider.RedirectsProviderInterface) middleware.Middleware {
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
return func(l *zap.Logger, _ string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// only get request will ever be in need of redirects
if r.Method == http.MethodGet {
Expand Down
8 changes: 4 additions & 4 deletions pkg/nats/updatesignal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type UpdateSignal struct {
func NewUpdateSignalSubscribeChannel(
ctx context.Context,
l *zap.Logger,
natsUri,
natsURI,
clientID,
topic string,
) (chan *nats.Msg, error) {
updateSignal, err := NewUpdateSignal(ctx, l, natsUri, clientID, topic)
updateSignal, err := NewUpdateSignal(ctx, l, natsURI, clientID, topic)
if err != nil {
return nil, err
}
Expand All @@ -36,14 +36,14 @@ func NewUpdateSignalSubscribeChannel(
return channel, nil
}

func NewUpdateSignal(ctx context.Context, l *zap.Logger, natsUri, clientID, topic string) (*UpdateSignal, error) {
func NewUpdateSignal(ctx context.Context, l *zap.Logger, natsURI, clientID, topic string) (*UpdateSignal, error) {
var err error
c := &UpdateSignal{
topic: topic,
l: l,
messages: make(chan *nats.Msg),
}
c.connection, err = nats.Connect(natsUri, DefaultConnectOptions(clientID)...)
c.connection, err = nats.Connect(natsURI, DefaultConnectOptions(clientID)...)
if err != nil {
keellog.WithError(c.l, err).Error("error when connecting to nats")
return nil, err
Expand Down
Loading

0 comments on commit 7f5dd85

Please sign in to comment.