Skip to content

Commit e8ba64d

Browse files
authored
Small refactoring (#2)
- Use val.Index(0).Interface().(type) in switch-case of SortSliceByValue - add go1.12 to TravisCI
1 parent 29dac15 commit e8ba64d

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: go
22
go:
33
- 1.11.x
4+
- 1.12.x
45
env:
56
- GO111MODULE=on
67
install:

hrw.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,38 @@ func SortSliceByValue(slice interface{}, hash uint64) {
8282
return
8383
}
8484

85-
switch slice := slice.(type) {
86-
case []int:
85+
switch val.Index(0).Interface().(type) {
86+
case int:
8787
var key = make([]byte, 16)
88+
slice := slice.([]int)
89+
8890
for i := 0; i < length; i++ {
8991
binary.BigEndian.PutUint64(key, uint64(slice[i]))
9092
rule = append(rule, weight(Hash(key), hash))
9193
}
92-
case []int32:
94+
case int32:
9395
var key = make([]byte, 16)
96+
slice := slice.([]int32)
97+
9498
for i := 0; i < length; i++ {
9599
binary.BigEndian.PutUint32(key, uint32(slice[i]))
96100
rule = append(rule, weight(Hash(key), hash))
97101
}
98-
case []string:
102+
case string:
103+
slice := slice.([]string)
104+
99105
for i := 0; i < length; i++ {
100106
rule = append(rule, weight(hash,
101107
Hash([]byte(slice[i]))))
102108
}
103-
default:
104-
if _, ok := val.Index(0).Interface().(Hasher); !ok {
105-
return
106-
}
107-
109+
case Hasher:
108110
for i := 0; i < length; i++ {
109111
h := val.Index(i).Interface().(Hasher)
110112
rule = append(rule, weight(hash, h.Hash()))
111113
}
114+
115+
default:
116+
return
112117
}
113118

114119
rule = SortByWeight(rule, hash)

0 commit comments

Comments
 (0)