Skip to content

Commit

Permalink
Merge pull request #19 from bayashi/add-struct-address
Browse files Browse the repository at this point in the history
add IsStructType() in obj.go
  • Loading branch information
bayashi committed Dec 30, 2023
2 parents 37cabb3 + f0886fd commit ac206f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion obj/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions obj/obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit ac206f8

Please sign in to comment.