From a27d480bccd86583e924fa59ebd294cc998c7b32 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Tue, 12 Apr 2022 05:32:28 +0200 Subject: [PATCH 1/2] Simplify ToString function for types (avoid unnecessary casting) --- utils/utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index d305a7c8..262c6257 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -26,7 +26,7 @@ func ToString(value interface{}) string { case int32: return strconv.FormatInt(int64(value), 10) case int64: - return strconv.FormatInt(int64(value), 10) + return strconv.FormatInt(value, 10) case uint8: return strconv.FormatUint(uint64(value), 10) case uint16: @@ -34,11 +34,11 @@ func ToString(value interface{}) string { case uint32: return strconv.FormatUint(uint64(value), 10) case uint64: - return strconv.FormatUint(uint64(value), 10) + return strconv.FormatUint(value, 10) case float32: return strconv.FormatFloat(float64(value), 'g', -1, 64) case float64: - return strconv.FormatFloat(float64(value), 'g', -1, 64) + return strconv.FormatFloat(value, 'g', -1, 64) case bool: return strconv.FormatBool(value) default: From 364a244af9003224bfb3c474b25ac829b6462221 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Tue, 12 Apr 2022 19:30:54 +0200 Subject: [PATCH 2/2] Enforce String() inteface on Container --- containers/containers.go | 3 ++- containers/containers_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/containers/containers.go b/containers/containers.go index c35ab36d..a512a3cb 100644 --- a/containers/containers.go +++ b/containers/containers.go @@ -21,10 +21,11 @@ type Container interface { Size() int Clear() Values() []interface{} + String() string } // GetSortedValues returns sorted container's elements with respect to the passed comparator. -// Does not effect the ordering of elements within the container. +// Does not affect the ordering of elements within the container. func GetSortedValues(container Container, comparator utils.Comparator) []interface{} { values := container.Values() if len(values) < 2 { diff --git a/containers/containers_test.go b/containers/containers_test.go index 94cf4f80..60e0332e 100644 --- a/containers/containers_test.go +++ b/containers/containers_test.go @@ -7,7 +7,9 @@ package containers import ( + "fmt" "github.com/emirpasic/gods/utils" + "strings" "testing" ) @@ -32,6 +34,16 @@ func (container ContainerTest) Values() []interface{} { return container.values } +func (container ContainerTest) String() string { + str := "ContainerTest\n" + var values []string + for _, value := range container.values { + values = append(values, fmt.Sprintf("%v", value)) + } + str += strings.Join(values, ", ") + return str +} + func TestGetSortedValuesInts(t *testing.T) { container := ContainerTest{} container.values = []interface{}{5, 1, 3, 2, 4}