refactor: need to abstract DirectoryMetadata maps to their own types to properly handle msgpack serialization

This commit is contained in:
Derrick Hammer 2024-01-04 12:41:39 -05:00
parent 9f2e17bf54
commit 2ac5ff60be
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 101 additions and 4 deletions

View File

@ -7,15 +7,19 @@ import (
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
) )
type directoryReferenceMap map[string]DirectoryReference
type fileReferenceMap map[string]FileReference
type DirectoryMetadata struct { type DirectoryMetadata struct {
Details DirectoryMetadataDetails `json:"details"` Details DirectoryMetadataDetails `json:"details"`
Directories map[string]DirectoryReference `json:"directories"` Directories directoryReferenceMap `json:"directories"`
Files map[string]FileReference `json:"files"` Files fileReferenceMap `json:"files"`
ExtraMetadata ExtraMetadata `json:"extraMetadata"` ExtraMetadata ExtraMetadata `json:"extraMetadata"`
BaseMetadata BaseMetadata
} }
var _ SerializableMetadata = (*DirectoryMetadata)(nil) var _ SerializableMetadata = (*DirectoryMetadata)(nil)
var _ SerializableMetadata = (*directoryReferenceMap)(nil)
func NewDirectoryMetadata(details DirectoryMetadataDetails, directories map[string]DirectoryReference, files map[string]FileReference, extraMetadata ExtraMetadata) *DirectoryMetadata { func NewDirectoryMetadata(details DirectoryMetadataDetails, directories map[string]DirectoryReference, files map[string]FileReference, extraMetadata ExtraMetadata) *DirectoryMetadata {
dirMetadata := &DirectoryMetadata{ dirMetadata := &DirectoryMetadata{
@ -90,3 +94,96 @@ func (dm *DirectoryMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
return nil 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
}