libs5-go/metadata/file_reference.go

171 lines
3.4 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
type FileReference struct {
Name string `json:"name"`
Created int `json:"created"`
Version int `json:"version"`
File *FileVersion `json:"file"`
Ext extMap `json:"ext"`
History fileHistoryMap `json:"history"`
MimeType string `json:"mimeType"`
URI string `json:"uri"`
Key string `json:"key"`
2024-01-04 13:20:37 +00:00
}
func NewFileReference(name string, created, version int, 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 := 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
}
fr.Version = val
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)
}