feat: implement EncodeMsgpack and DecodeMsgpack for WebAppMetadata
This commit is contained in:
parent
04fb3f155a
commit
ea60d8f0cf
|
@ -1,7 +1,18 @@
|
|||
package metadata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/serialize"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"github.com/samber/lo"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var (
|
||||
_ Metadata = (*WebAppMetadata)(nil)
|
||||
_ Metadata = (*WebAppMetadata)(nil)
|
||||
_ SerializableMetadata = (*WebAppMetadata)(nil)
|
||||
)
|
||||
|
||||
type WebAppMetadata struct {
|
||||
|
@ -25,3 +36,103 @@ func NewWebAppMetadata(name string, tryFiles []string, extraMetadata ExtraMetada
|
|||
func NewEmptyWebAppMetadata() *WebAppMetadata {
|
||||
return &WebAppMetadata{}
|
||||
}
|
||||
|
||||
func (wm *WebAppMetadata) EncodeMsgpack(enc *msgpack.Encoder) error {
|
||||
err := serialize.InitMarshaller(types.MetadataTypeWebApp, enc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
keys := lo.Keys[string, WebAppMetadataFileReference](wm.Paths)
|
||||
sort.Strings(keys)
|
||||
|
||||
paths := make([]WebAppMetadataFileReference, len(wm.Paths))
|
||||
|
||||
for i, v := range keys {
|
||||
paths[i] = wm.Paths[v]
|
||||
}
|
||||
|
||||
items := make([]interface{}, 5)
|
||||
|
||||
items[0] = wm.Name
|
||||
items[1] = wm.TryFiles
|
||||
items[2] = wm.ErrorPages
|
||||
items[3] = paths
|
||||
items[4] = wm.ExtraMetadata
|
||||
|
||||
return enc.Encode(items)
|
||||
}
|
||||
|
||||
func (wm *WebAppMetadata) DecodeMsgpack(dec *msgpack.Decoder) error {
|
||||
err := serialize.InitUnmarshaller(types.MetadataTypeWebApp, dec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
val, err := dec.DecodeArrayLen()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if val != 5 {
|
||||
return errors.New(" Corrupted metadata")
|
||||
}
|
||||
|
||||
for i := 0; i < val; i++ {
|
||||
switch i {
|
||||
case 0:
|
||||
wm.Name, err = dec.DecodeString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case 1:
|
||||
err = dec.Decode(&wm.TryFiles)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case 2:
|
||||
err = dec.Decode(&wm.ErrorPages)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case 3:
|
||||
paths, err := dec.DecodeSlice()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
wm.Paths = make(map[string]WebAppMetadataFileReference, len(paths))
|
||||
|
||||
for _, v := range paths {
|
||||
path := v.([]interface{})
|
||||
parsedCid, err := encoding.CIDFromBytes(path[1].([]byte))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contentType := ""
|
||||
|
||||
if path[2] != nil {
|
||||
contentType = path[2].(string)
|
||||
}
|
||||
|
||||
wm.Paths[path[0].(string)] = WebAppMetadataFileReference{
|
||||
Cid: parsedCid,
|
||||
ContentType: contentType,
|
||||
}
|
||||
}
|
||||
|
||||
case 4:
|
||||
err = dec.Decode(&wm.ExtraMetadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errors.New(" Corrupted metadata")
|
||||
}
|
||||
}
|
||||
|
||||
wm.Type = "web_app"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue