refactor: need to abstract DirectoryMetadata maps to their own types to properly handle msgpack serialization
This commit is contained in:
parent
9f2e17bf54
commit
2ac5ff60be
|
@ -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"`
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue