2024-01-04 13:20:37 +00:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
2024-01-05 08:46:08 +00:00
|
|
|
"encoding/json"
|
2024-01-04 13:20:37 +00:00
|
|
|
"errors"
|
2024-01-05 08:46:08 +00:00
|
|
|
"fmt"
|
2024-01-04 13:20:37 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/serialize"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-05 08:46:08 +00:00
|
|
|
"github.com/emirpasic/gods/maps/linkedhashmap"
|
2024-01-04 13:20:37 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
type directoryReferenceMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
|
|
|
type fileReferenceMap struct {
|
|
|
|
linkedhashmap.Map
|
|
|
|
}
|
2024-01-04 17:41:39 +00:00
|
|
|
|
2024-01-04 13:20:37 +00:00
|
|
|
type DirectoryMetadata struct {
|
2024-01-04 17:41:39 +00:00
|
|
|
Details DirectoryMetadataDetails `json:"details"`
|
|
|
|
Directories directoryReferenceMap `json:"directories"`
|
|
|
|
Files fileReferenceMap `json:"files"`
|
|
|
|
ExtraMetadata ExtraMetadata `json:"extraMetadata"`
|
2024-01-04 13:20:37 +00:00
|
|
|
BaseMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ SerializableMetadata = (*DirectoryMetadata)(nil)
|
2024-01-04 17:41:39 +00:00
|
|
|
var _ SerializableMetadata = (*directoryReferenceMap)(nil)
|
2024-01-04 13:20:37 +00:00
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
func NewDirectoryMetadata(details DirectoryMetadataDetails, directories directoryReferenceMap, files fileReferenceMap, extraMetadata ExtraMetadata) *DirectoryMetadata {
|
2024-01-04 13:20:37 +00:00
|
|
|
dirMetadata := &DirectoryMetadata{
|
|
|
|
Details: details,
|
|
|
|
Directories: directories,
|
|
|
|
Files: files,
|
|
|
|
ExtraMetadata: extraMetadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
dirMetadata.Type = "directory"
|
|
|
|
return dirMetadata
|
|
|
|
}
|
|
|
|
func (dm *DirectoryMetadata) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
err := serialize.InitMarshaller(types.MetadataTypeDirectory, enc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
items := make([]interface{}, 4)
|
|
|
|
|
|
|
|
items[0] = dm.Details
|
|
|
|
items[1] = dm.Directories
|
|
|
|
items[2] = dm.Files
|
|
|
|
items[3] = dm.ExtraMetadata.Data
|
|
|
|
|
|
|
|
return enc.Encode(items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DirectoryMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
|
|
|
|
err := serialize.InitUnmarshaller(types.MetadataTypeDirectory, dec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val, err := dec.DecodeArrayLen()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if val != 4 {
|
|
|
|
return errors.New(" Corrupted metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < val; i++ {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
err = dec.Decode(&dm.Details)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
err = dec.Decode(&dm.Directories)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
err = dec.Decode(&dm.Files)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
intMap, err := decodeIntMap(dec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dm.ExtraMetadata.Data = intMap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dm.Type = "directory"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-04 17:41:39 +00:00
|
|
|
func (drm directoryReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
2024-01-05 08:46:08 +00:00
|
|
|
return marshallMapMsgpack(enc, &drm.Map)
|
|
|
|
}
|
2024-01-04 17:41:39 +00:00
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
func (drm *directoryReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error {
|
|
|
|
return unmarshalMapMsgpack(dec, &drm.Map, &DirectoryReference{})
|
|
|
|
}
|
2024-01-04 17:41:39 +00:00
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
func (frm fileReferenceMap) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
return marshallMapMsgpack(enc, &frm.Map)
|
2024-01-04 17:41:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
func (frm *fileReferenceMap) DecodeMsgpack(dec *msgpack.Decoder) error {
|
|
|
|
return unmarshalMapMsgpack(dec, &frm.Map, &FileReference{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (frm *fileReferenceMap) UnmarshalJSON(bytes []byte) error {
|
|
|
|
createFileInstance := func() interface{} { return &FileReference{} }
|
|
|
|
return unmarshalMapJson(bytes, &frm.Map, createFileInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
type unmarshalNewInstanceFunc func() interface{}
|
|
|
|
|
|
|
|
func (drm *directoryReferenceMap) UnmarshalJSON(bytes []byte) error {
|
|
|
|
createDirInstance := func() interface{} { return &DirectoryReference{} }
|
|
|
|
return unmarshalMapJson(bytes, &drm.Map, createDirInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalMapMsgpack(dec *msgpack.Decoder, m *linkedhashmap.Map, placeholder interface{}) error {
|
|
|
|
*m = *linkedhashmap.New()
|
|
|
|
|
2024-01-04 17:41:39 +00:00
|
|
|
l, err := dec.DecodeMapLen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
key, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 08:46:08 +00:00
|
|
|
|
|
|
|
fmt.Println("dir: ", key)
|
|
|
|
|
|
|
|
switch placeholder.(type) {
|
|
|
|
case *DirectoryReference:
|
|
|
|
var value DirectoryReference
|
|
|
|
if err := dec.Decode(&value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Put(key, value)
|
|
|
|
|
|
|
|
case *FileReference:
|
|
|
|
var file FileReference
|
|
|
|
if err := dec.Decode(&file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Put(key, file)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported type for decoding")
|
2024-01-04 17:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
func marshallMapMsgpack(enc *msgpack.Encoder, m *linkedhashmap.Map) error {
|
2024-01-04 17:41:39 +00:00
|
|
|
// First, encode the length of the map
|
2024-01-05 08:46:08 +00:00
|
|
|
if err := enc.EncodeMapLen(m.Size()); err != nil {
|
2024-01-04 17:41:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
iter := m.Iterator()
|
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key().(string)
|
|
|
|
if err := enc.EncodeString(key); err != nil {
|
2024-01-04 17:41:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-01-05 08:46:08 +00:00
|
|
|
|
|
|
|
value := iter.Value()
|
|
|
|
switch v := value.(type) {
|
|
|
|
case FileReference:
|
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case DirectoryReference:
|
|
|
|
if err := enc.Encode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported type for encoding")
|
2024-01-04 17:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
func unmarshalMapJson(bytes []byte, m *linkedhashmap.Map, newInstance unmarshalNewInstanceFunc) error {
|
|
|
|
*m = *linkedhashmap.New()
|
|
|
|
err := m.FromJSON(bytes)
|
2024-01-04 17:41:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
iter := m.Iterator()
|
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
val := iter.Value()
|
2024-01-04 17:41:39 +00:00
|
|
|
|
2024-01-05 08:46:08 +00:00
|
|
|
instance := newInstance()
|
|
|
|
|
|
|
|
data, err := json.Marshal(val)
|
2024-01-04 17:41:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-05 08:46:08 +00:00
|
|
|
|
|
|
|
err = json.Unmarshal(data, &instance)
|
|
|
|
if err != nil {
|
2024-01-04 17:41:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-01-05 08:46:08 +00:00
|
|
|
|
|
|
|
// Type switch to handle different types
|
|
|
|
switch v := instance.(type) {
|
|
|
|
case *DirectoryReference:
|
|
|
|
m.Put(key, *v)
|
|
|
|
case *FileReference:
|
|
|
|
m.Put(key, *v)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unhandled type: %T", v)
|
|
|
|
}
|
2024-01-04 17:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|