Skip to content

Commit

Permalink
handle reflect.Value in Stringer and Byteser
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrre committed Aug 5, 2024
1 parent b3ad1d0 commit c284da1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _assertauto/TestConfig/ByteserHexReflectValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"entries": [
{
"equal": "(reflect.Value) => (int) 123"
},
{
"equal": 0
}
]
}
10 changes: 10 additions & 0 deletions _assertauto/TestConfig/StringerReflectValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"entries": [
{
"equal": "(reflect.Value) => (int) 123"
},
{
"equal": 0
}
]
}
6 changes: 6 additions & 0 deletions pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,9 @@ func writeByteserHex(c *Config, w io.Writer, st *State, v reflect.Value, maxLen
if v.Kind() == reflect.Pointer && v.IsNil() {
return false
}
if v.Type() == typeReflectValue {
return false
}
br := v.Interface().(byteser) //nolint:forcetypeassert // Checked above.
b := br.Bytes()
writeArrowWrappedString(w, ".Bytes() ")
Expand Down Expand Up @@ -764,6 +767,9 @@ func writeStringer(w io.Writer, v reflect.Value, maxLen int) bool {
if v.Kind() == reflect.Pointer && v.IsNil() {
return false
}
if v.Type() == typeReflectValue {
return false
}
sr := v.Interface().(fmt.Stringer) //nolint:forcetypeassert // Checked above.
s := sr.String()
writeArrowWrappedString(w, ".String() ")
Expand Down
20 changes: 20 additions & 0 deletions pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ var testCases = []testCase{
c.ValueWriters = []ValueWriter{NewByteserHexValueWriter(0)}
},
},
{
name: "ByteserHexReflectValue",
value: reflect.ValueOf(123),
configure: func(c *Config) {
c.ValueWriters = []ValueWriter{
NewByteserHexValueWriter(0),
NewReflectValueValueWriter(),
}
},
},
{
name: "Error",
value: &testError{},
Expand Down Expand Up @@ -494,6 +504,16 @@ var testCases = []testCase{
c.ValueWriters = []ValueWriter{NewStringerValueWriter(0)}
},
},
{
name: "StringerReflectValue",
value: reflect.ValueOf(123),
configure: func(c *Config) {
c.ValueWriters = []ValueWriter{
NewStringerValueWriter(0),
NewReflectValueValueWriter(),
}
},
},
{
name: "FilterMatch",
value: &testStringer{s: "test"},
Expand Down

0 comments on commit c284da1

Please sign in to comment.