From ae1d9b94d4cb4f689a7e4a9e7b82fa649e1ec0b4 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 5 May 2025 22:29:45 -0400 Subject: [PATCH] engine: util: Add a debug utility This is useful for some patches. Let's see if I can remember to use and improve it! --- engine/graph/exporter.go | 1 + engine/util/util.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/engine/graph/exporter.go b/engine/graph/exporter.go index e50fee5d..49dcea97 100644 --- a/engine/graph/exporter.go +++ b/engine/graph/exporter.go @@ -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? obj.Logf("%s", res) + //obj.Logf("%s\n", engineUtil.DebugStructFields(res)) // debug // XXX: Add a TTL if requested b, err := obj.World.ResExport(ctx, resourceExports) // do it! if err != nil { diff --git a/engine/util/util.go b/engine/util/util.go index fbeec9c2..120def73 100644 --- a/engine/util/util.go +++ b/engine/util/util.go @@ -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, "") + } else { + s += fmt.Sprintf("(%s): %v\n", field.Name, value.Elem().Interface()) + } + } + + return s +}