From 2ac5ff60bea320a62c87c4993337c29496002047 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 4 Jan 2024 12:41:39 -0500 Subject: [PATCH] refactor: need to abstract DirectoryMetadata maps to their own types to properly handle msgpack serialization --- metadata/directory.go | 105 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/metadata/directory.go b/metadata/directory.go index 9b251a1..867f945 100644 --- a/metadata/directory.go +++ b/metadata/directory.go @@ -7,15 +7,19 @@ import ( "github.com/vmihailenco/msgpack/v5" ) +type directoryReferenceMap map[string]DirectoryReference +type fileReferenceMap map[string]FileReference + type DirectoryMetadata struct { - Details DirectoryMetadataDetails `json:"details"` - Directories map[string]DirectoryReference `json:"directories"` - Files map[string]FileReference `json:"files"` - ExtraMetadata ExtraMetadata `json:"extraMetadata"` + Details DirectoryMetadataDetails `json:"details"` + Directories directoryReferenceMap `json:"directories"` + Files fileReferenceMap `json:"files"` + ExtraMetadata ExtraMetadata `json:"extraMetadata"` BaseMetadata } var _ SerializableMetadata = (*DirectoryMetadata)(nil) +var _ SerializableMetadata = (*directoryReferenceMap)(nil) func NewDirectoryMetadata(details DirectoryMetadataDetails, directories map[string]DirectoryReference, files map[string]FileReference, extraMetadata ExtraMetadata) *DirectoryMetadata { dirMetadata := &DirectoryMetadata{ @@ -90,3 +94,96 @@ func (dm *DirectoryMetadata) DecodeMsgpack(dec *msgpack.Decoder) error { return nil } +func (drm directoryReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error { + // First, encode the length of the map + if err := enc.EncodeMapLen(len(drm)); err != nil { + return err + } + + // Then, encode each key-value pair + for k, v := range drm { + if err := enc.EncodeString(k); err != nil { + return err + } + if err := enc.Encode(&v); err != nil { // Assuming DirectoryReference can be encoded by msgpack + return err + } + } + + return nil +} + +func (drm *directoryReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error { + // Read the map length + l, err := dec.DecodeMapLen() + if err != nil { + return err + } + + // Initialize the map if it's nil + if *drm == nil { + *drm = make(directoryReferenceMap, l) + } + + // Decode each key-value pair + for i := 0; i < l; i++ { + key, err := dec.DecodeString() + if err != nil { + return err + } + var value DirectoryReference + if err := dec.Decode(&value); err != nil { + return err + } + (*drm)[key] = value + } + + return nil +} + +func (frm fileReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error { + // First, encode the length of the map + if err := enc.EncodeMapLen(len(frm)); err != nil { + return err + } + + // Then, encode each key-value pair + for k, v := range frm { + if err := enc.EncodeString(k); err != nil { + return err + } + if err := enc.Encode(&v); err != nil { // Assuming DirectoryReference can be encoded by msgpack + return err + } + } + + return nil +} + +func (drm *fileReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error { + // Read the map length + l, err := dec.DecodeMapLen() + if err != nil { + return err + } + + // Initialize the map if it's nil + if *drm == nil { + *drm = make(fileReferenceMap, l) + } + + // Decode each key-value pair + for i := 0; i < l; i++ { + key, err := dec.DecodeString() + if err != nil { + return err + } + var value FileReference + if err := dec.Decode(&value); err != nil { + return err + } + (*drm)[key] = value + } + + return nil +}