refactor: create a type alias for error pages, so we can manage the msgpack encoding properly

This commit is contained in:
Derrick Hammer 2024-03-03 04:28:45 -05:00
parent f29c485b41
commit 96d99bb533
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 48 additions and 0 deletions

View File

@ -14,8 +14,11 @@ var (
_ Metadata = (*WebAppMetadata)(nil)
_ SerializableMetadata = (*WebAppMetadata)(nil)
_ SerializableMetadata = (*WebAppFileMap)(nil)
_ SerializableMetadata = (*WebAppErrorPages)(nil)
)
type WebAppErrorPages map[int]string
type WebAppMetadata struct {
BaseMetadata
Name string `json:"name"`
@ -231,3 +234,48 @@ func (wafm *WebAppFileMap) DecodeMsgpack(decoder *msgpack.Decoder) error {
return nil
}
func (w WebAppErrorPages) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeMapLen(len(w))
if err != nil {
return err
}
for k, v := range w {
err = enc.EncodeInt(int64(k))
if err != nil {
return err
}
err = enc.EncodeString(v)
if err != nil {
return err
}
}
return nil
}
func (w *WebAppErrorPages) DecodeMsgpack(dec *msgpack.Decoder) error {
mapLen, err := dec.DecodeMapLen()
if err != nil {
return err
}
*w = make(map[int]string, mapLen)
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeInt()
if err != nil {
return err
}
value, err := dec.DecodeString()
if err != nil {
return err
}
(*w)[key] = value
}
return nil
}