refactor: allow unmarshalMapMsgpack to handle int maps

This commit is contained in:
Derrick Hammer 2024-01-05 06:05:59 -05:00
parent 6c27a978d1
commit a10bec66ea
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 9 deletions

View File

@ -18,7 +18,7 @@ type unmarshalNewInstanceFunc func() interface{}
var _ SerializableMetadata = (*directoryReferenceMap)(nil)
func unmarshalMapMsgpack(dec *msgpack.Decoder, m *linkedhashmap.Map, placeholder interface{}) error {
func unmarshalMapMsgpack(dec *msgpack.Decoder, m *linkedhashmap.Map, placeholder interface{}, intMap bool) error {
*m = *linkedhashmap.New()
l, err := dec.DecodeMapLen()
@ -27,12 +27,20 @@ func unmarshalMapMsgpack(dec *msgpack.Decoder, m *linkedhashmap.Map, placeholder
}
for i := 0; i < l; i++ {
key, err := dec.DecodeString()
var key interface{}
if intMap {
intKey, err := dec.DecodeInt()
if err != nil {
return err
}
fmt.Println("dir: ", key)
key = intKey
} else {
strKey, err := dec.DecodeString()
if err != nil {
return err
}
key = strKey
}
switch placeholder.(type) {
case *DirectoryReference:
@ -119,7 +127,7 @@ func unmarshalMapJson(bytes []byte, m *linkedhashmap.Map, newInstance unmarshalN
return err
}
err = json.Unmarshal(data, &instance)
err = json.Unmarshal(data, instance)
if err != nil {
return err
}
@ -142,7 +150,7 @@ func (drm directoryReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
}
func (drm *directoryReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error {
return unmarshalMapMsgpack(dec, &drm.Map, &DirectoryReference{})
return unmarshalMapMsgpack(dec, &drm.Map, &DirectoryReference{}, false)
}
func (frm fileReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
@ -150,7 +158,7 @@ func (frm fileReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
}
func (frm *fileReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error {
return unmarshalMapMsgpack(dec, &frm.Map, &FileReference{})
return unmarshalMapMsgpack(dec, &frm.Map, &FileReference{}, false)
}
func (frm *fileReferenceMap) UnmarshalJSON(bytes []byte) error {