@@ -23,77 +23,149 @@ func (f *FormatterUtils) StringifyValues(ps *pool.State, v ...interface{}) {
23
23
// StringifyValue writes into the buffer converted to string value that
24
24
// it receives.
25
25
func (f * FormatterUtils ) StringifyValue (ps * pool.State , v interface {}) {
26
- switch v .(type ) {
26
+ switch t := v .(type ) {
27
+ case nil :
28
+ ps .Buffer .WriteString ("<nil>" )
27
29
case int :
28
- ps .Buffer .WriteInt (int64 (v .( int ) ))
30
+ ps .Buffer .WriteInt (int64 (t ))
29
31
case int8 :
30
- ps .Buffer .WriteInt (int64 (v .( int8 ) ))
32
+ ps .Buffer .WriteInt (int64 (t ))
31
33
case int16 :
32
- ps .Buffer .WriteInt (int64 (v .( int16 ) ))
34
+ ps .Buffer .WriteInt (int64 (t ))
33
35
case int32 :
34
- ps .Buffer .WriteInt (int64 (v .( int32 ) ))
36
+ ps .Buffer .WriteInt (int64 (t ))
35
37
case int64 :
36
- ps .Buffer .WriteInt (v .( int64 ) )
38
+ ps .Buffer .WriteInt (t )
37
39
case uint :
38
- ps .Buffer .WriteUint (uint64 (v .( uint ) ))
40
+ ps .Buffer .WriteUint (uint64 (t ))
39
41
case uint8 :
40
- ps .Buffer .WriteUint (uint64 (v .( uint8 ) ))
42
+ ps .Buffer .WriteUint (uint64 (t ))
41
43
case uint16 :
42
- ps .Buffer .WriteUint (uint64 (v .( uint16 ) ))
44
+ ps .Buffer .WriteUint (uint64 (t ))
43
45
case uint32 :
44
- ps .Buffer .WriteUint (uint64 (v .( uint32 ) ))
46
+ ps .Buffer .WriteUint (uint64 (t ))
45
47
case uint64 :
46
- ps .Buffer .WriteUint (v .( uint64 ) )
48
+ ps .Buffer .WriteUint (t )
47
49
case uintptr :
48
- ps .Buffer .WriteUint (uint64 (v .( uintptr ) ))
50
+ ps .Buffer .WriteUint (uint64 (t ))
49
51
case string :
50
- ps .Buffer .WriteString (v .( string ) )
52
+ ps .Buffer .WriteString (t )
51
53
case bool :
52
- ps .Buffer .WriteBool (v .( bool ) )
54
+ ps .Buffer .WriteBool (t )
53
55
case float32 :
54
- ps .Buffer .WriteFloat (float64 (v .( float32 ) ), 32 )
56
+ ps .Buffer .WriteFloat (float64 (t ), 32 )
55
57
case float64 :
56
- ps .Buffer .WriteFloat (v .(float64 ), 64 )
57
- case nil :
58
- ps .Buffer .WriteString ("<nil>" )
58
+ ps .Buffer .WriteFloat (t , 64 )
59
59
case complex64 :
60
+ f .writeComplex (ps , complex128 (t ), 64 )
60
61
case complex128 :
62
+ f .writeComplex (ps , t , 128 )
61
63
default :
62
- f .StringifyValueWithReflect (ps , v )
64
+ f .StringifyValueWithReflect (ps , reflect . ValueOf ( v ) )
63
65
}
64
66
}
65
67
66
68
// StringifyValueWithReflect converts the given value using the reflect.
67
- func (f * FormatterUtils ) StringifyValueWithReflect (ps * pool.State , v interface {}) {
68
- typeOfValue := reflect .TypeOf (v )
69
- switch typeOfValue .Kind () {
69
+ func (f * FormatterUtils ) StringifyValueWithReflect (ps * pool.State , value reflect.Value ) {
70
+ switch t := value .Kind (); t {
71
+ case reflect .Invalid :
72
+ ps .Buffer .WriteString ("<invalid reflect.Value>" )
73
+ case reflect .Bool :
74
+ ps .Buffer .WriteBool (value .Bool ())
75
+ case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
76
+ ps .Buffer .WriteInt (value .Int ())
77
+ case reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr :
78
+ ps .Buffer .WriteUint (value .Uint ())
79
+ case reflect .Float32 :
80
+ ps .Buffer .WriteFloat (value .Float (), 32 )
81
+ case reflect .Float64 :
82
+ ps .Buffer .WriteFloat (value .Float (), 64 )
83
+ case reflect .Complex64 :
84
+ f .writeComplex (ps , value .Complex (), 64 )
85
+ case reflect .Complex128 :
86
+ f .writeComplex (ps , value .Complex (), 128 )
87
+ case reflect .String :
88
+ ps .Buffer .WriteString (value .String ())
89
+ case reflect .Map :
90
+ ps .Buffer .WriteString (value .Type ().String ())
91
+ if value .IsNil () {
92
+ ps .Buffer .WriteString ("<nil>" )
93
+ return
94
+ }
95
+ ps .Buffer .WriteByte ('{' )
96
+ ps .Buffer .WriteSpace ()
97
+ keys := value .MapKeys ()
98
+ for i , key := range keys {
99
+ if i > 0 {
100
+ ps .Buffer .WriteString (", " )
101
+ }
102
+ f .StringifyValueWithReflect (ps , key )
103
+ ps .Buffer .WriteString (": " )
104
+ f .StringifyValueWithReflect (ps , value .MapIndex (key ))
105
+ }
106
+ ps .Buffer .WriteSpace ()
107
+ ps .Buffer .WriteByte ('}' )
70
108
case reflect .Struct :
71
- f .StringifyStruct (ps , typeOfValue )
72
- default :
73
- ps .Buffer .WriteString (typeOfValue .Name ())
74
- ps .Buffer .WriteString (typeOfValue .String ())
75
- }
76
- }
109
+ ps .Buffer .WriteString (value .Type ().String ())
110
+ ps .Buffer .WriteByte ('{' )
111
+ ps .Buffer .WriteSpace ()
77
112
78
- // StringifyStruct converts structure into a string.
79
- func (f * FormatterUtils ) StringifyStruct (ps * pool.State , t reflect.Type ) {
80
- ps .Buffer .WriteNewLine ()
81
- ps .Buffer .WriteString (t .Name ())
82
- ps .Buffer .WriteSpace ()
83
- ps .Buffer .WriteByte ('{' )
84
- ps .Buffer .WriteNewLine ()
85
-
86
- for i := 0 ; i < t .NumField (); i ++ {
87
- field := t .Field (i )
88
- ps .Buffer .WriteByte ('\t' )
89
- ps .Buffer .WriteString (field .Name )
90
- ps .Buffer .WriteString (": " )
91
- ps .Buffer .WriteString (field .Type .String ())
92
- ps .Buffer .WriteNewLine ()
93
- }
113
+ for i := 0 ; i < value .NumField (); i ++ {
114
+ if i > 0 {
115
+ ps .Buffer .WriteString (", " )
116
+ }
94
117
95
- ps .Buffer .WriteByte ('}' )
96
- ps .Buffer .WriteNewLine ()
118
+ field := value .Field (i )
119
+ ps .Buffer .WriteString (value .Type ().Field (i ).Name )
120
+ ps .Buffer .WriteString (": " )
121
+ f .StringifyValueWithReflect (ps , field )
122
+ }
123
+
124
+ ps .Buffer .WriteSpace ()
125
+ ps .Buffer .WriteByte ('}' )
126
+ case reflect .Interface :
127
+ v := value .Elem ()
128
+ if ! v .IsValid () {
129
+ ps .Buffer .WriteString (v .Type ().String ())
130
+ } else {
131
+ f .StringifyValue (ps , v .Interface ())
132
+ }
133
+ case reflect .Array , reflect .Slice :
134
+ ps .Buffer .WriteByte ('[' )
135
+ for i := 0 ; i < value .Len (); i ++ {
136
+ if i > 0 {
137
+ ps .Buffer .WriteByte (' ' )
138
+ }
139
+ f .StringifyValueWithReflect (ps , value .Index (i ))
140
+ }
141
+ ps .Buffer .WriteByte (']' )
142
+ case reflect .Ptr :
143
+ if value .Pointer () != 0 {
144
+ switch a := value .Elem (); value .Kind () {
145
+ case reflect .Array , reflect .Slice , reflect .Struct , reflect .Map :
146
+ ps .Buffer .WriteByte ('&' )
147
+ f .StringifyValueWithReflect (ps , a )
148
+ return
149
+ }
150
+ }
151
+ fallthrough
152
+ case reflect .Chan , reflect .Func , reflect .UnsafePointer :
153
+ switch value .Kind () {
154
+ case reflect .Chan , reflect .Func , reflect .Map , reflect .Ptr , reflect .Slice , reflect .UnsafePointer :
155
+ ptr := value .Pointer ()
156
+ ps .Buffer .WriteByte ('(' )
157
+ ps .Buffer .WriteString (value .Type ().String ())
158
+ ps .Buffer .WriteString (")(" )
159
+ if ptr == 0 {
160
+ ps .Buffer .WriteString ("<nil>" )
161
+ } else {
162
+ ps .Buffer .WriteUint (uint64 (ptr ))
163
+ }
164
+ ps .Buffer .WriteByte (')' )
165
+ }
166
+ default :
167
+ ps .Buffer .WriteString (value .String ())
168
+ }
97
169
}
98
170
99
171
// StringifyByTemplate converts value into a string by the given format/template.
@@ -180,3 +252,10 @@ func (f *FormatterUtils) KeyValuesToMap(ps *pool.State, keyValues ...interface{}
180
252
ps .Map .Set (key .(string ), keyValues [i + 1 ])
181
253
}
182
254
}
255
+
256
+ func (f * FormatterUtils ) writeComplex (ps * pool.State , v complex128 , size int ) {
257
+ ps .Buffer .WriteByte ('(' )
258
+ ps .Buffer .WriteFloat (real (v ), size / 2 )
259
+ ps .Buffer .WriteFloat (imag (v ), size / 2 )
260
+ ps .Buffer .WriteString ("i)" )
261
+ }
0 commit comments