Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samber/lo #774

Closed
H1rono opened this issue Jun 28, 2024 · 8 comments
Closed

samber/lo #774

H1rono opened this issue Jun 28, 2024 · 8 comments
Assignees

Comments

@H1rono
Copy link
Member

H1rono commented Jun 28, 2024

#771 でインストールした
他にも使えるところがあるかも?

@H1rono
Copy link
Member Author

H1rono commented Jun 30, 2024

究極、appendを撲滅できるよな

@H1rono
Copy link
Member Author

H1rono commented Jul 5, 2024

Jomon/router/admin.go

Lines 20 to 23 in ef91d11

res := []*uuid.UUID{}
for _, admin := range admins {
res = append(res, &admin.ID)
}

Jomon/model/tag_impl.go

Lines 19 to 22 in ef91d11

modeltags := []*Tag{}
for _, t := range tags {
modeltags = append(modeltags, ConvertEntTagToModelTag(t))
}

Jomon/model/user_impl.go

Lines 54 to 57 in ef91d11

var modelusers []*User
for _, u := range users {
modelusers = append(modelusers, convertEntUserToModelUser(u))
}

modelgroups := []*Group{}
for _, g := range groups {
modelgroups = append(modelgroups, ConvertEntGroupToModelGroup(g))
}

owners := []*Owner{}
for _, groupowner := range groupowners {
owners = append(owners, &Owner{ID: groupowner.ID})
}

Jomon/model/group_impl.go

Lines 101 to 104 in ef91d11

resowners := []*Owner{}
for _, owner := range ownerIDs {
resowners = append(resowners, &Owner{ID: owner})
}

Jomon/model/group_impl.go

Lines 130 to 133 in ef91d11

modelmembers := []*Member{}
for _, member := range members {
modelmembers = append(modelmembers, &Member{member.ID})
}

Jomon/model/group_impl.go

Lines 148 to 151 in ef91d11

resMembers := []*Member{}
for _, member := range userIDs {
resMembers = append(resMembers, &Member{member})
}

@H1rono
Copy link
Member Author

H1rono commented Jul 5, 2024

Jomon/router/transaction.go

Lines 139 to 147 in ef91d11

