Skip to content

Commit

Permalink
feat: Adding marshal for enum value to mailer
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 17, 2021
1 parent e33d12f commit 1ffcc40
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 31 deletions.
30 changes: 27 additions & 3 deletions pkg/model/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
Expand Down Expand Up @@ -31,9 +33,6 @@ const (
var (
// RepositoryKindValues string values
RepositoryKindValues = []string{"github", "helm", "docker", "npm", "pypi"}

// NoneRepository is an undefined repository
NoneRepository = Repository{}
)

// ParseRepositoryKind parse raw string into a RepositoryKind
Expand All @@ -51,6 +50,31 @@ func (r RepositoryKind) String() string {
return RepositoryKindValues[r]
}

// MarshalJSON marshals the enum as a quoted json string
func (r RepositoryKind) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(r.String())
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}

// UnmarshalJSON unmarshal JSOn
func (r *RepositoryKind) UnmarshalJSON(b []byte) error {
var strValue string
err := json.Unmarshal(b, &strValue)
if err != nil {
return fmt.Errorf("unable to unmarshal event type: %s", err)
}

value, err := ParseRepositoryKind(strValue)
if err != nil {
return fmt.Errorf("unable to parse event type: %s", err)
}

*r = value
return nil
}

// Repository of app
type Repository struct {
Versions map[string]string `json:"versions"`
Expand Down
8 changes: 4 additions & 4 deletions pkg/service/ketchup/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestCreate(t *testing.T) {

switch tc.intention {
case "repository error", "check error", "create error":
mockRepositoryService.EXPECT().GetOrCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.NoneRepository, nil)
mockRepositoryService.EXPECT().GetOrCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.Repository{}, nil)
case "success":
mockRepositoryService.EXPECT().GetOrCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.NewGithubRepository(1, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil)
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestUpdate(t *testing.T) {

switch tc.intention {
case "pattern change error":
mockRepositoryService.EXPECT().GetOrCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.NoneRepository, errors.New("failed"))
mockRepositoryService.EXPECT().GetOrCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.Repository{}, errors.New("failed"))
case "pattern change success":
mockRepositoryService.EXPECT().GetOrCreate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.NewGithubRepository(1, ketchupRepository).AddVersion("latest", "1.0.1"), nil)
}
Expand Down Expand Up @@ -505,7 +505,7 @@ func TestCheck(t *testing.T) {
app{ketchupStore: ketchuptest.New()},
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
old: model.NewKetchup(model.DefaultPattern, "1.0.0", model.Daily, model.NoneRepository),
old: model.NewKetchup(model.DefaultPattern, "1.0.0", model.Daily, model.Repository{}),
new: model.NoneKetchup,
},
nil,
Expand Down Expand Up @@ -535,7 +535,7 @@ func TestCheck(t *testing.T) {
app{ketchupStore: ketchuptest.New()},
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
old: model.NewKetchup(model.DefaultPattern, "1.0.0", model.Daily, model.NoneRepository),
old: model.NewKetchup(model.DefaultPattern, "1.0.0", model.Daily, model.Repository{}),
new: model.NewKetchup(model.DefaultPattern, "", model.Daily, model.NewGithubRepository(1, "")),
},
errors.New("version is required"),
Expand Down
14 changes: 7 additions & 7 deletions pkg/service/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (a App) GetOrCreate(ctx context.Context, kind model.RepositoryKind, name, p

repo, err := a.repositoryStore.GetByName(ctx, kind, sanitizedName, part)
if err != nil {
return model.NoneRepository, httpModel.WrapInternal(err)
return model.Repository{}, httpModel.WrapInternal(err)
}

if repo.ID == 0 {
Expand All @@ -92,30 +92,30 @@ func (a App) GetOrCreate(ctx context.Context, kind model.RepositoryKind, name, p
repo.Versions[pattern] = ""
versions, err := a.LatestVersions(repo)
if err != nil {
return model.NoneRepository, httpModel.WrapInternal(fmt.Errorf("unable to get releases for `%s`: %w", repo.Name, err))
return model.Repository{}, httpModel.WrapInternal(fmt.Errorf("unable to get releases for `%s`: %w", repo.Name, err))
}

version, ok := versions[pattern]
if !ok || version == semver.NoneVersion {
return model.NoneRepository, httpModel.WrapNotFound(fmt.Errorf("no release with pattern `%s` found for repository `%s`", pattern, repo.Name))
return model.Repository{}, httpModel.WrapNotFound(fmt.Errorf("no release with pattern `%s` found for repository `%s`", pattern, repo.Name))
}

repo.Versions[pattern] = version.Name
if err := a.repositoryStore.UpdateVersions(ctx, repo); err != nil {
return model.NoneRepository, httpModel.WrapInternal(fmt.Errorf("unable to update repository versions `%s`: %w", repo.Name, err))
return model.Repository{}, httpModel.WrapInternal(fmt.Errorf("unable to update repository versions `%s`: %w", repo.Name, err))
}

return repo, nil
}

func (a App) create(ctx context.Context, item model.Repository) (model.Repository, error) {
if err := a.check(ctx, model.NoneRepository, item); err != nil {
return model.NoneRepository, httpModel.WrapInvalid(err)
if err := a.check(ctx, model.Repository{}, item); err != nil {
return model.Repository{}, httpModel.WrapInvalid(err)
}

versions, err := a.LatestVersions(item)
if err != nil {
return model.NoneRepository, httpModel.WrapNotFound(fmt.Errorf("unable to get releases for `%s`: %w", item.Name, err))
return model.Repository{}, httpModel.WrapNotFound(fmt.Errorf("unable to get releases for `%s`: %w", item.Name, err))
}

for pattern, version := range versions {
Expand Down
20 changes: 10 additions & 10 deletions pkg/service/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ func TestGetOrCreate(t *testing.T) {
}{
{
"get error",
New(repositorytest.New().SetGetByName(model.NoneRepository, errors.New("failed")), githubtest.New(), nil, nil, nil, nil),
New(repositorytest.New().SetGetByName(model.Repository{}, errors.New("failed")), githubtest.New(), nil, nil, nil, nil),
args{
ctx: context.Background(),
name: "error",
repositoryKind: model.Github,
pattern: model.DefaultPattern,
},
model.NoneRepository,
model.Repository{},
httpModel.ErrInternalError,
},
{
Expand All @@ -198,7 +198,7 @@ func TestGetOrCreate(t *testing.T) {
repositoryKind: model.Github,
pattern: model.DefaultPattern,
},
model.NoneRepository,
model.Repository{},
httpModel.ErrInternalError,
},
{
Expand All @@ -212,7 +212,7 @@ func TestGetOrCreate(t *testing.T) {
repositoryKind: model.Github,
pattern: model.DefaultPattern,
},
model.NoneRepository,
model.Repository{},
httpModel.ErrNotFound,
},
{
Expand All @@ -238,7 +238,7 @@ func TestGetOrCreate(t *testing.T) {
repositoryKind: model.Helm,
pattern: model.DefaultPattern,
},
model.NoneRepository,
model.Repository{},
httpModel.ErrInternalError,
},
{
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestCreate(t *testing.T) {
ctx: context.Background(),
item: model.NewGithubRepository(1, ""),
},
model.NoneRepository,
model.Repository{},
httpModel.ErrInvalid,
},
{
Expand All @@ -326,7 +326,7 @@ func TestCreate(t *testing.T) {
ctx: context.Background(),
item: model.NewGithubRepository(1, "invalid"),
},
model.NoneRepository,
model.Repository{},
httpModel.ErrNotFound,
},
{
Expand Down Expand Up @@ -397,13 +397,13 @@ func TestUpdate(t *testing.T) {
New(repositorytest.New().SetDoAtomic(errAtomicStart), nil, nil, nil, nil, nil),
args{
ctx: context.TODO(),
item: model.NoneRepository,
item: model.Repository{},
},
errAtomicStart,
},
{
"fetch error",
New(repositorytest.New().SetGet(model.NoneRepository, errors.New("failed")), nil, nil, nil, nil, nil),
New(repositorytest.New().SetGet(model.Repository{}, errors.New("failed")), nil, nil, nil, nil, nil),
args{
ctx: context.Background(),
item: model.NewGithubRepository(0, ""),
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestCheck(t *testing.T) {
},
{
"get error",
App{repositoryStore: repositorytest.New().SetGetByName(model.NoneRepository, errors.New("failed"))},
App{repositoryStore: repositorytest.New().SetGetByName(model.Repository{}, errors.New("failed"))},
args{
new: model.NewGithubRepository(1, "error"),
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/store/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (a app) get(ctx context.Context, query string, args ...interface{}) (model.
scanner := func(row *sql.Row) error {
err := row.Scan(&item.ID, &rawRepositoryKind, &item.Name, &item.Part)
if errors.Is(err, sql.ErrNoRows) {
item = model.NoneRepository
item = model.Repository{}
return nil
}

Expand All @@ -97,7 +97,7 @@ func (a app) get(ctx context.Context, query string, args ...interface{}) (model.
}

if err := a.db.Get(ctx, scanner, query, args...); err != nil {
return model.NoneRepository, err
return model.Repository{}, err
}

if item.ID == 0 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/store/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestGet(t *testing.T) {
id: 1,
},
"SELECT id, kind, name, part FROM ketchup.repository WHERE id =",
model.NoneRepository,
model.Repository{},
nil,
},
}
Expand Down Expand Up @@ -313,15 +313,15 @@ func TestGetByName(t *testing.T) {
args{
name: ketchupRepository,
},
model.NoneRepository,
model.Repository{},
nil,
},
{
"invalid kind",
args{
name: ketchupRepository,
},
model.NoneRepository,
model.Repository{},
errors.New("invalid value `wrong` for repository kind"),
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/repository/repository_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func findRepository(repositories []model.Repository, id uint64) model.Repository
}
}

return model.NoneRepository
return model.Repository{}
}

const listRepositoryVersionQuery = `
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/repository/repository_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestUpdateVersions(t *testing.T) {
{
"no version",
args{
o: model.NoneRepository,
o: model.Repository{},
},
nil,
},
Expand Down

0 comments on commit 1ffcc40

Please sign in to comment.