From 42508e35c7725c7b17fe46d44597f90010e516d3 Mon Sep 17 00:00:00 2001 From: Rene Kaufmann Date: Thu, 29 Dec 2016 17:48:00 +0100 Subject: [PATCH] use go 1.8 slice sorting --- template/sort.go | 63 ------------------- ...ilter_functions.go => template_filters.go} | 8 ++- ...tions_test.go => template_filters_test.go} | 0 template/template_funcs.go | 7 ++- 4 files changed, 12 insertions(+), 66 deletions(-) delete mode 100644 template/sort.go rename template/{filter_functions.go => template_filters.go} (96%) rename template/{filter_functions_test.go => template_filters_test.go} (100%) diff --git a/template/sort.go b/template/sort.go deleted file mode 100644 index d9175862..00000000 --- a/template/sort.go +++ /dev/null @@ -1,63 +0,0 @@ -/* - * This file is part of remco. - * Based on code from confd. - * https://github.com/kelseyhightower/confd/blob/6bb3c21a63459c3be340d53c4d3463397c8324c6/resource/template/template_funcs.go - * © 2013 Kelsey Hightower - * © 2015 Justin Burnham - * - * © 2016 The Remco Authors - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -package template - -import ( - "fmt" - "net" - - "github.com/HeavyHorst/memkv" -) - -type sortSRV []*net.SRV - -func (s sortSRV) Len() int { - return len(s) -} - -func (s sortSRV) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} - -func (s sortSRV) Less(i, j int) bool { - str1 := fmt.Sprintf("%s%d%d%d", s[i].Target, s[i].Port, s[i].Priority, s[i].Weight) - str2 := fmt.Sprintf("%s%d%d%d", s[j].Target, s[j].Port, s[j].Priority, s[j].Weight) - return str1 < str2 -} - -type byLengthKV []memkv.KVPair - -func (s byLengthKV) Len() int { - return len(s) -} - -func (s byLengthKV) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} - -func (s byLengthKV) Less(i, j int) bool { - return len(s[i].Key) < len(s[j].Key) -} - -type byLength []string - -func (s byLength) Len() int { - return len(s) -} -func (s byLength) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} -func (s byLength) Less(i, j int) bool { - return len(s[i]) < len(s[j]) -} diff --git a/template/filter_functions.go b/template/template_filters.go similarity index 96% rename from template/filter_functions.go rename to template/template_filters.go index bde1981f..8010fd01 100644 --- a/template/filter_functions.go +++ b/template/template_filters.go @@ -106,11 +106,15 @@ func filterSortByLength(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, * switch values.(type) { case []string: v := values.([]string) - sort.Sort(byLength(v)) + sort.Slice(v, func(i, j int) bool { + return len(v[i]) < len(v[j]) + }) return pongo2.AsValue(v), nil case memkv.KVPairs: v := values.(memkv.KVPairs) - sort.Sort(byLengthKV(v)) + sort.Slice(v, func(i, j int) bool { + return len(v[i].Key) < len(v[j].Key) + }) return pongo2.AsValue(v), nil } diff --git a/template/filter_functions_test.go b/template/template_filters_test.go similarity index 100% rename from template/filter_functions_test.go rename to template/template_filters_test.go diff --git a/template/template_funcs.go b/template/template_funcs.go index a4213a57..88881a80 100644 --- a/template/template_funcs.go +++ b/template/template_funcs.go @@ -80,6 +80,11 @@ func lookupSRV(service, proto, name string) []*net.SRV { if err != nil { return []*net.SRV{} } - sort.Sort(sortSRV(addrs)) + //sort.Sort(sortSRV(addrs)) + sort.Slice(addrs, func(i, j int) bool { + str1 := fmt.Sprintf("%s%d%d%d", addrs[i].Target, addrs[i].Port, addrs[i].Priority, addrs[i].Weight) + str2 := fmt.Sprintf("%s%d%d%d", addrs[j].Target, addrs[j].Port, addrs[j].Priority, addrs[j].Weight) + return str1 < str2 + }) return addrs }