@@ -18,6 +18,7 @@ package value
18
18
19
19
import (
20
20
"bytes"
21
+ "encoding/base64"
21
22
"encoding/json"
22
23
"fmt"
23
24
"reflect"
@@ -78,24 +79,32 @@ func (r valueReflect) IsMap() bool {
78
79
}
79
80
80
81
func (r valueReflect ) IsList () bool {
81
- return r .isKind (reflect .Slice , reflect .Array )
82
+ typ := r .Value .Type ()
83
+ return typ .Kind () == reflect .Slice && ! (typ .Elem ().Kind () == reflect .Uint8 )
82
84
}
83
85
84
86
func (r valueReflect ) IsBool () bool {
85
87
return r .isKind (reflect .Bool )
86
88
}
87
89
88
90
func (r valueReflect ) IsInt () bool {
89
- // This feels wrong. Very wrong .
90
- return r .isKind (reflect .Int , reflect .Int64 , reflect .Int32 , reflect .Int16 , reflect .Int8 , reflect .Uint64 , reflect . Uint , reflect .Uint32 , reflect .Uint16 , reflect .Uint8 )
91
+ // Uint64 deliberately excluded, see valueUnstructured.Int .
92
+ return r .isKind (reflect .Int , reflect .Int64 , reflect .Int32 , reflect .Int16 , reflect .Int8 , reflect .Uint , reflect .Uint32 , reflect .Uint16 , reflect .Uint8 )
91
93
}
92
94
93
95
func (r valueReflect ) IsFloat () bool {
94
96
return r .isKind (reflect .Float64 , reflect .Float32 )
95
97
}
96
98
97
99
func (r valueReflect ) IsString () bool {
98
- return r .isKind (reflect .String )
100
+ kind := r .Value .Kind ()
101
+ if kind == reflect .String {
102
+ return true
103
+ }
104
+ if kind == reflect .Slice && r .Value .Type ().Elem ().Kind () == reflect .Uint8 {
105
+ return true
106
+ }
107
+ return false
99
108
}
100
109
101
110
func (r valueReflect ) IsNull () bool {
@@ -153,9 +162,13 @@ func (r valueReflect) Bool() bool {
153
162
}
154
163
155
164
func (r valueReflect ) Int () int64 {
156
- if r .IsInt ( ) {
165
+ if r .isKind ( reflect . Int , reflect . Int64 , reflect . Int32 , reflect . Int16 , reflect . Int8 ) {
157
166
return r .Value .Int ()
158
167
}
168
+ if r .isKind (reflect .Uint , reflect .Uint32 , reflect .Uint16 , reflect .Uint8 ) {
169
+ return int64 (r .Value .Uint ())
170
+ }
171
+
159
172
panic ("value is not an int" )
160
173
}
161
174
@@ -167,9 +180,13 @@ func (r valueReflect) Float() float64 {
167
180
}
168
181
169
182
func (r valueReflect ) String () string {
170
- if r .IsString () {
183
+ kind := r .Value .Kind ()
184
+ if kind == reflect .String {
171
185
return r .Value .String ()
172
186
}
187
+ if kind == reflect .Slice && r .Value .Type ().Elem ().Kind () == reflect .Uint8 {
188
+ return base64 .StdEncoding .EncodeToString (r .Value .Bytes ())
189
+ }
173
190
panic ("value is not a string" )
174
191
}
175
192
@@ -193,7 +210,7 @@ func (r valueReflect) Unstructured() interface{} {
193
210
case r .IsFloat ():
194
211
return r .Float ()
195
212
default :
196
- panic ("value is not a map or struct" )
213
+ panic (fmt . Sprintf ( "value of type %s is not a supported by value reflector" , val . Type ()) )
197
214
}
198
215
}
199
216
0 commit comments