summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-05-10 04:46:06 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-05-10 04:46:06 -0600
commit2744e0700ca6fe956f569d47010fd4e693fedcfa (patch)
tree17cc6eb920ece3700a10a86ed465d4a0947d32c4 /pkg
parenta16049ef805c0c08b90885a5b7dfea7f74e51c5f (diff)
more
Diffstat (limited to 'pkg')
-rw-r--r--pkg/binstruct/l1.go6
-rw-r--r--pkg/binstruct/l2.go7
-rw-r--r--pkg/binstruct/l3.go14
3 files changed, 16 insertions, 11 deletions
diff --git a/pkg/binstruct/l1.go b/pkg/binstruct/l1.go
index c535d2e..e76e7d0 100644
--- a/pkg/binstruct/l1.go
+++ b/pkg/binstruct/l1.go
@@ -136,10 +136,10 @@ func genHandler(typ reflect.Type) (handler, error) {
}
return primitive{
unmarshal: func(dat []byte) interface{} {
- val := reflect.Zero(typ)
+ val := reflect.New(typ).Elem()
for i := 0; i < typ.Len(); i++ {
- fmt.Printf("%v[%d]: %v\n", typ, i, val.Index(i))
- val.Index(i).Set(reflect.ValueOf(inner.Unmarshal(dat[i*int(inner.Size()):])))
+ fieldVal := inner.Unmarshal(dat[i*int(inner.Size()):])
+ val.Index(i).Set(reflect.ValueOf(fieldVal).Convert(typ.Elem()))
}
return val.Interface()
},
diff --git a/pkg/binstruct/l2.go b/pkg/binstruct/l2.go
index 005d686..32aa313 100644
--- a/pkg/binstruct/l2.go
+++ b/pkg/binstruct/l2.go
@@ -16,18 +16,20 @@ type structHandler struct {
}
type structField struct {
+ typ reflect.Type
tag
handler
name string
}
func (sh structHandler) Unmarshal(dat []byte) interface{} {
- val := reflect.Zero(sh.typ)
+ val := reflect.New(sh.typ).Elem()
for i, field := range sh.fields {
if field.skip {
continue
}
- val.Field(i).Set(reflect.ValueOf(field.Unmarshal(dat[field.off:])))
+ fieldVal := field.Unmarshal(dat[field.off:])
+ val.Field(i).Set(reflect.ValueOf(fieldVal).Convert(field.typ))
}
return val.Interface()
}
@@ -96,6 +98,7 @@ func genStructHandler(structInfo reflect.Type) (handler, error) {
curOffset += fieldTag.siz
ret.fields = append(ret.fields, structField{
+ typ: fieldInfo.Type,
tag: fieldTag,
handler: fieldHandler,
name: fieldInfo.Name,
diff --git a/pkg/binstruct/l3.go b/pkg/binstruct/l3.go
index f9fb8b1..1ccaaf0 100644
--- a/pkg/binstruct/l3.go
+++ b/pkg/binstruct/l3.go
@@ -21,12 +21,14 @@ func getHandler(typ reflect.Type) (handler, error) {
return h, nil
}
-func Unmarshal(dat []byte, dst interface{}) error {
- _dst := reflect.ValueOf(dst)
- if _dst.Kind() != reflect.Ptr {
- return fmt.Errorf("not a pointer: %v", _dst.Type())
+func Unmarshal(dat []byte, dstPtr interface{}) error {
+ _dstPtr := reflect.ValueOf(dstPtr)
+ if _dstPtr.Kind() != reflect.Ptr {
+ return fmt.Errorf("not a pointer: %v", _dstPtr.Type())
}
- handler, err := getHandler(_dst.Type().Elem())
+
+ dst := _dstPtr.Elem()
+ handler, err := getHandler(dst.Type())
if err != nil {
return err
}
@@ -35,7 +37,7 @@ func Unmarshal(dat []byte, dst interface{}) error {
handler.Size(), len(dat))
}
val := handler.Unmarshal(dat[:handler.Size()])
- _dst.Elem().Set(reflect.ValueOf(val))
+ dst.Set(reflect.ValueOf(val).Convert(dst.Type()))
return nil
}