From 86547c5019bf82a1abe994b45b333ab23b7fe310 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 25 Oct 2019 10:19:27 -0400 Subject: [PATCH] do run run --- apply.go | 1 + apply_debug.go | 26 ---------- pkging/embed/embed.go | 86 ++++++++++++++++++--------------- pkging/embed/embed_test.go | 22 +++++++++ pkging/mem/embed.go | 97 +++++++++++++++++++++++++------------- pkging/mem/mem.go | 71 ---------------------------- pkging/pkgutil/stuff.go | 18 +++++-- 7 files changed, 147 insertions(+), 174 deletions(-) delete mode 100644 apply_debug.go create mode 100644 pkging/embed/embed_test.go diff --git a/apply.go b/apply.go index 733bcb2..fcf954f 100644 --- a/apply.go +++ b/apply.go @@ -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() diff --git a/apply_debug.go b/apply_debug.go deleted file mode 100644 index f2db6a4..0000000 --- a/apply_debug.go +++ /dev/null @@ -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 -} diff --git a/pkging/embed/embed.go b/pkging/embed/embed.go index 187202a..29dfdf5 100644 --- a/pkging/embed/embed.go +++ b/pkging/embed/embed.go @@ -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 -} diff --git a/pkging/embed/embed_test.go b/pkging/embed/embed_test.go new file mode 100644 index 0000000..5b7f855 --- /dev/null +++ b/pkging/embed/embed_test.go @@ -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) +} diff --git a/pkging/mem/embed.go b/pkging/mem/embed.go index 27a8617..a7086ed 100644 --- a/pkging/mem/embed.go +++ b/pkging/mem/embed.go @@ -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 + } + 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, } - if err := gz.Close(); err != nil { - return nil, err - } - s := hex.EncodeToString(bb.Bytes()) - return []byte(s), nil + 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)) - if err != nil { - return err + p.Here = y.Here + p.infos = &maps.Infos{} + for k, v := range y.Infos { + p.infos.Store(k, v) } - defer gz.Close() - p := &Pkger{} - if err := json.NewDecoder(gz).Decode(p); err != nil { - return err + 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) } - (*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 diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index 7b5766b..6345cb9 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -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) diff --git a/pkging/pkgutil/stuff.go b/pkging/pkgutil/stuff.go index d52ef44..c580c4e 100644 --- a/pkging/pkgutil/stuff.go +++ b/pkging/pkgutil/stuff.go @@ -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() - if err != nil { - return err - } - _, err = w.Write(b) } + + b, err := pkg.MarshalJSON() + if err != nil { + return err + } + + b, err = embed.Encode(b) + if err != nil { + return err + } + + _, err = w.Write(b) return nil }