From e8ba64d915fd6a08c06d86dd874cbf2cbe7b79a3 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Thu, 14 Mar 2019 15:48:58 +0300 Subject: [PATCH] Small refactoring (#2) - Use val.Index(0).Interface().(type) in switch-case of SortSliceByValue - add go1.12 to TravisCI --- .travis.yml | 1 + hrw.go | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8ea4227..cf7f947 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: go go: - 1.11.x + - 1.12.x env: - GO111MODULE=on install: diff --git a/hrw.go b/hrw.go index 8ee43e4..8c6cce4 100644 --- a/hrw.go +++ b/hrw.go @@ -82,33 +82,38 @@ func SortSliceByValue(slice interface{}, hash uint64) { return } - switch slice := slice.(type) { - case []int: + switch val.Index(0).Interface().(type) { + case int: var key = make([]byte, 16) + slice := slice.([]int) + for i := 0; i < length; i++ { binary.BigEndian.PutUint64(key, uint64(slice[i])) rule = append(rule, weight(Hash(key), hash)) } - case []int32: + case int32: var key = make([]byte, 16) + slice := slice.([]int32) + for i := 0; i < length; i++ { binary.BigEndian.PutUint32(key, uint32(slice[i])) rule = append(rule, weight(Hash(key), hash)) } - case []string: + case string: + slice := slice.([]string) + for i := 0; i < length; i++ { rule = append(rule, weight(hash, Hash([]byte(slice[i])))) } - default: - if _, ok := val.Index(0).Interface().(Hasher); !ok { - return - } - + case Hasher: for i := 0; i < length; i++ { h := val.Index(i).Interface().(Hasher) rule = append(rule, weight(hash, h.Hash())) } + + default: + return } rule = SortByWeight(rule, hash)