From a76dc6c72806ab047d4143eeb558ffdd60bac9d1 Mon Sep 17 00:00:00 2001 From: Larry Clapp Date: Thu, 18 Apr 2024 12:05:37 -0400 Subject: [PATCH] DebugPrint: Print the string value of tokens In DebugPrint, if a value implements fmt.Stringer, and isn't a zero value, display its string value in addition to the default %#v format. For the most part, this prints the string value of "tokens", e.g. for a syntax.Redirect.Op, instead of "Op: 0x3b", you get "Op: 0x3b (>&)". --- syntax/walk.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/syntax/walk.go b/syntax/walk.go index 5be8f9c6..c9b713a3 100644 --- a/syntax/walk.go +++ b/syntax/walk.go @@ -308,6 +308,10 @@ func (p *debugPrinter) print(x reflect.Value) { } p.printf("}") default: - p.printf("%#v", x.Interface()) + if s, ok := x.Interface().(fmt.Stringer); ok && !x.IsZero() { + p.printf("%#v (%s)", x.Interface(), s) + } else { + p.printf("%#v", x.Interface()) + } } }