tags := []*TagOverview{}
for _, tag := range tx.Tags {
tags = append(tags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/transaction.go

Lines 195 to 203 in ef91d11

tags := []*TagOverview{}
for _, tag := range created.Tags {
tags = append(tags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/transaction.go

Lines 245 to 253 in ef91d11

tags := []*TagOverview{}
for _, tag := range tx.Tags {
tags = append(tags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/transaction.go

Lines 299 to 307 in ef91d11

tags := []*TagOverview{}
for _, tag := range updated.Tags {
tags = append(tags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

↓バグに見える

Jomon/router/request.go

Lines 188 to 195 in ef91d11

for _, tag := range request.Tags {
tags = append(tags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/request.go

Lines 197 to 206 in ef91d11

restargets := []*TargetOverview{}
for _, target := range request.Targets {
restargets = append(restargets, &TargetOverview{
ID: target.ID,
Target: target.Target,
Amount: target.Amount,
PaidAt: target.PaidAt,
CreatedAt: target.CreatedAt,
})
}

Jomon/router/request.go

Lines 259 to 265 in ef91d11

targets := []*model.RequestTarget{}
for _, target := range req.Targets {
targets = append(targets, &model.RequestTarget{
Target: target.Target,
Amount: target.Amount,
})
}

Jomon/router/request.go

Lines 298 to 307 in ef91d11

var reqtargets []*TargetOverview
for _, target := range request.Targets {
reqtargets = append(reqtargets, &TargetOverview{
ID: target.ID,
Target: target.Target,
Amount: target.Amount,
PaidAt: target.PaidAt,
CreatedAt: target.CreatedAt,
})
}

Jomon/router/request.go

Lines 308 to 316 in ef91d11

var restags []*TagOverview
for _, tag := range request.Tags {
restags = append(restags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/request.go

Lines 317 to 324 in ef91d11

var statuses []*StatusResponseOverview
for _, status := range request.Statuses {
statuses = append(statuses, &StatusResponseOverview{
Status: status.Status,
CreatedAt: status.CreatedAt,
CreatedBy: status.CreatedBy,
})
}

Jomon/router/request.go

Lines 371 to 381 in ef91d11

comments := []*CommentDetail{}
for _, modelcomment := range modelcomments {
comment := &CommentDetail{
ID: modelcomment.ID,
User: modelcomment.User,
Comment: modelcomment.Comment,
CreatedAt: modelcomment.CreatedAt,
UpdatedAt: modelcomment.UpdatedAt,
}
comments = append(comments, comment)
}

Jomon/router/request.go

Lines 393 to 402 in ef91d11

reqtargets := []*TargetOverview{}
for _, target := range request.Targets {
reqtargets = append(reqtargets, &TargetOverview{
ID: target.ID,
Target: target.Target,
Amount: target.Amount,
PaidAt: target.PaidAt,
CreatedAt: target.CreatedAt,
})
}

Jomon/router/request.go

Lines 403 to 411 in ef91d11

restags := []*TagOverview{}
for _, tag := range request.Tags {
restags = append(restags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/request.go

Lines 412 to 419 in ef91d11

var resstatuses []*StatusResponseOverview
for _, status := range request.Statuses {
resstatuses = append(resstatuses, &StatusResponseOverview{
CreatedBy: status.CreatedBy,
Status: status.Status,
CreatedAt: status.CreatedAt,
})
}

Jomon/router/request.go

Lines 468 to 474 in ef91d11

targets := []*model.RequestTarget{}
for _, target := range req.Targets {
targets = append(targets, &model.RequestTarget{
Target: target.Target,
Amount: target.Amount,
})
}

Jomon/router/request.go

Lines 501 to 511 in ef91d11

var comments []*CommentDetail
for _, modelcomment := range modelcomments {
comment := &CommentDetail{
ID: modelcomment.ID,
User: modelcomment.User,
Comment: modelcomment.Comment,
CreatedAt: modelcomment.CreatedAt,
UpdatedAt: modelcomment.UpdatedAt,
}
comments = append(comments, comment)
}

Jomon/router/request.go

Lines 524 to 532 in ef91d11

var restags []*TagOverview
for _, tag := range request.Tags {
restags = append(restags, &TagOverview{
ID: tag.ID,
Name: tag.Name,
CreatedAt: tag.CreatedAt,
UpdatedAt: tag.UpdatedAt,
})
}

Jomon/router/request.go

Lines 533 to 542 in ef91d11

var restargets []*TargetOverview
for _, target := range request.Targets {
restargets = append(restargets, &TargetOverview{
ID: target.ID,
Target: target.Target,
Amount: target.Amount,
PaidAt: target.PaidAt,
CreatedAt: target.CreatedAt,
})
}

Jomon/router/request.go

Lines 543 to 550 in ef91d11

var resstatuses []*StatusResponseOverview
for _, status := range request.Statuses {
resstatuses = append(resstatuses, &StatusResponseOverview{
CreatedBy: status.CreatedBy,
Status: status.Status,
CreatedAt: status.CreatedAt,
})
}

@H1rono
Copy link
Member Author

H1rono commented Jul 5, 2024

多すぎ...

@H1rono
Copy link
Member Author

H1rono commented Jul 23, 2024

Jomon/router/request.go

Lines 707 to 717 in 1db6bd3

var paid bool
for _, target := range targets {
if target.PaidAt != nil {
paid = true
break
}
}
if paid {
h.Logger.Info("someone already paid")
return echo.NewHTTPError(http.StatusBadRequest, errors.New("someone already paid"))
}

ここlo.Reduce使える

@Hueter57
Copy link
Contributor

resが一度resesを経由しているのは何か意味あるんですかね

reses := []*Transaction{}
for _, tx := range txs {
tag := []*TagOverview{}
for _, modelTag := range tx.Tags {
tag = append(tag, &TagOverview{
ID: modelTag.ID,
Name: modelTag.Name,
CreatedAt: modelTag.CreatedAt,
UpdatedAt: modelTag.UpdatedAt,
})
}
group := &GroupOverview{
ID: tx.Group.ID,
Name: tx.Group.Name,
Description: tx.Group.Description,
Budget: tx.Group.Budget,
CreatedAt: tx.Group.CreatedAt,
UpdatedAt: tx.Group.UpdatedAt,
}
resOverview = Transaction{
ID: tx.ID,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Group: group,
CreatedAt: tx.CreatedAt,
UpdatedAt: tx.UpdatedAt,
}
reses = append(reses, &resOverview)
}
res := reses

ここだとresOverviewを経由してます
resOverview := []*Transaction{}

やってることが変わらないので一旦経由しているもの消して書いてます

@H1rono
Copy link
Member Author

H1rono commented Jul 25, 2024

できますね、やっちゃってください
途中でbreakなりreturnなりが入っていない限りはlo.Mapに置き換えてOK
可能であればPRでは置き換えなかったfor文もできれば列挙して欲しいです

@H1rono
Copy link
Member Author

H1rono commented Oct 2, 2024

closed by #789

@H1rono H1rono closed this as completed Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants