2024-01-05 09:43:46 +00:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-01-05 12:31:54 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-05 09:43:46 +00:00
|
|
|
"github.com/emirpasic/gods/maps/linkedhashmap"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type directoryReferenceMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
2024-02-29 16:32:13 +00:00
|
|
|
|
|
|
|
func (drm directoryReferenceMap) Items() map[string]*DirectoryReference {
|
|
|
|
files := make(map[string]*DirectoryReference)
|
|
|
|
iter := drm.Iterator()
|
|
|
|
for iter.Next() {
|
|
|
|
files[iter.Key().(string)] = iter.Value().(*DirectoryReference)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2024-03-02 03:21:49 +00:00
|
|
|
func (drm directoryReferenceMap) Get(key string) *DirectoryReference {
|
|
|
|
ret, found := drm.Map.Get(key)
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.(*DirectoryReference)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drm directoryReferenceMap) Has(key string) bool {
|
|
|
|
_, found := drm.Map.Get(key)
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2024-01-05 09:43:46 +00:00
|
|
|
type fileReferenceMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
2024-02-29 16:32:13 +00:00
|
|
|
|
|
|
|
func (drm fileReferenceMap) Items() map[string]*FileReference {
|
|
|
|
files := make(map[string]*FileReference)
|
|
|
|
iter := drm.Iterator()
|
|
|
|
for iter.Next() {
|
|
|
|
files[iter.Key().(string)] = iter.Value().(*FileReference)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2024-03-02 03:23:31 +00:00
|
|
|
func (drm fileReferenceMap) Get(key string) *FileReference {
|
|
|
|
ret, found := drm.Map.Get(key)
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.(*FileReference)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drm fileReferenceMap) Has(key string) bool {
|
|
|
|
_, found := drm.Map.Get(key)
|
|
|
|
return found
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:31:54 +00:00
|
|
|
type fileReferenceSerializationMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
2024-01-05 11:36:18 +00:00
|
|
|
type directoryReferenceSerializationMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:19:31 +00:00
|
|
|
type fileVersionSerializationMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileVersionThumbnailSerializationMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
|
|
|
|
2024-01-05 09:43:46 +00:00
|
|
|
type unmarshalNewInstanceFunc func() interface{}
|
|
|
|
|
|
|
|
var _ SerializableMetadata = (*directoryReferenceMap)(nil)
|
2024-01-05 11:36:18 +00:00
|
|
|
var _ SerializableMetadata = (*fileReferenceMap)(nil)
|
|
|
|
var _ msgpack.CustomEncoder = (*directoryReferenceSerializationMap)(nil)
|
2024-01-05 12:19:31 +00:00
|
|
|
var _ msgpack.CustomEncoder = (*fileVersionSerializationMap)(nil)
|
2024-01-05 12:31:54 +00:00
|
|
|
var _ msgpack.CustomEncoder = (*fileReferenceSerializationMap)(nil)
|
2024-01-05 09:43:46 +00:00
|
|
|
|
2024-01-05 11:05:59 +00:00
|
|
|
func unmarshalMapMsgpack(dec *msgpack.Decoder, m *linkedhashmap.Map, placeholder interface{}, intMap bool) error {
|
2024-01-05 09:43:46 +00:00
|
|
|
*m = *linkedhashmap.New()
|
|
|
|
|
|
|
|
l, err := dec.DecodeMapLen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
2024-01-05 11:05:59 +00:00
|
|
|
var key interface{}
|
|
|
|
if intMap {
|
|
|
|
intKey, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key = intKey
|
|
|
|
} else {
|
|
|
|
strKey, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key = strKey
|
2024-01-05 09:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch placeholder.(type) {
|
|
|
|
case *DirectoryReference:
|
|
|
|
var value DirectoryReference
|
|
|
|
if err := dec.Decode(&value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-03 16:00:34 +00:00
|
|
|
m.Put(key, &value)
|
2024-01-05 09:43:46 +00:00
|
|
|
|
|
|
|
case *FileReference:
|
|
|
|
var file FileReference
|
|
|
|
if err := dec.Decode(&file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-03 16:00:34 +00:00
|
|
|
m.Put(key, &file)
|
2024-01-05 09:43:46 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported type for decoding")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshallMapMsgpack(enc *msgpack.Encoder, m *linkedhashmap.Map) error {
|
|
|
|
// First, encode the length of the map
|
|
|
|
if err := enc.EncodeMapLen(m.Size()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := m.Iterator()
|
|
|
|
for iter.Next() {
|
2024-01-05 11:59:55 +00:00
|
|
|
key := iter.Key()
|
|
|
|
|
|
|
|
// Determine the type of the key and encode it
|
|
|
|
switch k := key.(type) {
|
|
|
|
case string:
|
|
|
|
if err := enc.EncodeString(k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case int:
|
|
|
|
if err := enc.EncodeInt(int64(k)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported key type for encoding")
|
2024-01-05 09:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value := iter.Value()
|
|
|
|
switch v := value.(type) {
|
2024-03-03 16:00:34 +00:00
|
|
|
case *FileReference:
|
2024-01-05 09:43:46 +00:00
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-03 16:00:34 +00:00
|
|
|
case *DirectoryReference:
|
2024-01-05 09:43:46 +00:00
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 11:04:46 +00:00
|
|
|
case string:
|
|
|
|
if err := enc.EncodeString(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case int:
|
|
|
|
if err := enc.EncodeInt(int64(v)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 12:01:27 +00:00
|
|
|
|
|
|
|
case uint64:
|
|
|
|
if err := enc.EncodeInt(int64(v)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Base64UrlBinary:
|
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 11:04:46 +00:00
|
|
|
case FileVersion:
|
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 12:32:34 +00:00
|
|
|
case *FileVersion:
|
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *encoding.CID:
|
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 09:43:46 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported type for encoding")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalMapJson(bytes []byte, m *linkedhashmap.Map, newInstance unmarshalNewInstanceFunc) error {
|
|
|
|
*m = *linkedhashmap.New()
|
|
|
|
err := m.FromJSON(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := m.Iterator()
|
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
val := iter.Value()
|
|
|
|
|
|
|
|
instance := newInstance()
|
|
|
|
|
|
|
|
data, err := json.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 11:05:59 +00:00
|
|
|
err = json.Unmarshal(data, instance)
|
2024-01-05 09:43:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
// kind switch to handle different types
|
2024-01-05 09:43:46 +00:00
|
|
|
switch v := instance.(type) {
|
|
|
|
case *DirectoryReference:
|
|
|
|
m.Put(key, *v)
|
|
|
|
case *FileReference:
|
|
|
|
m.Put(key, *v)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unhandled type: %T", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (drm directoryReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
return marshallMapMsgpack(enc, &drm.Map)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drm *directoryReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error {
|
2024-01-05 11:05:59 +00:00
|
|
|
return unmarshalMapMsgpack(dec, &drm.Map, &DirectoryReference{}, false)
|
2024-01-05 09:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (frm fileReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
return marshallMapMsgpack(enc, &frm.Map)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (frm *fileReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error {
|
2024-01-05 11:05:59 +00:00
|
|
|
return unmarshalMapMsgpack(dec, &frm.Map, &FileReference{}, false)
|
2024-01-05 09:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (frm *fileReferenceMap) UnmarshalJSON(bytes []byte) error {
|
|
|
|
createFileInstance := func() interface{} { return &FileReference{} }
|
|
|
|
return unmarshalMapJson(bytes, &frm.Map, createFileInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drm *directoryReferenceMap) UnmarshalJSON(bytes []byte) error {
|
|
|
|
createDirInstance := func() interface{} { return &DirectoryReference{} }
|
|
|
|
return unmarshalMapJson(bytes, &drm.Map, createDirInstance)
|
|
|
|
}
|
2024-01-05 11:36:18 +00:00
|
|
|
|
|
|
|
func (frm directoryReferenceSerializationMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
return marshallMapMsgpack(enc, &frm.Map)
|
|
|
|
}
|
2024-01-05 12:19:31 +00:00
|
|
|
|
2024-01-05 12:31:54 +00:00
|
|
|
func (frt fileReferenceSerializationMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
return marshallMapMsgpack(enc, &frt.Map)
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:19:31 +00:00
|
|
|
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)
|
|
|
|
}
|