pkger/pkging/mem/embed.go

43 lines
764 B
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-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()
pkg := &Pkger{}
if err := json.NewDecoder(gz).Decode(pkg); err != nil {
return nil, err
}
return pkg, nil
}