Skip to content

Commit cc58872

Browse files
committed
Fix handling of []byte and uint in value reflector
1 parent a7c3ccd commit cc58872

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

typed/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (p ParseableType) FromStructured(in interface{}) (*TypedValue, error) {
124124
}
125125
return AsTyped(v, p.Schema, p.TypeRef)
126126
}
127+
127128
// DeducedParseableType is a ParseableType that deduces the type from
128129
// the content of the object.
129130
var DeducedParseableType ParseableType = createOrDie(YAMLObject(`types:

value/valuereflect.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package value
1818

1919
import (
2020
"bytes"
21+
"encoding/base64"
2122
"encoding/json"
2223
"fmt"
2324
"reflect"
@@ -78,24 +79,32 @@ func (r valueReflect) IsMap() bool {
7879
}
7980

8081
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)
8284
}
8385

8486
func (r valueReflect) IsBool() bool {
8587
return r.isKind(reflect.Bool)
8688
}
8789

8890
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)
9193
}
9294

9395
func (r valueReflect) IsFloat() bool {
9496
return r.isKind(reflect.Float64, reflect.Float32)
9597
}
9698

9799
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
99108
}
100109

101110
func (r valueReflect) IsNull() bool {
@@ -153,9 +162,13 @@ func (r valueReflect) Bool() bool {
153162
}
154163

155164
func (r valueReflect) Int() int64 {
156-
if r.IsInt() {
165+
if r.isKind(reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8) {
157166
return r.Value.Int()
158167
}
168+
if r.isKind(reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uint8) {
169+
return int64(r.Value.Uint())
170+
}
171+
159172
panic("value is not an int")
160173
}
161174

@@ -167,9 +180,13 @@ func (r valueReflect) Float() float64 {
167180
}
168181

169182
func (r valueReflect) String() string {
170-
if r.IsString() {
183+
kind := r.Value.Kind()
184+
if kind == reflect.String {
171185
return r.Value.String()
172186
}
187+
if kind == reflect.Slice && r.Value.Type().Elem().Kind() == reflect.Uint8 {
188+
return base64.StdEncoding.EncodeToString(r.Value.Bytes())
189+
}
173190
panic("value is not a string")
174191
}
175192

@@ -193,7 +210,7 @@ func (r valueReflect) Unstructured() interface{} {
193210
case r.IsFloat():
194211
return r.Float()
195212
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()))
197214
}
198215
}
199216

value/valuereflect_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package value
1818

1919
import (
20+
"encoding/base64"
2021
"encoding/json"
2122
"reflect"
2223
"testing"
@@ -37,31 +38,51 @@ func TestReflectPrimitives(t *testing.T) {
3738
t.Error("expected IsString to be true")
3839
}
3940
if rv.String() != "string" {
40-
t.Errorf("expected rv.String to be 'string' but got %s", rv.String())
41+
t.Errorf("expected rv.String to be 'string' but got %v", rv.Unstructured())
42+
}
43+
44+
rv = MustReflect([]byte("string"))
45+
if !rv.IsString() {
46+
t.Error("expected IsString to be true")
47+
}
48+
if rv.IsList() {
49+
t.Error("expected IsList to be false ([]byte is represented as a base64 encoded string)")
50+
}
51+
encoded := base64.StdEncoding.EncodeToString([]byte("string"))
52+
if rv.String() != encoded {
53+
t.Errorf("expected rv.String to be %v but got %v", []byte(encoded), rv.Unstructured())
4154
}
4255

4356
rv = MustReflect(1)
4457
if !rv.IsInt() {
4558
t.Error("expected IsInt to be true")
4659
}
4760
if rv.Int() != 1 {
48-
t.Errorf("expected rv.Int to be 1 but got %s", rv.String())
61+
t.Errorf("expected rv.Int to be 1 but got %v", rv.Unstructured())
62+
}
63+
64+
rv = MustReflect(uint32(3000000000))
65+
if !rv.IsInt() {
66+
t.Error("expected IsInt to be true")
67+
}
68+
if rv.Int() != 3000000000 {
69+
t.Errorf("expected rv.Int to be 3000000000 but got %v", rv.Unstructured())
4970
}
5071

5172
rv = MustReflect(1.5)
5273
if !rv.IsFloat() {
5374
t.Error("expected IsFloat to be true")
5475
}
5576
if rv.Float() != 1.5 {
56-
t.Errorf("expected rv.Float to be 1.1 but got %s", rv.String())
77+
t.Errorf("expected rv.Float to be 1.1 but got %v", rv.Unstructured())
5778
}
5879

5980
rv = MustReflect(true)
6081
if !rv.IsBool() {
6182
t.Error("expected IsBool to be true")
6283
}
6384
if rv.Bool() != true {
64-
t.Errorf("expected rv.Bool to be true but got %s", rv.String())
85+
t.Errorf("expected rv.Bool to be true but got %v", rv.Unstructured())
6586
}
6687

6788
rv = MustReflect(nil)

0 commit comments

Comments
 (0)