Skip to content

Commit

Permalink
add TypeFullName option to print type full name
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrre committed Aug 4, 2024
1 parent c08e045 commit 520a5be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions _assertauto/TestConfig/TypeFullName.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"entries": [
{
"equal": "(github.com/pierrre/pretty_test.testStruct) {\n\tFoo: (int) 123,\n\tBar: (float64) 123.456,\n\tunexported: (int) 123,\n}"
},
{
"equal": 0
}
]
}
12 changes: 11 additions & 1 deletion pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"unsafe" //nolint:depguard // Required for string to []byte conversion.

"github.com/pierrre/go-libs/bufpool"
"github.com/pierrre/go-libs/reflectutil"
"github.com/pierrre/go-libs/strconvio"
)

Expand All @@ -39,6 +40,9 @@ var DefaultConfig = NewConfig()
//
// It should be created with [NewConfig].
type Config struct {
// TypeFullName prints the full type name if true.
// Default: false.
TypeFullName bool
// Indent is the string used to indent.
// Default: "\t".
Indent string
Expand Down Expand Up @@ -178,7 +182,13 @@ func (c *Config) writeTypeAndValue(w io.Writer, st *State, v reflect.Value) {
}

func (c *Config) writeType(w io.Writer, typ reflect.Type) {
_, _ = writeString(w, typ.String())
var s string
if c.TypeFullName {
s = reflectutil.TypeFullName(typ)
} else {
s = typ.String()
}
_, _ = writeString(w, s)
}

func (c *Config) writeValue(w io.Writer, st *State, v reflect.Value) {
Expand Down
11 changes: 11 additions & 0 deletions pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ var testCases = []testCase{
c.StructUnexported = false
},
},
{
name: "TypeFullName",
value: testStruct{
Foo: 123,
Bar: 123.456,
unexported: 123,
},
configure: func(c *Config) {
c.TypeFullName = true
},
},
{
name: "MaxDepth",
value: func() any {
Expand Down

0 comments on commit 520a5be

Please sign in to comment.