refactor: add a map wrapper for FileVersionThumbnail and FileVersion serialization

This commit is contained in:
Derrick Hammer 2024-01-05 07:19:31 -05:00
parent 40d7c90595
commit 7ad63aea3a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 34 additions and 15 deletions

View File

@ -18,11 +18,20 @@ type directoryReferenceSerializationMap struct {
linkedhashmap.Map
}
type fileVersionSerializationMap struct {
linkedhashmap.Map
}
type fileVersionThumbnailSerializationMap struct {
linkedhashmap.Map
}
type unmarshalNewInstanceFunc func() interface{}
var _ SerializableMetadata = (*directoryReferenceMap)(nil)
var _ SerializableMetadata = (*fileReferenceMap)(nil)
var _ msgpack.CustomEncoder = (*directoryReferenceSerializationMap)(nil)
var _ msgpack.CustomEncoder = (*fileVersionSerializationMap)(nil)
func unmarshalMapMsgpack(dec *msgpack.Decoder, m *linkedhashmap.Map, placeholder interface{}, intMap bool) error {
*m = *linkedhashmap.New()
@ -200,3 +209,10 @@ func (drm *directoryReferenceMap) UnmarshalJSON(bytes []byte) error {
func (frm directoryReferenceSerializationMap) EncodeMsgpack(enc *msgpack.Encoder) error {
return marshallMapMsgpack(enc, &frm.Map)
}
func (fvs fileVersionSerializationMap) EncodeMsgpack(enc *msgpack.Encoder) error {
return marshallMapMsgpack(enc, &fvs.Map)
}
func (fvts fileVersionThumbnailSerializationMap) EncodeMsgpack(enc *msgpack.Encoder) error {
return marshallMapMsgpack(enc, &fvts.Map)
}

View File

@ -2,6 +2,7 @@ package metadata
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/vmihailenco/msgpack/v5"
)
@ -26,16 +27,17 @@ func NewFileVersion(ts int, encryptedCID *encoding.EncryptedCID, plaintextCID *e
}
func (fv *FileVersion) EncodeMsgpack(enc *msgpack.Encoder) error {
data := map[int]interface{}{
8: fv.Ts,
}
fmap := &fileVersionSerializationMap{*linkedhashmap.New()}
fmap.Put(8, fv.Ts)
if fv.EncryptedCID != nil {
data[1] = fv.EncryptedCID
fmap.Put(1, fv.EncryptedCID)
}
if fv.PlaintextCID != nil {
data[2] = fv.PlaintextCID
fmap.Put(2, fv.PlaintextCID)
}
if len(fv.Hashes) > 0 {
@ -43,14 +45,14 @@ func (fv *FileVersion) EncodeMsgpack(enc *msgpack.Encoder) error {
for i, hash := range fv.Hashes {
hashesData[i] = hash.FullBytes
}
data[9] = hashesData
fmap.Put(9, hashesData)
}
if fv.Thumbnail != nil {
data[10] = fv.Thumbnail
fmap.Put(10, fv.Thumbnail)
}
return enc.Encode(data)
return enc.Encode(fmap)
}
func (fv *FileVersion) DecodeMsgpack(dec *msgpack.Decoder) error {

View File

@ -2,6 +2,7 @@ package metadata
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/vmihailenco/msgpack/v5"
)
@ -22,20 +23,20 @@ func NewFileVersionThumbnail(imageType string, aspectRatio float64, cid *encodin
}
func (fvt *FileVersionThumbnail) EncodeMsgpack(enc *msgpack.Encoder) error {
data := map[int]interface{}{
2: fvt.AspectRatio,
3: fvt.CID.ToBytes(),
}
fmap := &fileVersionThumbnailSerializationMap{*linkedhashmap.New()}
fmap.Put(2, fvt.AspectRatio)
fmap.Put(3, fvt.CID.ToBytes())
if fvt.ImageType != "" {
data[1] = fvt.ImageType
fmap.Put(1, fvt.ImageType)
}
if fvt.Thumbhash != nil {
data[4] = fvt.Thumbhash
fmap.Put(4, fvt.Thumbhash)
}
return enc.Encode(data)
return enc.Encode(fmap)
}
func (fvt *FileVersionThumbnail) DecodeMsgpack(dec *msgpack.Decoder) error {
mapLen, err := dec.DecodeMapLen()