From 2425ac4c880ad0f3d28449f80481351eceb7fab6 Mon Sep 17 00:00:00 2001 From: Vuong <3168632+vuon9@users.noreply.github.com> Date: Sun, 30 Jan 2022 12:44:16 +0700 Subject: [PATCH] Improve test for readable purpose --- cointop/util.go | 9 +++------ cointop/util_test.go | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cointop/util.go b/cointop/util.go index a9cdc7b..856568e 100644 --- a/cointop/util.go +++ b/cointop/util.go @@ -71,7 +71,7 @@ func normalizeFloatString(input string, allowNegative bool) string { func getStructHash(x interface{}) string { raw := make(map[string]string) - collectTypeFields(reflect.TypeOf(x), raw) + collectTypeFieldsAsMap(reflect.TypeOf(x), raw) h := sha256.New() h.Write([]byte(fmt.Sprintf("%v", raw))) @@ -79,8 +79,7 @@ func getStructHash(x interface{}) string { return fmt.Sprintf("%x", h.Sum(nil)) } -func collectTypeFields(t reflect.Type, m map[string]string) { - // Return if not struct or pointer to struct. +func collectTypeFieldsAsMap(t reflect.Type, m map[string]string) { if t.Kind() == reflect.Ptr { t = t.Elem() } @@ -88,14 +87,12 @@ func collectTypeFields(t reflect.Type, m map[string]string) { return } - // Iterate through fields collecting names in map. for i := 0; i < t.NumField(); i++ { sf := t.Field(i) m[sf.Name] = fmt.Sprintf("%v", sf) - // Recurse into anonymous fields. if t.Kind() == reflect.Struct { - collectTypeFields(sf.Type, m) + collectTypeFieldsAsMap(sf.Type, m) } } } diff --git a/cointop/util_test.go b/cointop/util_test.go index 9809640..c4dc1c5 100644 --- a/cointop/util_test.go +++ b/cointop/util_test.go @@ -23,7 +23,7 @@ func Test_getStructHash(t *testing.T) { P10D int } }{}, - str2: struct { + str2: &struct { Name string Properties struct { P7D int