Improve test for readable purpose

pull/302/head
Vuong 2 years ago
parent 4039bc1b08
commit 2425ac4c88
No known key found for this signature in database
GPG Key ID: F4C603CC37B47A46

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

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

Loading…
Cancel
Save