1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package format
import (
"reflect"
"unsafe"
)
type Stringer interface{ String() string }
var (
// stringer type for implement checks.
stringerType = typeof[Stringer]()
// error type for implement checks.
errorType = typeof[error]()
)
// getMethodType returns a *possible* FormatFunc to handle case
// of a type that implements any known interface{} types, else nil.
func getMethodType(t typenode) FormatFunc {
switch {
case t.rtype.Implements(stringerType):
switch t.rtype.Kind() {
case reflect.Interface:
return getInterfaceStringerType(t)
default:
return getConcreteStringerType(t)
}
case t.rtype.Implements(errorType):
switch t.rtype.Kind() {
case reflect.Interface:
return getInterfaceErrorType(t)
default:
return getConcreteErrorType(t)
}
default:
return nil
}
}
// getInterfaceStringerType returns a FormatFunc to handle case of an interface{}
// type that implements Stringer{}, i.e. Stringer{} itself and any superset of.
func getInterfaceStringerType(t typenode) FormatFunc {
switch t.indirect() && !t.iface_indir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil {
appendNil(s)
return
}
v := *(*Stringer)(s.P)
appendString(s, v.String())
})
case false:
return with_typestr_ptrs(t, func(s *State) {
if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil {
appendNil(s)
return
}
v := *(*Stringer)(s.P)
appendString(s, v.String())
})
default:
panic("unreachable")
}
}
// getConcreteStringerType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has a Stringer{} method receiver.
func getConcreteStringerType(t typenode) FormatFunc {
itab := get_iface_ITab[Stringer](t.rtype)
switch t.indirect() && !t.iface_indir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
if s.P == nil {
appendNil(s)
return
}
v := *(*Stringer)(pack_iface(itab, s.P))
appendString(s, v.String())
})
case false:
return with_typestr_ptrs(t, func(s *State) {
if s.P == nil {
appendNil(s)
return
}
v := *(*Stringer)(pack_iface(itab, s.P))
appendString(s, v.String())
})
default:
panic("unreachable")
}
}
// getInterfaceErrorType returns a FormatFunc to handle case of an interface{}
// type that implements error{}, i.e. error{} itself and any superset of.
func getInterfaceErrorType(t typenode) FormatFunc {
switch t.indirect() && !t.iface_indir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil {
appendNil(s)
return
}
v := *(*error)(s.P)
appendString(s, v.Error())
})
case false:
return with_typestr_ptrs(t, func(s *State) {
if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil {
appendNil(s)
return
}
v := *(*error)(s.P)
appendString(s, v.Error())
})
default:
panic("unreachable")
}
}
// getConcreteErrorType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has an error{} method receiver.
func getConcreteErrorType(t typenode) FormatFunc {
itab := get_iface_ITab[error](t.rtype)
switch t.indirect() && !t.iface_indir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
if s.P == nil {
appendNil(s)
return
}
v := *(*error)(pack_iface(itab, s.P))
appendString(s, v.Error())
})
case false:
return with_typestr_ptrs(t, func(s *State) {
if s.P == nil {
appendNil(s)
return
}
v := *(*error)(pack_iface(itab, s.P))
appendString(s, v.Error())
})
default:
panic("unreachable")
}
}
|