Skip to content

Commit

Permalink
refactor: Changing error for enum
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Apr 27, 2024
1 parent ac9d7af commit e69a094
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
10 changes: 6 additions & 4 deletions pkg/ketchup/ketchups.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func (s Service) handleCreate(w http.ResponseWriter, r *http.Request) {
return
}

ketchupFrequency, err := model.ParseKetchupFrequency(r.FormValue("frequency"))
rawKetchupFrequency := r.FormValue("frequency")
ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
s.renderer.Error(w, r, nil, httpModel.WrapInvalid(err))
s.renderer.Error(w, r, nil, httpModel.WrapInvalid(fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)))
return
}

Expand Down Expand Up @@ -103,9 +104,10 @@ func (s Service) handleUpdate(w http.ResponseWriter, r *http.Request) {
return
}

ketchupFrequency, err := model.ParseKetchupFrequency(r.FormValue("frequency"))
rawKetchupFrequency := r.FormValue("frequency")
ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
s.renderer.Error(w, r, nil, httpModel.WrapInvalid(err))
s.renderer.Error(w, r, nil, httpModel.WrapInvalid(fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)))
return
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/model/ketchup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package model

import (
"fmt"
"errors"
"strings"

"github.com/ViBiOh/httputils/v4/pkg/hash"
Expand All @@ -16,20 +16,23 @@ const (
Weekly
)

var KetchupFrequencyValues = []string{"None", "Daily", "Weekly"}
var (
ErrUnknownKetchupFrequency = errors.New("unknown ketchup frequency")
ketchupFrequencyValues = []string{"None", "Daily", "Weekly"}
)

func ParseKetchupFrequency(value string) (KetchupFrequency, error) {
for i, short := range KetchupFrequencyValues {
for i, short := range ketchupFrequencyValues {
if strings.EqualFold(short, value) {
return KetchupFrequency(i), nil
}
}

return Daily, fmt.Errorf("invalid value `%s` for ketchup frequency", value)
return Daily, ErrUnknownKetchupFrequency
}

func (r KetchupFrequency) String() string {
return KetchupFrequencyValues[r]
return ketchupFrequencyValues[r]
}

type Ketchup struct {
Expand Down
3 changes: 1 addition & 2 deletions pkg/model/ketchup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"errors"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestParseKetchupFrequency(t *testing.T) {
value: "wrong",
},
Daily,
errors.New("invalid value `wrong` for ketchup frequency"),
ErrUnknownKetchupFrequency,
},
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/model/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
)
Expand All @@ -23,20 +24,23 @@ const (
Pypi
)

var RepositoryKindValues = []string{"github", "helm", "docker", "npm", "pypi"}
var (
ErrUnknownRepositoryKind = errors.New("unknown repository kind")
repositoryKindValues = []string{"github", "helm", "docker", "npm", "pypi"}
)

func ParseRepositoryKind(value string) (RepositoryKind, error) {
for i, short := range RepositoryKindValues {
for i, short := range repositoryKindValues {
if strings.EqualFold(short, value) {
return RepositoryKind(i), nil
}
}

return Github, fmt.Errorf("invalid value `%s` for repository kind", value)
return Github, ErrUnknownRepositoryKind
}

func (r RepositoryKind) String() string {
return RepositoryKindValues[r]
return repositoryKindValues[r]
}

func (r RepositoryKind) MarshalJSON() ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/model/repository_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"errors"
"strings"
"testing"
)
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestParseRepositoryKind(t *testing.T) {
value: "wrong",
},
Github,
errors.New("invalid value `wrong` for repository kind"),
ErrUnknownRepositoryKind,
},
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/store/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func (s Service) List(ctx context.Context, pageSize uint, last string) ([]model.

ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
return err
return fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)
}
item.Frequency = ketchupFrequency

repositoryKind, err := model.ParseRepositoryKind(rawRepositoryKind)
if err != nil {
return err
return fmt.Errorf("parse kind `%s`: %w", rawRepositoryKind, err)
}
item.Repository.Kind = repositoryKind

Expand Down Expand Up @@ -154,7 +154,7 @@ func (s Service) ListByRepositoriesIDAndFrequencies(ctx context.Context, ids []m

ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
return err
return fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)
}
item.Frequency = ketchupFrequency

Expand Down Expand Up @@ -208,13 +208,13 @@ func (s Service) ListOutdated(ctx context.Context, userIds ...model.Identifier)

ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
return err
return fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)
}
item.Frequency = ketchupFrequency

repositoryKind, err := model.ParseRepositoryKind(rawRepositoryKind)
if err != nil {
return err
return fmt.Errorf("parse kind `%s`: %w", rawRepositoryKind, err)
}
item.Repository.Kind = repositoryKind

Expand Down Expand Up @@ -265,7 +265,7 @@ func (s Service) ListSilentForRepositories(ctx context.Context, ids []uint64) ([

ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
return err
return fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)
}
item.Frequency = ketchupFrequency

Expand Down Expand Up @@ -320,13 +320,13 @@ func (s Service) GetByRepository(ctx context.Context, id model.Identifier, patte

ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
return err
return fmt.Errorf("parse frequency `%s`: %w", rawKetchupFrequency, err)
}
item.Frequency = ketchupFrequency

repositoryKind, err := model.ParseRepositoryKind(rawRepositoryKind)
if err != nil {
return err
return fmt.Errorf("parse kind `%s`: %w", rawRepositoryKind, err)
}
item.Repository.Kind = repositoryKind

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/ketchup/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestList(t *testing.T) {
pageSize: 20,
},
nil,
errors.New("invalid value `wrong` for repository kind"),
model.ErrUnknownRepositoryKind,
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s Service) list(ctx context.Context, query string, args ...any) ([]model.R

repositoryKind, err := model.ParseRepositoryKind(rawRepositoryKind)
if err != nil {
return err
return fmt.Errorf("parse kind `%s`: %w", rawRepositoryKind, err)
}
item.Kind = repositoryKind

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestList(t *testing.T) {
},
nil,
0,
errors.New("invalid value `wrong` for repository kind"),
model.ErrUnknownRepositoryKind,
},
}

Expand Down

0 comments on commit e69a094

Please sign in to comment.