You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2023/11/09 03:37:45 UTC
(incubator-answer) branch main updated: refactor: remove redundant `len` and `nil` check
This is an automated email from the ASF dual-hosted git repository.
linkinstar pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
The following commit(s) were added to refs/heads/main by this push:
new b6f0c708 refactor: remove redundant `len` and `nil` check
b6f0c708 is described below
commit b6f0c70874a526067c30f0ce99eac669cb226822
Author: Eng Zer Jun <en...@gmail.com>
AuthorDate: Sat Nov 4 19:06:18 2023 +0800
refactor: remove redundant `len` and `nil` check
From the Go specification [1]:
"1. For a nil slice, the number of iterations is 0."
`len` returns 0 if the slice [2]. Therefore, checking `len(v) > 0`
around a loop is unnecessary.
[1]: https://go.dev/ref/spec#For_range
[2]: https://pkg.go.dev/builtin#len
Signed-off-by: Eng Zer Jun <en...@gmail.com>
---
internal/base/server/http_funcmap.go | 16 ++++----
internal/repo/search_common/search_repo.go | 66 ++++++++++++++----------------
2 files changed, 37 insertions(+), 45 deletions(-)
diff --git a/internal/base/server/http_funcmap.go b/internal/base/server/http_funcmap.go
index a64e49ea..0f99dca0 100644
--- a/internal/base/server/http_funcmap.go
+++ b/internal/base/server/http_funcmap.go
@@ -143,15 +143,13 @@ var funcMap = template.FuncMap{
func FormatLinkNofollow(html string) string {
var hrefRegexp = regexp.MustCompile("(?m)<a.*?[^<]>.*?</a>")
match := hrefRegexp.FindAllString(html, -1)
- if match != nil {
- for _, v := range match {
- hasNofollow := strings.Contains(v, "rel=\"nofollow\"")
- hasSiteUrl := strings.Contains(v, controller.SiteUrl)
- if !hasSiteUrl {
- if !hasNofollow {
- nofollowUrl := strings.Replace(v, "<a", "<a rel=\"nofollow\"", 1)
- html = strings.Replace(html, v, nofollowUrl, 1)
- }
+ for _, v := range match {
+ hasNofollow := strings.Contains(v, "rel=\"nofollow\"")
+ hasSiteUrl := strings.Contains(v, controller.SiteUrl)
+ if !hasSiteUrl {
+ if !hasNofollow {
+ nofollowUrl := strings.Replace(v, "<a", "<a rel=\"nofollow\"", 1)
+ html = strings.Replace(html, v, nofollowUrl, 1)
}
}
}
diff --git a/internal/repo/search_common/search_repo.go b/internal/repo/search_common/search_repo.go
index 51495a46..67996164 100644
--- a/internal/repo/search_common/search_repo.go
+++ b/internal/repo/search_common/search_repo.go
@@ -146,22 +146,20 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
ub.Where(likeConA)
// check tag
- if len(tagIDs) > 0 {
- for ti, tagID := range tagIDs {
- ast := "tag_rel" + strconv.Itoa(ti)
- b.Join("INNER", "tag_rel as "+ast, "question.id = "+ast+".object_id").
- And(builder.Eq{
- ast + ".tag_id": tagID,
- ast + ".status": entity.TagRelStatusAvailable,
- })
- ub.Join("INNER", "tag_rel as "+ast, "question_id = "+ast+".object_id").
- And(builder.Eq{
- ast + ".tag_id": tagID,
- ast + ".status": entity.TagRelStatusAvailable,
- })
- argsQ = append(argsQ, entity.TagRelStatusAvailable, tagID)
- argsA = append(argsA, entity.TagRelStatusAvailable, tagID)
- }
+ for ti, tagID := range tagIDs {
+ ast := "tag_rel" + strconv.Itoa(ti)
+ b.Join("INNER", "tag_rel as "+ast, "question.id = "+ast+".object_id").
+ And(builder.Eq{
+ ast + ".tag_id": tagID,
+ ast + ".status": entity.TagRelStatusAvailable,
+ })
+ ub.Join("INNER", "tag_rel as "+ast, "question_id = "+ast+".object_id").
+ And(builder.Eq{
+ ast + ".tag_id": tagID,
+ ast + ".status": entity.TagRelStatusAvailable,
+ })
+ argsQ = append(argsQ, entity.TagRelStatusAvailable, tagID)
+ argsA = append(argsA, entity.TagRelStatusAvailable, tagID)
}
// check user
@@ -265,16 +263,14 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
b.Where(likeConQ)
// check tag
- if len(tagIDs) > 0 {
- for ti, tagID := range tagIDs {
- ast := "tag_rel" + strconv.Itoa(ti)
- b.Join("INNER", "tag_rel as "+ast, "question.id = "+ast+".object_id").
- And(builder.Eq{
- ast + ".tag_id": tagID,
- ast + ".status": entity.TagRelStatusAvailable,
- })
- args = append(args, entity.TagRelStatusAvailable, tagID)
- }
+ for ti, tagID := range tagIDs {
+ ast := "tag_rel" + strconv.Itoa(ti)
+ b.Join("INNER", "tag_rel as "+ast, "question.id = "+ast+".object_id").
+ And(builder.Eq{
+ ast + ".tag_id": tagID,
+ ast + ".status": entity.TagRelStatusAvailable,
+ })
+ args = append(args, entity.TagRelStatusAvailable, tagID)
}
// check need filter has not accepted
@@ -376,16 +372,14 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs
b.Where(likeConA)
// check tag
- if len(tagIDs) > 0 {
- for ti, tagID := range tagIDs {
- ast := "tag_rel" + strconv.Itoa(ti)
- b.Join("INNER", "tag_rel as "+ast, "question_id = "+ast+".object_id").
- And(builder.Eq{
- ast + ".tag_id": tagID,
- ast + ".status": entity.TagRelStatusAvailable,
- })
- args = append(args, entity.TagRelStatusAvailable, tagID)
- }
+ for ti, tagID := range tagIDs {
+ ast := "tag_rel" + strconv.Itoa(ti)
+ b.Join("INNER", "tag_rel as "+ast, "question_id = "+ast+".object_id").
+ And(builder.Eq{
+ ast + ".tag_id": tagID,
+ ast + ".status": entity.TagRelStatusAvailable,
+ })
+ args = append(args, entity.TagRelStatusAvailable, tagID)
}
// check limit accepted