Skip to content

Commit

Permalink
for文をsamber/loで置き換え
Browse files Browse the repository at this point in the history
  • Loading branch information
Hueter57 committed Oct 1, 2024
1 parent 80577a7 commit 44d8317
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
7 changes: 3 additions & 4 deletions router/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,9 @@ func (h Handlers) PostOwner(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

res := make([]uuid.UUID, len(added))
for i, owner := range added {
res[i] = owner.ID
}
res := lo.Map(added, func(owner *model.Owner, index int) uuid.UUID {
return owner.ID
})

return c.JSON(http.StatusOK, res)
}
Expand Down
9 changes: 4 additions & 5 deletions router/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,12 @@ func (h Handlers) PutStatus(c echo.Context) error {
h.Logger.Error("failed to get request targets from repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
var paid bool
for _, target := range targets {
paid := lo.Reduce(targets, func(p bool, target *model.RequestTargetDetail, _ int) bool {
if target.PaidAt != nil {
paid = true
break
return true
}
}
return p
}, false)
if paid {
h.Logger.Info("someone already paid")
return echo.NewHTTPError(http.StatusBadRequest, errors.New("someone already paid"))
Expand Down
15 changes: 7 additions & 8 deletions service/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) {
"https://jomon.trap.jp",
resApp.ID)

amount := 0
for _, target := range resApp.Targets {
amount += target.Amount
}
amount := lo.Reduce(resApp.Targets, func(amo int, target *Target, _ int) int {
return amo + target.Amount
}, 0)

Check warning on line 122 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L120-L122

Added lines #L120 - L122 were not covered by tests
message += fmt.Sprintf("- 支払金額: %d円\n", amount)

if resApp.Group != nil {
Expand Down Expand Up @@ -167,10 +166,10 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) {
resApp.Amount)
}
} else {
targets := make([]string, len(resApps))
for i, resApp := range resApps {
targets[i] = resApp.Target
}
targets := lo.Map(
resApps, func(resApp TransactionRequestApplication, index int) string {
return resApp.Target
})

Check warning on line 172 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L169-L172

Added lines #L169 - L172 were not covered by tests
if resApp.Amount < 0 {
message += fmt.Sprintf(
"- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n",
Expand Down
26 changes: 11 additions & 15 deletions testutil/random/ramdom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package random
import (
"math/rand/v2"
"testing"

"github.com/samber/lo"
)

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Expand Down Expand Up @@ -32,27 +34,21 @@ func Numeric64(t *testing.T, n int64) int64 {

func AlphaNumericSlice(t *testing.T, length int, n int64) []string {
t.Helper()
slice := []string{}
for range length {
slice = append(slice, AlphaNumeric(t, int(n)))
}
return slice
return lo.Times(length, func(index int) string {
return AlphaNumeric(t, int(n))
})

Check warning on line 39 in testutil/random/ramdom.go

View check run for this annotation

Codecov / codecov/patch

testutil/random/ramdom.go#L37-L39

Added lines #L37 - L39 were not covered by tests
}

func NumericSlice(t *testing.T, length int, n int) []int {
t.Helper()
slice := []int{}
for range length {
slice = append(slice, Numeric(t, n))
}
return slice
return lo.Times(length, func(index int) int {
return Numeric(t, n)
})

Check warning on line 46 in testutil/random/ramdom.go

View check run for this annotation

Codecov / codecov/patch

testutil/random/ramdom.go#L44-L46

Added lines #L44 - L46 were not covered by tests
}

func Numeric64Slice(t *testing.T, length int, n int64) []int64 {
t.Helper()
slice := []int64{}
for range length {
slice = append(slice, Numeric64(t, n))
}
return slice
return lo.Times(length, func(index int) int64 {
return Numeric64(t, n)
})

Check warning on line 53 in testutil/random/ramdom.go

View check run for this annotation

Codecov / codecov/patch

testutil/random/ramdom.go#L51-L53

Added lines #L51 - L53 were not covered by tests
}

0 comments on commit 44d8317

Please sign in to comment.