Skip to content

Commit aeb5224

Browse files
committed
refactor(serverHandler): simplify sort query string logic
No longer distinguish sorting not specified or specified as empty string.
1 parent 7c0578f commit aeb5224

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

src/serverHandler/pathContext.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package serverHandler
33
type pathContext struct {
44
simple bool
55
download bool
6-
sort *string // keep different for param is not specified or is empty
6+
sort string
77
defaultSort string
88
}
99

@@ -19,10 +19,10 @@ func (ctx pathContext) QueryString() string {
1919
buffer = append(buffer, []byte("download&")...) // 9 bytes
2020
}
2121

22-
if ctx.sort != nil && *(ctx.sort) != ctx.defaultSort {
23-
buffer = append(buffer, []byte("sort=")...) // 5 bytes
24-
buffer = append(buffer, []byte(*ctx.sort)...) // 2 bytes
25-
buffer = append(buffer, '&') // 1 byte
22+
if len(ctx.sort) > 0 && ctx.sort != ctx.defaultSort {
23+
buffer = append(buffer, []byte("sort=")...) // 5 bytes
24+
buffer = append(buffer, []byte(ctx.sort)...) // 2 bytes
25+
buffer = append(buffer, '&') // 1 byte
2626
}
2727

2828
buffer = buffer[:len(buffer)-1]
@@ -31,7 +31,7 @@ func (ctx pathContext) QueryString() string {
3131

3232
func (ctx pathContext) QueryStringOfSort(sort string) string {
3333
copiedCtx := ctx
34-
copiedCtx.sort = &sort
34+
copiedCtx.sort = sort
3535
return copiedCtx.QueryString()
3636
}
3737

src/serverHandler/pathContext_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ func TestPathContext(t *testing.T) {
1414
}
1515

1616
sort = ""
17-
result = (&pathContext{defaultSort: "/n", sort: &sort}).QueryString()
18-
if result != "?sort=" {
17+
result = (&pathContext{defaultSort: "/n", sort: sort}).QueryString()
18+
if result != "" {
1919
t.Error(result)
2020
}
2121

2222
sort = "/n"
23-
result = (&pathContext{defaultSort: "/n", sort: &sort}).QueryString()
23+
result = (&pathContext{defaultSort: "/n", sort: sort}).QueryString()
2424
if result != "" {
2525
t.Error(result)
2626
}
2727

2828
sort = ""
29-
result = (&pathContext{sort: &sort}).QueryString()
29+
result = (&pathContext{sort: sort}).QueryString()
3030
if result != "" {
3131
t.Error(result)
3232
}
3333

3434
sort = "/n"
35-
result = (&pathContext{sort: &sort}).QueryString()
35+
result = (&pathContext{sort: sort}).QueryString()
3636
if result != "?sort=/n" {
3737
t.Error(result)
3838
}
@@ -58,25 +58,25 @@ func TestPathContext(t *testing.T) {
5858
}
5959

6060
sort = "/n"
61-
result = (&pathContext{simple: false, sort: &sort}).QueryString()
61+
result = (&pathContext{simple: false, sort: sort}).QueryString()
6262
if result != "?sort=/n" {
6363
t.Error(result)
6464
}
6565

6666
sort = "/n"
67-
result = (&pathContext{simple: true, sort: &sort}).QueryString()
67+
result = (&pathContext{simple: true, sort: sort}).QueryString()
6868
if result != "?simple&sort=/n" {
6969
t.Error(result)
7070
}
7171

7272
sort = "/n"
73-
result = (&pathContext{download: true, sort: &sort}).QueryString()
73+
result = (&pathContext{download: true, sort: sort}).QueryString()
7474
if result != "?download&sort=/n" {
7575
t.Error(result)
7676
}
7777

7878
sort = "/n"
79-
result = (&pathContext{simple: true, download: true, sort: &sort}).QueryString()
79+
result = (&pathContext{simple: true, download: true, sort: sort}).QueryString()
8080
if result != "?simple&download&sort=/n" {
8181
t.Error(result)
8282
}

src/serverHandler/sessionData.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,11 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
484484
}
485485

486486
subItems = h.FilterItems(subItems)
487-
rawSortBy, sortState := sortInfos(subItems, query.Get("sort"), h.defaultSort)
487+
sort := query.Get("sort")
488+
if len(sort) > 2 {
489+
sort = sort[:2]
490+
}
491+
sortState := sortInfos(subItems, sort, h.defaultSort)
488492

489493
if h.emptyRoot && status == http.StatusOK && len(vhostReqPath) > 1 {
490494
status = http.StatusNotFound
@@ -566,7 +570,7 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
566570
Context: pathContext{
567571
simple: isSimple,
568572
download: isDownload,
569-
sort: rawSortBy,
573+
sort: sort,
570574
defaultSort: h.defaultSort,
571575
},
572576
}

src/serverHandler/sort.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,10 @@ func sortInfoOriginal(items []os.FileInfo, compareDir fnCompareDir) {
357357

358358
// sort
359359

360-
func sortInfos(items []os.FileInfo, sortBy string, defaultSortBy string) (rawSortBy *string, sortInfo SortState) {
360+
func sortInfos(items []os.FileInfo, sortBy string, defaultSortBy string) (sortInfo SortState) {
361361
if len(sortBy) == 0 {
362362
sortBy = defaultSortBy
363-
} else {
364-
rawSortBy = &sortBy
365363
}
366-
367364
if len(sortBy) == 0 {
368365
return
369366
}
@@ -413,5 +410,5 @@ func sortInfos(items []os.FileInfo, sortBy string, defaultSortBy string) (rawSor
413410
}
414411
}
415412

416-
return rawSortBy, SortState{dirSort, sortKey}
413+
return SortState{dirSort, sortKey}
417414
}

0 commit comments

Comments
 (0)