refactor: use map struct wrappers for and ext, fileHistory, and add serialization methods for them

This commit is contained in:
Derrick Hammer 2024-01-05 06:07:35 -05:00
parent a10bec66ea
commit c328cb1f1b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 53 additions and 12 deletions

View File

@ -1,22 +1,34 @@
package metadata package metadata
import "github.com/vmihailenco/msgpack/v5" import (
"github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/vmihailenco/msgpack/v5"
)
var _ SerializableMetadata = (*FileReference)(nil) var _ SerializableMetadata = (*FileReference)(nil)
var _ SerializableMetadata = (*fileHistoryMap)(nil)
var _ SerializableMetadata = (*extMap)(nil)
type fileHistoryMap struct {
linkedhashmap.Map
}
type extMap struct {
linkedhashmap.Map
}
type FileReference struct { type FileReference struct {
Name string `json:"name"` Name string `json:"name"`
Created int `json:"created"` Created int `json:"created"`
Version int `json:"version"` Version int `json:"version"`
File *FileVersion `json:"file"` File *FileVersion `json:"file"`
Ext map[string]interface{} `json:"ext"` Ext extMap `json:"ext"`
History map[int]*FileVersion `json:"history"` History fileHistoryMap `json:"history"`
MimeType string `json:"mimeType"` MimeType string `json:"mimeType"`
URI string `json:"uri"` URI string `json:"uri"`
Key string `json:"key"` Key string `json:"key"`
} }
func NewFileReference(name string, created, version int, file *FileVersion, ext map[string]interface{}, history map[int]*FileVersion, mimeType string) *FileReference { func NewFileReference(name string, created, version int, file *FileVersion, ext extMap, history fileHistoryMap, mimeType string) *FileReference {
return &FileReference{ return &FileReference{
Name: name, Name: name,
Created: created, Created: created,
@ -130,3 +142,32 @@ func (fr *FileReference) DecodeMsgpack(dec *msgpack.Decoder) error {
} }
return nil return nil
} }
func (ext extMap) EncodeMsgpack(enc *msgpack.Encoder) error {
return marshallMapMsgpack(enc, &ext.Map)
}
func (ext *extMap) DecodeMsgpack(dec *msgpack.Decoder) error {
return unmarshalMapMsgpack(dec, &ext.Map, &extMap{}, true)
}
func (fhm fileHistoryMap) EncodeMsgpack(enc *msgpack.Encoder) error {
return marshallMapMsgpack(enc, &fhm.Map)
}
func (fhm *fileHistoryMap) DecodeMsgpack(dec *msgpack.Decoder) error {
return unmarshalMapMsgpack(dec, &fhm.Map, &extMap{}, false)
}
func (m *fileHistoryMap) UnmarshalJSON(bytes []byte) error {
if string(bytes) == "null" {
m.Map = *linkedhashmap.New()
return nil
}
return m.FromJSON(bytes)
}
func (m *extMap) UnmarshalJSON(bytes []byte) error {
if string(bytes) == "null" {
m.Map = *linkedhashmap.New()
return nil
}
return m.FromJSON(bytes)
}