Skip to content

Commit d0c4aad

Browse files
committed
refactor(acceptHeaders): remove redundant field wildcards
1 parent 0f270f8 commit d0c4aad

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

src/acceptHeaders/acceptItem.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,39 @@ const defaultQuality = 1000
1111
const maxQualityDecimals = 3
1212

1313
type acceptItem struct {
14-
value string
15-
quality int
16-
wildcards int
14+
value string
15+
quality int
1716
}
1817

1918
func (item acceptItem) less(other acceptItem) bool {
2019
if item.quality != other.quality {
2120
return item.quality > other.quality
2221
}
23-
return item.wildcards < other.wildcards
22+
23+
if item.value != other.value {
24+
if other.value == "*/*" {
25+
return true
26+
} else if strings.HasSuffix(other.value, "/*") && !strings.HasPrefix(item.value, "/*") {
27+
return true
28+
}
29+
}
30+
31+
return false
2432
}
2533

2634
func (item acceptItem) match(value string) bool {
2735
if item.value == value {
2836
return true
2937
}
30-
31-
switch item.wildcards {
32-
case 0:
33-
return false
34-
case 1:
35-
slashIndex := strings.IndexByte(item.value, '/')
36-
itemPrefix := item.value[:slashIndex+1]
37-
return strings.HasPrefix(value, itemPrefix)
38-
case 2:
38+
if item.value == "*/*" {
3939
return true
4040
}
41+
if !strings.HasSuffix(item.value, "/*") {
42+
return false
43+
}
4144

42-
return false
45+
itemPrefix := item.value[:len(item.value)-1]
46+
return strings.HasPrefix(value, itemPrefix)
4347
}
4448

4549
func parseAcceptItem(input string) acceptItem {
@@ -60,14 +64,7 @@ func parseAcceptItem(input string) acceptItem {
6064
}
6165
}
6266

63-
wildcards := 0
64-
if value == "*/*" {
65-
wildcards = 2
66-
} else if strings.HasSuffix(value, "/*") {
67-
wildcards = 1
68-
}
69-
70-
return acceptItem{value, quality, wildcards}
67+
return acceptItem{value, quality}
7168
}
7269

7370
func parseQuality(input string) (quality int) {

src/acceptHeaders/acceptItem_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ func TestParseAcceptItem(t *testing.T) {
163163
func TestAcceptItemMatch(t *testing.T) {
164164
var item acceptItem
165165

166-
item = acceptItem{"text/html", 1000, 0}
166+
item = acceptItem{"text/html", 1000}
167167
if !item.match("text/html") {
168168
t.Error()
169169
}
170170
if item.match("text/plain") {
171171
t.Error()
172172
}
173173

174-
item = acceptItem{"text/*", 1000, 1}
174+
item = acceptItem{"text/*", 1000}
175175
if !item.match("text/*") {
176176
t.Error()
177177
}
@@ -185,7 +185,7 @@ func TestAcceptItemMatch(t *testing.T) {
185185
t.Error()
186186
}
187187

188-
item = acceptItem{"*/*", 1000, 2}
188+
item = acceptItem{"*/*", 1000}
189189
if !item.match("text/*") {
190190
t.Error()
191191
}

src/acceptHeaders/accepts_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ func TestParseAccept(t *testing.T) {
1212
t.Error(len(accepts))
1313
}
1414

15-
if !reflect.DeepEqual(accepts[0], acceptItem{"text/html", 1000, 0}) {
15+
if !reflect.DeepEqual(accepts[0], acceptItem{"text/html", 1000}) {
1616
t.Error(accepts[0])
1717
}
18-
if !reflect.DeepEqual(accepts[1], acceptItem{"text/plain", 900, 0}) {
18+
if !reflect.DeepEqual(accepts[1], acceptItem{"text/plain", 900}) {
1919
t.Error(accepts[1])
2020
}
21-
if !reflect.DeepEqual(accepts[2], acceptItem{"image/png", 800, 0}) {
21+
if !reflect.DeepEqual(accepts[2], acceptItem{"image/png", 800}) {
2222
t.Error(accepts[2])
2323
}
24-
if !reflect.DeepEqual(accepts[3], acceptItem{"image/*", 800, 1}) {
24+
if !reflect.DeepEqual(accepts[3], acceptItem{"image/*", 800}) {
2525
t.Error(accepts[3])
2626
}
27-
if !reflect.DeepEqual(accepts[4], acceptItem{"application/json", 700, 0}) {
27+
if !reflect.DeepEqual(accepts[4], acceptItem{"application/json", 700}) {
2828
t.Error(accepts[4])
2929
}
30-
if !reflect.DeepEqual(accepts[5], acceptItem{"*/*", 600, 2}) {
30+
if !reflect.DeepEqual(accepts[5], acceptItem{"*/*", 600}) {
3131
t.Error(accepts[5])
3232
}
3333

0 commit comments

Comments
 (0)