Added allocation for pointers
This commit is contained in:
parent
5e0ae8c260
commit
26d353867b
|
@ -0,0 +1,27 @@
|
|||
package ssob
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func allocType(out reflect.Value) (err error) {
|
||||
if out.Type().Kind() != reflect.Ptr || out.IsNil() {
|
||||
return errors.New("ssob: Error allocating type (Got nil pointer)")
|
||||
}
|
||||
|
||||
t := out.Elem().Type()
|
||||
ret := reflect.New(t)
|
||||
retp := ret
|
||||
for {
|
||||
if reflect.Ptr != t.Kind() {
|
||||
break
|
||||
}
|
||||
retp = retp.Elem()
|
||||
t = retp.Type().Elem()
|
||||
retp.Set(reflect.New(t))
|
||||
}
|
||||
out.Elem().Set(reflect.Indirect(ret))
|
||||
|
||||
return nil
|
||||
}
|
|
@ -573,8 +573,15 @@ func unmarshal(e interface{}, in []byte) (n int, err error) {
|
|||
return unmarshal(e, in)
|
||||
}
|
||||
}
|
||||
if !p.Elem().IsValid() {
|
||||
err = allocType(v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
p = reflect.Indirect(v)
|
||||
}
|
||||
|
||||
return unmarshal(p, in)
|
||||
return unmarshal(p.Interface(), in)
|
||||
}
|
||||
|
||||
func RegisterDecoder(name string, f func(e interface{}, in []byte) (n int, err error)) {
|
||||
|
|
|
@ -700,8 +700,15 @@ func unsafeUnmarshal(e interface{}, in []byte) (n int, err error) {
|
|||
return unsafeUnmarshal(e, in)
|
||||
}
|
||||
}
|
||||
if !p.Elem().IsValid() {
|
||||
err = allocType(v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
p = reflect.Indirect(v)
|
||||
}
|
||||
|
||||
return unsafeUnmarshal(p, in)
|
||||
return unsafeUnmarshal(p.Interface(), in)
|
||||
}
|
||||
|
||||
func RegisterUnsafeDecoder(name string, f func(e interface{}, in []byte) (n int, err error)) {
|
||||
|
|
Loading…
Reference in New Issue