diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-05-30 12:00:54 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-05-30 12:00:54 -0400 |
commit | 703e98f2759148aa2d6ac80f2519ae8e41da3e95 (patch) | |
tree | 746c17fb2e155ed8448b3ac44aced0672fc83a95 /pkg/binstruct/marshal.go | |
parent | 8576e5f207f9d3b7c6324ed71a3ca6a005f9ae7c (diff) |
ahhhhhhh
Diffstat (limited to 'pkg/binstruct/marshal.go')
-rw-r--r-- | pkg/binstruct/marshal.go | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/pkg/binstruct/marshal.go b/pkg/binstruct/marshal.go index c8158a4..90f30b6 100644 --- a/pkg/binstruct/marshal.go +++ b/pkg/binstruct/marshal.go @@ -1,20 +1,27 @@ package binstruct import ( + "encoding" "fmt" "reflect" ) -type Marshaler interface { - MarshalBinary() ([]byte, error) -} +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: + return val.Convert(u8Type).Interface().(Marshaler).MarshalBinary() + case reflect.Int8: + return val.Convert(i8Type).Interface().(Marshaler).MarshalBinary() case reflect.Ptr: return Marshal(val.Elem().Interface()) case reflect.Array: @@ -28,7 +35,7 @@ func Marshal(obj any) ([]byte, error) { } return ret, nil case reflect.Struct: - // TODO + 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())) |