Ensure that slices have been allocated when entering array

This commit is contained in:
Thomas Pelletier
2021-02-19 08:53:19 -05:00
parent 7f9822db35
commit 978143ce99
2 changed files with 19 additions and 0 deletions
+15
View File
@@ -490,6 +490,21 @@ func (b *Builder) Set(v reflect.Value) error {
return t.set(v)
}
// EnsureSlice makes sure that the cursor points to a non-nil slice.
func (b *Builder) EnsureSlice() error {
t := b.top()
v := t.get()
if v.Kind() != reflect.Slice {
return IncorrectKindError{Actual: v.Kind(), Expected: reflect.Slice}
}
if v.IsNil() {
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
}
return nil
}
func checkKindInt(rt reflect.Type) error {
switch rt.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+4
View File
@@ -65,6 +65,10 @@ func (u *unmarshaler) ArrayBegin() {
return
}
u.builder.Save()
u.err = u.builder.EnsureSlice()
if u.err != nil {
return
}
if u.assign {
u.assign = false
} else {