libs5-go/metadata/file_reference.go

179 lines
3.6 KiB
Go
Raw Normal View History

2024-01-04 13:20:37 +00:00
package metadata
import (
"github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/vmihailenco/msgpack/v5"
)
2024-01-04 13:20:37 +00:00
var _ SerializableMetadata = (*FileReference)(nil)
var _ SerializableMetadata = (*FileHistoryMap)(nil)
var _ SerializableMetadata = (*ExtMap)(nil)
type FileHistoryMap struct {
linkedhashmap.Map
}
type ExtMap struct {
linkedhashmap.Map
}
2024-01-04 13:20:37 +00:00
func NewExtMap() ExtMap {
return ExtMap{*linkedhashmap.New()}
}
func NewFileHistoryMap() FileHistoryMap {
return FileHistoryMap{*linkedhashmap.New()}
}
2024-01-04 13:20:37 +00:00
type FileReference struct {
Name string `json:"name"`
2024-01-17 19:12:18 +00:00
Created uint64 `json:"created"`
Version uint64 `json:"version"`
File *FileVersion `json:"file"`
Ext ExtMap `json:"ext"`
History FileHistoryMap `json:"history"`
MimeType string `json:"mimeType"`
URI string `json:"uri,omitempty"`
Key string `json:"key,omitempty"`
2024-01-04 13:20:37 +00:00
}
func NewFileReference(name string, created, version uint64, file *FileVersion, ext ExtMap, history FileHistoryMap, mimeType string) *FileReference {
2024-01-04 13:20:37 +00:00
return &FileReference{
Name: name,
Created: created,
Version: version,
File: file,
Ext: ext,
History: history,
MimeType: mimeType,
URI: "",
Key: "",
}
}
func (fr *FileReference) Modified() int {
return fr.File.Ts
}
func (fr *FileReference) EncodeMsgpack(enc *msgpack.Encoder) error {
tempMap := &fileReferenceSerializationMap{*linkedhashmap.New()}
tempMap.Put(1, fr.Name)
tempMap.Put(2, fr.Created)
tempMap.Put(4, fr.File)
tempMap.Put(5, fr.Version)
2024-01-04 13:20:37 +00:00
if fr.MimeType != "" {
tempMap.Put(6, fr.MimeType)
2024-01-04 13:20:37 +00:00
}
if !fr.Ext.Empty() {
tempMap.Put(7, fr.Ext)
2024-01-04 13:20:37 +00:00
}
if !fr.History.Empty() {
tempMap.Put(8, fr.History)
2024-01-04 13:20:37 +00:00
}
2024-01-04 18:48:17 +00:00
return enc.Encode(tempMap)
2024-01-04 13:20:37 +00:00
}
func (fr *FileReference) DecodeMsgpack(dec *msgpack.Decoder) error {
mapLen, err := dec.DecodeMapLen()
if err != nil {
return err
}
hasExt := false
hasHistory := false
2024-01-04 13:20:37 +00:00
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeInt8()
if err != nil {
return err
}
switch key {
case int8(1):
err := dec.Decode(&fr.Name)
if err != nil {
return err
}
case int8(2):
err := dec.Decode(&fr.Created)
if err != nil {
return err
}
case int8(4):
err := dec.Decode(&fr.File)
if err != nil {
return err
}
case int8(5):
val, err := dec.DecodeInt()
if err != nil {
return err
}
2024-01-17 19:12:18 +00:00
fr.Version = uint64(val)
2024-01-04 13:20:37 +00:00
case int8(6):
err := dec.Decode(&fr.MimeType)
if err != nil {
return err
}
case int8(7):
err := dec.Decode(&fr.Ext)
if err != nil {
return err
}
hasExt = true
2024-01-04 13:20:37 +00:00
case int8(8):
err := dec.Decode(&fr.History)
2024-01-04 13:20:37 +00:00
if err != nil {
return err
}
hasHistory = true
2024-01-04 13:20:37 +00:00
}
}
if !hasExt {
fr.Ext = ExtMap{*linkedhashmap.New()}
}
if !hasHistory {
fr.History = FileHistoryMap{*linkedhashmap.New()}
}
2024-01-04 13:20:37 +00:00
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)
}