fix: we need to define custom array encoding and decoding api due to non-standard message packing in the dart implementation

This commit is contained in:
Derrick Hammer 2024-01-07 22:33:04 -05:00
parent fec2adb72f
commit 819219cdcf
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 62 additions and 0 deletions

62
utils/array.go Normal file
View File

@ -0,0 +1,62 @@
package utils
import (
"errors"
"github.com/vmihailenco/msgpack/v5"
"net/url"
)
func EncodeMsgpackArray(enc *msgpack.Encoder, array interface{}) error {
switch v := array.(type) {
case []*url.URL:
// Handle []*url.URL slice
err := enc.EncodeInt(int64(len(v)))
if err != nil {
return err
}
for _, item := range v {
err = enc.Encode(item)
if err != nil {
return err
}
}
return nil
default:
// Handle generic case
arr, ok := array.([]interface{})
if !ok {
return errors.New("unsupported type for EncodeMsgpackArray")
}
err := enc.EncodeInt(int64(len(arr)))
if err != nil {
return err
}
for _, item := range arr {
err = enc.Encode(item)
if err != nil {
return err
}
}
return nil
}
}
func DecodeMsgpackArray(dec *msgpack.Decoder) ([]interface{}, error) {
arrayLen, err := dec.DecodeInt()
if err != nil {
return nil, err
}
array := make([]interface{}, arrayLen)
for i := 0; i < int(arrayLen); i++ {
item, err := dec.DecodeInterface()
if err != nil {
return nil, err
}
array[i] = item
}
return array, nil
}