do run run

This commit is contained in:
Mark Bates 2019-10-25 10:19:27 -04:00
parent 27560b91c3
commit 86547c5019
7 changed files with 147 additions and 174 deletions

View File

@ -11,6 +11,7 @@ import (
// for layering of pkging.Pkger implementations.
func Apply(pkg pkging.Pkger, err error) error {
if err != nil {
panic(err)
return err
}
gil.Lock()

View File

@ -1,26 +0,0 @@
// +build debug
package pkger
import (
"os"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/pkgutil"
)
// Apply will wrap the current implementation
// of pkger.Pkger with the new pkg. This allows
// for layering of pkging.Pkger implementations.
func Apply(pkg pkging.Pkger, err error) error {
gil.Lock()
defer gil.Unlock()
if err != nil {
return err
}
if err := pkgutil.Dump(os.Stdout, pkg); err != nil {
return err
}
current = pkging.Wrap(current, pkg)
return nil
}

View File

@ -4,17 +4,59 @@ import (
"bytes"
"compress/gzip"
"encoding/hex"
"encoding/json"
"io"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
)
type Embedder interface {
MarshalEmbed() ([]byte, error)
func Decode(src []byte) ([]byte, error) {
dst := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(dst, src)
if err != nil {
return nil, err
}
r, err := gzip.NewReader(bytes.NewReader(dst))
if err != nil {
return nil, err
}
bb := &bytes.Buffer{}
if _, err := io.Copy(bb, r); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
type Unembedder interface {
UnmarshalEmbed([]byte) error
func Encode(b []byte) ([]byte, error) {
bb := &bytes.Buffer{}
gz := gzip.NewWriter(bb)
if _, err := gz.Write(b); err != nil {
return nil, err
}
if err := gz.Flush(); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
hep := hepa.New()
hep = hepa.With(hep, filters.Home())
hep = hepa.With(hep, filters.Golang())
b, err := hep.Filter(bb.Bytes())
if err != nil {
return nil, err
}
s := hex.EncodeToString(b)
return []byte(s), nil
}
type Data struct {
@ -22,37 +64,3 @@ type Data struct {
Files map[string]File `json:"files"`
Here here.Info `json:"here"`
}
func (d *Data) MarshalEmbed() ([]byte, error) {
bb := &bytes.Buffer{}
gz := gzip.NewWriter(bb)
defer gz.Close()
if err := json.NewEncoder(gz).Encode(d); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
s := hex.EncodeToString(bb.Bytes())
return []byte(s), nil
}
func (d *Data) 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 := &Data{}
if err := json.NewDecoder(gz).Decode(p); err != nil {
return err
}
(*d) = *p
return nil
}

View File

@ -0,0 +1,22 @@
package embed
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_Encoding(t *testing.T) {
r := require.New(t)
in := []byte("hi\n")
enc, err := Encode(in)
r.NoError(err)
r.NotEqual(in, enc)
dec, err := Decode(enc)
r.NoError(err)
r.Equal(in, dec)
}

View File

@ -1,60 +1,91 @@
package mem
import (
"bytes"
"compress/gzip"
"encoding/hex"
"encoding/json"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/maps"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/embed"
)
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
// MarshalJSON creates a fully re-hydratable JSON representation of *Pkger
func (p *Pkger) MarshalJSON() ([]byte, error) {
files := map[string]embed.File{}
p.files.Range(func(key here.Path, file pkging.File) bool {
f, ok := file.(*File)
if !ok {
return true
}
if err := gz.Close(); err != nil {
return nil, err
ef := embed.File{
Info: f.info,
Here: f.Here,
Path: f.path,
Parent: f.parent,
Data: f.data,
}
s := hex.EncodeToString(bb.Bytes())
return []byte(s), nil
files[key.String()] = ef
return true
})
infos := map[string]here.Info{}
p.infos.Range(func(key string, info here.Info) bool {
infos[key] = info
return true
})
ed := embed.Data{
Infos: infos,
Files: files,
Here: p.Here,
}
return json.Marshal(ed)
}
func (pkg *Pkger) UnmarshalEmbed(in []byte) error {
b := make([]byte, len(in))
if _, err := hex.Decode(b, in); err != nil {
// UnmarshalJSON re-hydrates the *Pkger
func (p *Pkger) UnmarshalJSON(b []byte) error {
y := &embed.Data{
Infos: map[string]here.Info{},
Files: map[string]embed.File{},
}
if err := json.Unmarshal(b, &y); err != nil {
return err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
p.Here = y.Here
p.infos = &maps.Infos{}
for k, v := range y.Infos {
p.infos.Store(k, v)
}
p.files = &maps.Files{}
for k, v := range y.Files {
pt, err := p.Parse(k)
if err != nil {
return err
}
defer gz.Close()
p := &Pkger{}
if err := json.NewDecoder(gz).Decode(p); err != nil {
return err
f := &File{
Here: v.Here,
info: v.Info,
path: v.Path,
data: v.Data,
parent: v.Parent,
}
p.files.Store(pt, f)
}
(*pkg) = *p
return nil
}
func UnmarshalEmbed(in []byte) (*Pkger, error) {
b := make([]byte, len(in))
if _, err := hex.Decode(b, in); err != nil {
return nil, err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
b, err := embed.Decode(in)
if err != nil {
return nil, err
}
defer gz.Close()
p := &Pkger{}
if err := json.NewDecoder(gz).Decode(p); err != nil {
if err := json.Unmarshal(b, p); err != nil {
return nil, err
}
return p, nil

View File

@ -1,7 +1,6 @@
package mem
import (
"encoding/json"
"fmt"
"os"
"path"
@ -12,7 +11,6 @@ import (
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/maps"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/embed"
)
var _ pkging.Pkger = &Pkger{}
@ -34,75 +32,6 @@ type Pkger struct {
files *maps.Files
}
// MarshalJSON creates a fully re-hydratable JSON representation of *Pkger
func (p *Pkger) MarshalJSON() ([]byte, error) {
files := map[string]embed.File{}
p.files.Range(func(key here.Path, file pkging.File) bool {
f, ok := file.(*File)
if !ok {
return true
}
ef := embed.File{
Info: f.info,
Here: f.Here,
Path: f.path,
Parent: f.parent,
Data: f.data,
}
files[key.String()] = ef
return true
})
infos := map[string]here.Info{}
p.infos.Range(func(key string, info here.Info) bool {
infos[key] = info
return true
})
ed := embed.Data{
Infos: infos,
Files: files,
Here: p.Here,
}
return json.Marshal(ed)
}
// UnmarshalJSON re-hydrates the *Pkger
func (p *Pkger) UnmarshalJSON(b []byte) error {
y := &embed.Data{
Infos: map[string]here.Info{},
Files: map[string]embed.File{},
}
if err := json.Unmarshal(b, &y); err != nil {
return err
}
p.Here = y.Here
p.infos = &maps.Infos{}
for k, v := range y.Infos {
p.infos.Store(k, v)
}
p.files = &maps.Files{}
for k, v := range y.Files {
pt, err := p.Parse(k)
if err != nil {
return err
}
f := &File{
Here: v.Here,
info: v.Info,
path: v.Path,
data: v.Data,
parent: v.Parent,
}
p.files.Store(pt, f)
}
return nil
}
// Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
func (f *Pkger) Abs(p string) (string, error) {
pt, err := f.Parse(p)

View File

@ -6,6 +6,7 @@ import (
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkging/embed"
"github.com/markbates/pkger/pkging/mem"
)
@ -38,11 +39,18 @@ func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
if err != nil {
return err
}
b, err := pkg.MarshalEmbed()
}
b, err := pkg.MarshalJSON()
if err != nil {
return err
}
_, err = w.Write(b)
b, err = embed.Encode(b)
if err != nil {
return err
}
_, err = w.Write(b)
return nil
}