pkger/pkging/mem/embed.go

62 lines
1.1 KiB
Go
Raw Normal View History

2019-09-21 19:51:29 +03:00
package mem
import (
"bytes"
"compress/gzip"
"encoding/hex"
"encoding/json"
)
func (pkg *Pkger) MarshalEmbed() ([]byte, error) {
bb := &bytes.Buffer{}
gz := gzip.NewWriter(bb)
defer gz.Close()
if err := json.NewEncoder(gz).Encode(pkg); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
s := hex.EncodeToString(bb.Bytes())
return []byte(s), nil
}
2019-10-21 00:52:16 +03:00
func (pkg *Pkger) UnmarshalEmbed(in []byte) error {
b := make([]byte, len(in))
if _, err := hex.Decode(b, in); err != nil {
return err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return err
}
defer gz.Close()
p := &Pkger{}
if err := json.NewDecoder(gz).Decode(p); err != nil {
return err
}
(*pkg) = *p
return nil
}
2019-10-18 19:01:48 +03:00
func UnmarshalEmbed(in []byte) (*Pkger, error) {
2019-09-21 19:51:29 +03:00
b := make([]byte, len(in))
if _, err := hex.Decode(b, in); err != nil {
return nil, err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
}
defer gz.Close()
2019-10-21 00:52:16 +03:00
p := &Pkger{}
if err := json.NewDecoder(gz).Decode(p); err != nil {
2019-09-21 19:51:29 +03:00
return nil, err
}
2019-10-21 00:52:16 +03:00
return p, nil
2019-09-21 19:51:29 +03:00
}