feat: implement EncodeMsgpack and DecodeMsgpack for WebAppMetadata

This commit is contained in:
Derrick Hammer 2024-01-18 12:06:47 -05:00
parent 04fb3f155a
commit ea60d8f0cf
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 112 additions and 1 deletions

View File

@ -1,7 +1,18 @@
package metadata 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 ( var (
_ Metadata = (*WebAppMetadata)(nil) _ Metadata = (*WebAppMetadata)(nil)
_ SerializableMetadata = (*WebAppMetadata)(nil)
) )
type WebAppMetadata struct { type WebAppMetadata struct {
@ -25,3 +36,103 @@ func NewWebAppMetadata(name string, tryFiles []string, extraMetadata ExtraMetada
func NewEmptyWebAppMetadata() *WebAppMetadata { func NewEmptyWebAppMetadata() *WebAppMetadata {
return &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
}