engine: util: Add a debug utility

This is useful for some patches. Let's see if I can remember to use and
improve it!
This commit is contained in:
James Shubin
2025-05-05 22:29:45 -04:00
parent 267bcc144b
commit ae1d9b94d4
2 changed files with 32 additions and 0 deletions

View File

@@ -485,3 +485,34 @@ func CleanError(err error) string {
}
return strings.ReplaceAll(err.Error(), "\n", " ")
}
// DebugStructFields returns a pretty string display of struct fields (like a
// resource) for debugging. The output is not guaranteed to be stable.
func DebugStructFields(st interface{}) string {
s := ""
v := reflect.ValueOf(st)
t := reflect.TypeOf(st)
// if it's a pointer, get the element it points to
if v.Kind() == reflect.Ptr {
v = v.Elem()
t = t.Elem()
}
for i := 0; i < v.NumField(); i++ {
field := t.Field(i)
value := v.Field(i)
// only print exported (public) fields
if field.PkgPath != "" {
continue
}
if value.IsZero() {
s += fmt.Sprintf("(%s): %v\n", field.Name, "<nil>")
} else {
s += fmt.Sprintf("(%s): %v\n", field.Name, value.Elem().Interface())
}
}
return s
}