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