Skip to content

Commit 5a6641a

Browse files
committed
refactor: Use ptr for QueryNodes.toSQL()
Since `SortBy` and `SortDirection` is modified directly from `QueryNodes` pointer, `sortBy` and `sortDirrection` return value from `QueryNodes.toSQL()` no longger needed
1 parent 37798bd commit 5a6641a

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

internal/monero/monero.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type QueryNodes struct {
8787
}
8888

8989
// toSQL generates SQL query from query parameters
90-
func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection string) {
90+
func (q *QueryNodes) toSQL() (args []interface{}, where string) {
9191
wq := []string{}
9292

9393
if q.Host != "" {
@@ -128,17 +128,14 @@ func (q QueryNodes) toSQL() (args []interface{}, where, sortBy, sortDirection st
128128
where = "WHERE " + strings.Join(wq, " AND ")
129129
}
130130

131-
as := []string{"last_checked", "uptime"}
132-
sortBy = "last_checked"
133-
if slices.Contains(as, q.SortBy) {
134-
sortBy = q.SortBy
131+
if !slices.Contains([]string{"last_checked", "uptime"}, q.SortBy) {
132+
q.SortBy = "last_checked"
135133
}
136-
sortDirection = "DESC"
137-
if q.SortDirection == "asc" {
138-
sortDirection = "ASC"
134+
if q.SortDirection != "asc" {
135+
q.SortDirection = "DESC"
139136
}
140137

141-
return args, where, sortBy, sortDirection
138+
return args, where
142139
}
143140

144141
// Nodes represents a list of nodes
@@ -150,7 +147,7 @@ type Nodes struct {
150147

151148
// Get nodes from database
152149
func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
153-
args, where, sortBy, sortDirection := q.toSQL()
150+
args, where := q.toSQL()
154151

155152
var nodes Nodes
156153

@@ -174,12 +171,12 @@ func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) {
174171
*
175172
FROM
176173
tbl_node
177-
%s -- where query if any
174+
%s
178175
ORDER BY
179176
%s
180177
%s
181178
LIMIT ?
182-
OFFSET ?`, where, sortBy, sortDirection)
179+
OFFSET ?`, where, q.SortBy, q.SortDirection)
183180
err = r.db.Select(&nodes.Items, query, args...)
184181

185182
return nodes, err

internal/monero/monero_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ func TestQueryNodes_toSQL(t *testing.T) {
8787
}
8888
for _, tt := range tests {
8989
t.Run(tt.name, func(t *testing.T) {
90-
gotArgs, gotWhere, gotSortBy, gotSortDirection := tt.query.toSQL()
90+
gotArgs, gotWhere := tt.query.toSQL()
9191
if !equalArgs(gotArgs, tt.wantArgs) {
9292
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
9393
}
9494
if gotWhere != tt.wantWhere {
9595
t.Errorf("QueryNodes.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere)
9696
}
97-
if gotSortBy != tt.wantSortBy {
98-
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", gotSortBy, tt.wantSortBy)
97+
if tt.query.SortBy != tt.wantSortBy {
98+
t.Errorf("QueryNodes.toSQL() gotSortBy = %v, want %v", tt.query.SortBy, tt.wantSortBy)
9999
}
100-
if gotSortDirection != tt.wantSortDirection {
101-
t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", gotSortDirection, tt.wantSortDirection)
100+
if tt.query.SortDirection != tt.wantSortDirection {
101+
t.Errorf("QueryNodes.toSQL() gotSortDirection = %v, want %v", tt.query.SortDirection, tt.wantSortDirection)
102102
}
103103
})
104104
}
@@ -107,19 +107,20 @@ func TestQueryNodes_toSQL(t *testing.T) {
107107
// Single bench test:
108108
// go test ./internal/monero -bench QueryNodes_toSQL -benchmem -run=^$ -v
109109
func Benchmark_QueryNodes_toSQL(b *testing.B) {
110+
q := QueryNodes{
111+
Host: "test",
112+
Nettype: "any",
113+
Protocol: "any",
114+
CC: "any",
115+
Status: -1,
116+
CORS: -1,
117+
RowsPerPage: 10,
118+
Page: 1,
119+
SortBy: "last_checked",
120+
SortDirection: "desc",
121+
}
110122
for i := 0; i < b.N; i++ {
111-
_, _, _, _ = QueryNodes{
112-
Host: "test",
113-
Nettype: "any",
114-
Protocol: "any",
115-
CC: "any",
116-
Status: -1,
117-
CORS: -1,
118-
RowsPerPage: 10,
119-
Page: 1,
120-
SortBy: "last_checked",
121-
SortDirection: "desc",
122-
}.toSQL()
123+
_, _ = q.toSQL()
123124
}
124125
}
125126

0 commit comments

Comments
 (0)