summaryrefslogtreecommitdiff
path: root/pkg/binstruct/marshal.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-10 13:18:30 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-10 13:35:20 -0600
commit27401b6ea459921a6152ab1744da1618358465f4 (patch)
tree2c4f9c096f1a593e65d7f824901e815ca48bfaf0 /pkg/binstruct/marshal.go
parent42f6f78e0a32ba0eda707154f8e1ffb4579604ee (diff)
Rename the module, mv pkg lib
Diffstat (limited to 'pkg/binstruct/marshal.go')
-rw-r--r--pkg/binstruct/marshal.go42
1 files changed, 0 insertions, 42 deletions
diff --git a/pkg/binstruct/marshal.go b/pkg/binstruct/marshal.go
deleted file mode 100644
index 684d2f3..0000000
--- a/pkg/binstruct/marshal.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package binstruct
-
-import (
- "encoding"
- "fmt"
- "reflect"
-)
-
-type Marshaler = encoding.BinaryMarshaler
-
-func Marshal(obj any) ([]byte, error) {
- if mar, ok := obj.(Marshaler); ok {
- return mar.MarshalBinary()
- }
- return MarshalWithoutInterface(obj)
-}
-
-func MarshalWithoutInterface(obj any) ([]byte, error) {
- val := reflect.ValueOf(obj)
- switch val.Kind() {
- case reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64:
- typ := intKind2Type[val.Kind()]
- return val.Convert(typ).Interface().(Marshaler).MarshalBinary()
- case reflect.Ptr:
- return Marshal(val.Elem().Interface())
- case reflect.Array:
- var ret []byte
- for i := 0; i < val.Len(); i++ {
- bs, err := Marshal(val.Index(i).Interface())
- ret = append(ret, bs...)
- if err != nil {
- return ret, err
- }
- }
- return ret, nil
- case reflect.Struct:
- return getStructHandler(val.Type()).Marshal(val)
- default:
- panic(fmt.Errorf("type=%v does not implement binfmt.Marshaler and kind=%v is not a supported statically-sized kind",
- val.Type(), val.Kind()))
- }
-}