From f0886fdae9fe2735b70bd63ce4f9b24bb09ff87a Mon Sep 17 00:00:00 2001 From: bayashi <42190+bayashi@users.noreply.github.com> Date: Sat, 30 Dec 2023 10:37:40 +0000 Subject: [PATCH] add IsStructType() in obj.go --- obj/obj.go | 11 ++++++++++- obj/obj_test.go | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/obj/obj.go b/obj/obj.go index dd8368c..70d2ed9 100644 --- a/obj/obj.go +++ b/obj/obj.go @@ -66,7 +66,11 @@ func (o *Object) AsString() string { case error: return fmt.Sprintf("%+v", o.value) default: - return fmt.Sprintf("%#v", o.value) + if o.IsStructType() { + return fmt.Sprintf("%p, %#v", &o.value, o.value) + } else { + return fmt.Sprintf("%#v", o.value) + } } } @@ -106,6 +110,11 @@ func (o *Object) IsPointerType() bool { return o.value != nil && reflect.TypeOf(o.value).Kind() == reflect.Pointer } +// Return boolean whether the value is a struct +func (o *Object) IsStructType() bool { + return o.value != nil && reflect.TypeOf(o.value).Kind() == reflect.Struct +} + // format and truncate. For `fmt/print.go` Formatter interface func (o *Object) Format(s fmt.State, verb rune) { switch verb { diff --git a/obj/obj_test.go b/obj/obj_test.go index e57787f..fa99a3f 100644 --- a/obj/obj_test.go +++ b/obj/obj_test.go @@ -75,6 +75,16 @@ func TestPointerValue(t *testing.T) { } } +func TestStructValue(t *testing.T) { + i := struct{ ID int }{ ID: 123 } + o := NewObject(i) + + expectRe := regexp.MustCompile(`[0-9a-fx]+, struct { ID int }{ID:123}`) + if expectRe.FindStringSubmatch(o.AsString()) == nil { + t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString()) + } +} + func TestDump(t *testing.T) { o := NewObject(123) if o.AsDumpString() != "(int) 123\n" {