blob: ec4ca7e4d38ea17bd5cb9b09708d2367bc6ee70b [file] [log] [blame]
bseenivadd66c362026-02-12 19:13:26 +05301package gomock
2
3import (
4 "fmt"
5 "reflect"
6)
7
8// getString is a safe way to convert a value to a string for printing results
9// If the value is a a mock, getString avoids calling the mocked String() method,
10// which avoids potential deadlocks
11func getString(x any) string {
12 if isGeneratedMock(x) {
13 return fmt.Sprintf("%T", x)
14 }
15 if s, ok := x.(fmt.Stringer); ok {
16 return s.String()
17 }
18 return fmt.Sprintf("%v", x)
19}
20
21// isGeneratedMock checks if the given type has a "isgomock" field,
22// indicating it is a generated mock.
23func isGeneratedMock(x any) bool {
24 typ := reflect.TypeOf(x)
25 if typ == nil {
26 return false
27 }
28 if typ.Kind() == reflect.Ptr {
29 typ = typ.Elem()
30 }
31 if typ.Kind() != reflect.Struct {
32 return false
33 }
34 _, isgomock := typ.FieldByName("isgomock")
35 return isgomock
36}