浏览代码

refactor: replace `Sprintf` with string concatenation

There is no need to have go parse the whole format string then introspect the
arguments when we an simply use string concatenation.
Julien Voisin 11 月之前
父节点
当前提交
b3fce752d2
共有 2 个文件被更改,包括 7 次插入20 次删除
  1. 1 2
      internal/http/response/builder.go
  2. 6 18
      internal/storage/entry_query_builder.go

+ 1 - 2
internal/http/response/builder.go

@@ -6,7 +6,6 @@ package response // import "miniflux.app/v2/internal/http/response"
 import (
 	"compress/flate"
 	"compress/gzip"
-	"fmt"
 	"io"
 	"log/slog"
 	"net/http"
@@ -48,7 +47,7 @@ func (b *Builder) WithBody(body any) *Builder {
 
 // WithAttachment forces the document to be downloaded by the web browser.
 func (b *Builder) WithAttachment(filename string) *Builder {
-	b.headers["Content-Disposition"] = fmt.Sprintf("attachment; filename=%s", filename)
+	b.headers["Content-Disposition"] = "attachment; filename=" + filename
 	return b
 }
 

+ 6 - 18
internal/storage/entry_query_builder.go

@@ -191,7 +191,7 @@ func (e *EntryQueryBuilder) WithShareCodeNotEmpty() *EntryQueryBuilder {
 
 // WithSorting add a sort expression.
 func (e *EntryQueryBuilder) WithSorting(column, direction string) *EntryQueryBuilder {
-	e.sortExpressions = append(e.sortExpressions, fmt.Sprintf("%s %s", column, direction))
+	e.sortExpressions = append(e.sortExpressions, column+" "+direction)
 	return e
 }
 
@@ -224,11 +224,9 @@ func (e *EntryQueryBuilder) CountEntries() (count int, err error) {
 		FROM entries e
 			JOIN feeds f ON f.id = e.feed_id
 			JOIN categories c ON c.id = f.category_id
-		WHERE %s
-	`
-	condition := e.buildCondition()
+		WHERE ` + e.buildCondition()
 
-	err = e.store.db.QueryRow(fmt.Sprintf(query, condition), e.args...).Scan(&count)
+	err = e.store.db.QueryRow(query, e.args...).Scan(&count)
 	if err != nil {
 		return 0, fmt.Errorf("store: unable to count entries: %v", err)
 	}
@@ -308,12 +306,7 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
 			icons i ON i.id=fi.icon_id
 		LEFT JOIN
 			users u ON u.id=e.user_id
-		WHERE %s %s
-	`
-
-	condition := e.buildCondition()
-	sorting := e.buildSorting()
-	query = fmt.Sprintf(query, condition, sorting)
+		WHERE ` + e.buildCondition() + " " + e.buildSorting()
 
 	rows, err := e.store.db.Query(query, e.args...)
 	if err != nil {
@@ -426,12 +419,7 @@ func (e *EntryQueryBuilder) GetEntryIDs() ([]int64, error) {
 			feeds f
 		ON
 			f.id=e.feed_id
-		WHERE
-			%s %s
-	`
-
-	condition := e.buildCondition()
-	query = fmt.Sprintf(query, condition, e.buildSorting())
+		WHERE ` + e.buildCondition() + " " + e.buildSorting()
 
 	rows, err := e.store.db.Query(query, e.args...)
 	if err != nil {
@@ -462,7 +450,7 @@ func (e *EntryQueryBuilder) buildSorting() string {
 	var parts string
 
 	if len(e.sortExpressions) > 0 {
-		parts += fmt.Sprintf(" ORDER BY %s", strings.Join(e.sortExpressions, ", "))
+		parts += " ORDER BY " + strings.Join(e.sortExpressions, ", ")
 	}
 
 	if e.limit > 0 {