Skip to content

Commit

Permalink
Merge pull request #530 from traPtitech/refactor/util
Browse files Browse the repository at this point in the history
Refactor util
  • Loading branch information
motoki317 authored May 13, 2023
2 parents a193681 + a809da0 commit 028d158
Show file tree
Hide file tree
Showing 26 changed files with 65 additions and 154 deletions.
2 changes: 1 addition & 1 deletion pkg/domain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ func GetActiveStaticSites(ctx context.Context, appRepo ApplicationRepository, bu
return nil, err
}

commits := lo.Map(applications, func(app *Application, i int) string { return app.CurrentCommit })
commits := ds.Map(applications, func(app *Application) string { return app.CurrentCommit })
builds, err := buildRepo.GetBuilds(ctx, GetBuildCondition{CommitIn: optional.From(commits), Status: optional.From(BuildStatusSucceeded)})
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/infrastructure/backend/dockerimpl/list_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/friendsofgo/errors"
"github.com/samber/lo"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

func (b *dockerBackend) GetContainer(ctx context.Context, appID string) (*domain.Container, error) {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (b *dockerBackend) ListContainers(ctx context.Context) ([]*domain.Container
return nil, errors.Wrap(err, "failed to fetch containers")
}

result := lo.Map(containers, func(c types.Container, i int) *domain.Container {
result := ds.Map(containers, func(c types.Container) *domain.Container {
return &domain.Container{
ApplicationID: c.Labels[appIDLabel],
State: getContainerState(&c),
Expand Down
2 changes: 1 addition & 1 deletion pkg/infrastructure/backend/k8simpl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *Config) podSchedulingTolerations() []v1.Toleration {
if len(c.Scheduling.Tolerations) == 0 {
return nil
}
return lo.Map(c.Scheduling.Tolerations, func(t *toleration, _ int) v1.Toleration {
return ds.Map(c.Scheduling.Tolerations, func(t *toleration) v1.Toleration {
return v1.Toleration{
Key: t.Key,
Operator: tolerationOperatorMapper.IntoMust(t.Operator),
Expand Down
4 changes: 2 additions & 2 deletions pkg/infrastructure/backend/k8simpl/list_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"

"github.com/friendsofgo/errors"
"github.com/samber/lo"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

func (b *k8sBackend) GetContainer(ctx context.Context, appID string) (*domain.Container, error) {
Expand Down Expand Up @@ -39,7 +39,7 @@ func (b *k8sBackend) ListContainers(ctx context.Context) ([]*domain.Container, e
return nil, errors.Wrap(err, "failed to fetch pods")
}

result := lo.Map(list.Items, func(pod v1.Pod, i int) *domain.Container {
result := ds.Map(list.Items, func(pod v1.Pod) *domain.Container {
return &domain.Container{
ApplicationID: pod.Labels[appIDLabel],
State: getContainerState(pod.Status),
Expand Down
7 changes: 2 additions & 5 deletions pkg/infrastructure/grpc/api_app_build_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (

"github.com/bufbuild/connect-go"
"github.com/friendsofgo/errors"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pbconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

func (s *APIService) GetBuilds(ctx context.Context, req *connect.Request[pb.ApplicationIdRequest]) (*connect.Response[pb.GetBuildsResponse], error) {
Expand All @@ -19,9 +18,7 @@ func (s *APIService) GetBuilds(ctx context.Context, req *connect.Request[pb.Appl
return nil, handleUseCaseError(err)
}
res := connect.NewResponse(&pb.GetBuildsResponse{
Builds: lo.Map(builds, func(build *domain.Build, i int) *pb.Build {
return pbconvert.ToPBBuild(build)
}),
Builds: ds.Map(builds, pbconvert.ToPBBuild),
})
return res, nil
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/infrastructure/grpc/api_app_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"time"

"github.com/bufbuild/connect-go"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pbconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

func (s *APIService) GetEnvVars(ctx context.Context, req *connect.Request[pb.ApplicationIdRequest]) (*connect.Response[pb.ApplicationEnvVars], error) {
Expand All @@ -19,9 +19,7 @@ func (s *APIService) GetEnvVars(ctx context.Context, req *connect.Request[pb.App
return nil, handleUseCaseError(err)
}
res := connect.NewResponse(&pb.ApplicationEnvVars{
Variables: lo.Map(environments, func(env *domain.Environment, i int) *pb.ApplicationEnvVar {
return pbconvert.ToPBEnvironment(env)
}),
Variables: ds.Map(environments, pbconvert.ToPBEnvironment),
})
return res, nil
}
Expand All @@ -47,9 +45,7 @@ func (s *APIService) GetOutput(ctx context.Context, req *connect.Request[pb.GetO
return nil, handleUseCaseError(err)
}
res := connect.NewResponse(&pb.GetOutputResponse{
Outputs: lo.Map(logs, func(l *domain.ContainerLog, i int) *pb.ApplicationOutput {
return pbconvert.ToPBApplicationOutput(l)
}),
Outputs: ds.Map(logs, pbconvert.ToPBApplicationOutput),
})
return res, nil
}
Expand Down
34 changes: 15 additions & 19 deletions pkg/infrastructure/grpc/api_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ func (s *APIService) CreateApplication(ctx context.Context, req *connect.Request
now := time.Now()
config := pbconvert.FromPBApplicationConfig(msg.Config)
app := &domain.Application{
ID: domain.NewID(),
Name: msg.Name,
RepositoryID: msg.RepositoryId,
RefName: msg.RefName,
DeployType: config.BuildConfig.BuildType().DeployType(),
Running: msg.StartOnCreate,
Container: domain.ContainerStateMissing,
CurrentCommit: domain.EmptyCommit,
WantCommit: domain.EmptyCommit,
CreatedAt: now,
UpdatedAt: now,
Config: config,
Websites: lo.Map(msg.Websites, func(website *pb.CreateWebsiteRequest, i int) *domain.Website {
return pbconvert.FromPBCreateWebsiteRequest(website)
}),
ID: domain.NewID(),
Name: msg.Name,
RepositoryID: msg.RepositoryId,
RefName: msg.RefName,
DeployType: config.BuildConfig.BuildType().DeployType(),
Running: msg.StartOnCreate,
Container: domain.ContainerStateMissing,
CurrentCommit: domain.EmptyCommit,
WantCommit: domain.EmptyCommit,
CreatedAt: now,
UpdatedAt: now,
Config: config,
Websites: ds.Map(msg.Websites, pbconvert.FromPBCreateWebsiteRequest),
PortPublications: ds.Map(msg.PortPublications, pbconvert.FromPBPortPublication),
OwnerIDs: []string{user.ID},
}
Expand All @@ -54,9 +52,7 @@ func (s *APIService) GetApplications(ctx context.Context, _ *connect.Request[emp
return nil, handleUseCaseError(err)
}
res := connect.NewResponse(&pb.GetApplicationsResponse{
Applications: lo.Map(applications, func(app *domain.Application, i int) *pb.Application {
return pbconvert.ToPBApplication(app)
}),
Applications: ds.Map(applications, pbconvert.ToPBApplication),
})
return res, nil
}
Expand All @@ -82,7 +78,7 @@ func (s *APIService) UpdateApplication(ctx context.Context, req *connect.Request
websites = append(websites, pbconvert.FromPBCreateWebsiteRequest(createReq))
}
for _, deleteReq := range msg.DeleteWebsites {
websites = lo.Reject(websites, func(w *domain.Website, i int) bool { return w.ID == deleteReq.Id })
websites = lo.Reject(websites, func(w *domain.Website, _ int) bool { return w.ID == deleteReq.Id })
}

err = s.svc.UpdateApplication(ctx, msg.Id, &domain.UpdateApplicationArgs{
Expand Down
6 changes: 2 additions & 4 deletions pkg/infrastructure/grpc/api_repository_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"

"github.com/bufbuild/connect-go"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/domain/web"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pbconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
"github.com/traPtitech/neoshowcase/pkg/util/optional"
)

Expand Down Expand Up @@ -38,9 +38,7 @@ func (s *APIService) GetRepositories(ctx context.Context, _ *connect.Request[emp
return nil, handleUseCaseError(err)
}
res := connect.NewResponse(&pb.GetRepositoriesResponse{
Repositories: lo.Map(repositories, func(repo *domain.Repository, i int) *pb.Repository {
return pbconvert.ToPBRepository(repo)
}),
Repositories: ds.Map(repositories, pbconvert.ToPBRepository),
})
return res, nil
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/infrastructure/grpc/api_system_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/bufbuild/connect-go"
"github.com/samber/lo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -29,9 +28,7 @@ func (s *APIService) GetAvailableDomains(ctx context.Context, _ *connect.Request
return nil, status.Errorf(codes.Internal, "%v", err)
}
res := connect.NewResponse(&pb.AvailableDomains{
Domains: lo.Map(domains, func(ad *domain.AvailableDomain, i int) *pb.AvailableDomain {
return pbconvert.ToPBAvailableDomain(ad)
}),
Domains: ds.Map(domains, pbconvert.ToPBAvailableDomain),
})
return res, nil
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/infrastructure/grpc/api_user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"

"github.com/bufbuild/connect-go"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/domain/web"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pbconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

func (s *APIService) GetMe(ctx context.Context, _ *connect.Request[emptypb.Empty]) (*connect.Response[pb.User], error) {
Expand All @@ -34,7 +33,7 @@ func (s *APIService) GetUserKeys(ctx context.Context, _ *connect.Request[emptypb
return nil, handleUseCaseError(err)
}
res := connect.NewResponse(&pb.GetUserKeysResponse{
Keys: lo.Map(keys, func(key *domain.UserKey, _ int) *pb.UserKey { return pbconvert.ToPBUserKey(key) }),
Keys: ds.Map(keys, pbconvert.ToPBUserKey),
})
return res, nil
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/infrastructure/grpc/pbconvert/application.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pbconvert

import (
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
"github.com/traPtitech/neoshowcase/pkg/util/mapper"
)

Expand Down Expand Up @@ -37,9 +37,7 @@ func ToPBApplication(app *domain.Application) *pb.Application {
CreatedAt: timestamppb.New(app.CreatedAt),
UpdatedAt: timestamppb.New(app.UpdatedAt),
Config: ToPBApplicationConfig(app.Config),
Websites: lo.Map(app.Websites, func(website *domain.Website, i int) *pb.Website {
return ToPBWebsite(website)
}),
OwnerIds: app.OwnerIDs,
Websites: ds.Map(app.Websites, ToPBWebsite),
OwnerIds: app.OwnerIDs,
}
}
5 changes: 2 additions & 3 deletions pkg/infrastructure/repository/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/models"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/repoconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

type applicationRepository struct {
Expand Down Expand Up @@ -69,9 +70,7 @@ func (r *applicationRepository) GetApplications(ctx context.Context, cond domain
if err != nil {
return nil, errors.Wrap(err, "failed to get applications")
}
return lo.Map(applications, func(app *models.Application, i int) *domain.Application {
return repoconvert.ToDomainApplication(app)
}), nil
return ds.Map(applications, repoconvert.ToDomainApplication), nil
}

func (r *applicationRepository) getApplication(ctx context.Context, id string, forUpdate bool, ex boil.ContextExecutor) (*models.Application, error) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/infrastructure/repository/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"fmt"

"github.com/friendsofgo/errors"
"github.com/samber/lo"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/models"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/repoconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
"github.com/traPtitech/neoshowcase/pkg/util/optional"
)

Expand Down Expand Up @@ -54,9 +54,7 @@ func (r *artifactRepository) GetArtifacts(ctx context.Context, cond domain.GetAr
if err != nil {
return nil, errors.Wrap(err, "failed to get artifacts")
}
return lo.Map(artifacts, func(a *models.Artifact, i int) *domain.Artifact {
return repoconvert.ToDomainArtifact(a)
}), nil
return ds.Map(artifacts, repoconvert.ToDomainArtifact), nil
}

func (r *artifactRepository) CreateArtifact(ctx context.Context, artifact *domain.Artifact) error {
Expand Down
6 changes: 2 additions & 4 deletions pkg/infrastructure/repository/available_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"fmt"

"github.com/friendsofgo/errors"
"github.com/samber/lo"
"github.com/volatiletech/sqlboiler/v4/boil"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/models"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/repoconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

type availableDomainRepository struct {
Expand All @@ -29,9 +29,7 @@ func (r *availableDomainRepository) GetAvailableDomains(ctx context.Context) (do
if err != nil {
return nil, errors.Wrap(err, "failed to get available domains")
}
dDomains := lo.Map(domains, func(ad *models.AvailableDomain, i int) *domain.AvailableDomain {
return repoconvert.ToDomainAvailableDomain(ad)
})
dDomains := ds.Map(domains, repoconvert.ToDomainAvailableDomain)
return dDomains, nil
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/infrastructure/repository/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"database/sql"

"github.com/friendsofgo/errors"
"github.com/samber/lo"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/models"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/repoconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
"github.com/traPtitech/neoshowcase/pkg/util/optional"
)

Expand Down Expand Up @@ -59,9 +59,7 @@ func (r *buildRepository) GetBuilds(ctx context.Context, cond domain.GetBuildCon
if err != nil {
return nil, errors.Wrap(err, "failed to get builds")
}
return lo.Map(builds, func(b *models.Build, i int) *domain.Build {
return repoconvert.ToDomainBuild(b)
}), nil
return ds.Map(builds, repoconvert.ToDomainBuild), nil
}

func (r *buildRepository) GetBuild(ctx context.Context, buildID string) (*domain.Build, error) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/infrastructure/repository/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"database/sql"

"github.com/friendsofgo/errors"
"github.com/samber/lo"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/models"
"github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/repoconvert"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

type environmentRepository struct {
Expand Down Expand Up @@ -41,9 +41,7 @@ func (r *environmentRepository) GetEnv(ctx context.Context, cond domain.GetEnvCo
if err != nil {
return nil, err
}
return lo.Map(environments, func(env *models.Environment, i int) *domain.Environment {
return repoconvert.ToDomainEnvironment(env)
}), nil
return ds.Map(environments, repoconvert.ToDomainEnvironment), nil
}

func (r *environmentRepository) SetEnv(ctx context.Context, env *domain.Environment) error {
Expand Down
Loading

0 comments on commit 028d158

Please sign in to comment.