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:
@@ -184,6 +184,7 @@ func (obj *Exporter) Export(ctx context.Context, res engine.Res) (bool, error) {
|
|||||||
|
|
||||||
// TODO: Do we want to log more information about where this exports to?
|
// TODO: Do we want to log more information about where this exports to?
|
||||||
obj.Logf("%s", res)
|
obj.Logf("%s", res)
|
||||||
|
//obj.Logf("%s\n", engineUtil.DebugStructFields(res)) // debug
|
||||||
// XXX: Add a TTL if requested
|
// XXX: Add a TTL if requested
|
||||||
b, err := obj.World.ResExport(ctx, resourceExports) // do it!
|
b, err := obj.World.ResExport(ctx, resourceExports) // do it!
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -485,3 +485,34 @@ func CleanError(err error) string {
|
|||||||
}
|
}
|
||||||
return strings.ReplaceAll(err.Error(), "\n", " ")
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user