2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-14 18:12:57 +00:00

Improve test for readable purpose

This commit is contained in:
Vuong 2022-01-30 12:44:16 +07:00
parent 4039bc1b08
commit 2425ac4c88
No known key found for this signature in database
GPG Key ID: F4C603CC37B47A46
2 changed files with 4 additions and 7 deletions

View File

@ -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)
}
}
}

View File

@ -23,7 +23,7 @@ func Test_getStructHash(t *testing.T) {
P10D int
}
}{},
str2: struct {
str2: &struct {
Name string
Properties struct {
P7D int