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. // for layering of pkging.Pkger implementations.
func Apply(pkg pkging.Pkger, err error) error { func Apply(pkg pkging.Pkger, err error) error {
if err != nil { if err != nil {
panic(err)
return err return err
} }
gil.Lock() 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" "bytes"
"compress/gzip" "compress/gzip"
"encoding/hex" "encoding/hex"
"encoding/json" "io"
"github.com/markbates/pkger/here" "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 { func Decode(src []byte) ([]byte, error) {
MarshalEmbed() ([]byte, error) dst := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(dst, src)
if err != nil {
return nil, err
} }
type Unembedder interface { r, err := gzip.NewReader(bytes.NewReader(dst))
UnmarshalEmbed([]byte) error if err != nil {
return nil, err
}
bb := &bytes.Buffer{}
if _, err := io.Copy(bb, r); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
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 { type Data struct {
@ -22,37 +64,3 @@ type Data struct {
Files map[string]File `json:"files"` Files map[string]File `json:"files"`
Here here.Info `json:"here"` 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 package mem
import ( import (
"bytes"
"compress/gzip"
"encoding/hex"
"encoding/json" "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) { // MarshalJSON creates a fully re-hydratable JSON representation of *Pkger
bb := &bytes.Buffer{} func (p *Pkger) MarshalJSON() ([]byte, error) {
gz := gzip.NewWriter(bb) files := map[string]embed.File{}
defer gz.Close()
if err := json.NewEncoder(gz).Encode(pkg); err != nil { p.files.Range(func(key here.Path, file pkging.File) bool {
return nil, err f, ok := file.(*File)
if !ok {
return true
} }
if err := gz.Close(); err != nil { ef := embed.File{
return nil, err Info: f.info,
Here: f.Here,
Path: f.path,
Parent: f.parent,
Data: f.data,
} }
s := hex.EncodeToString(bb.Bytes()) files[key.String()] = ef
return []byte(s), nil 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 { // UnmarshalJSON re-hydrates the *Pkger
b := make([]byte, len(in)) func (p *Pkger) UnmarshalJSON(b []byte) error {
if _, err := hex.Decode(b, in); err != nil { y := &embed.Data{
Infos: map[string]here.Info{},
Files: map[string]embed.File{},
}
if err := json.Unmarshal(b, &y); err != nil {
return err 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 { if err != nil {
return err return err
} }
defer gz.Close()
p := &Pkger{} f := &File{
if err := json.NewDecoder(gz).Decode(p); err != nil { Here: v.Here,
return err info: v.Info,
path: v.Path,
data: v.Data,
parent: v.Parent,
}
p.files.Store(pt, f)
} }
(*pkg) = *p
return nil return nil
} }
func UnmarshalEmbed(in []byte) (*Pkger, error) { func UnmarshalEmbed(in []byte) (*Pkger, error) {
b := make([]byte, len(in)) b, err := embed.Decode(in)
if _, err := hex.Decode(b, in); err != nil {
return nil, err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer gz.Close()
p := &Pkger{} p := &Pkger{}
if err := json.NewDecoder(gz).Decode(p); err != nil { if err := json.Unmarshal(b, p); err != nil {
return nil, err return nil, err
} }
return p, nil return p, nil

View File

@ -1,7 +1,6 @@
package mem package mem
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"path" "path"
@ -12,7 +11,6 @@ import (
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/maps" "github.com/markbates/pkger/internal/maps"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/embed"
) )
var _ pkging.Pkger = &Pkger{} var _ pkging.Pkger = &Pkger{}
@ -34,75 +32,6 @@ type Pkger struct {
files *maps.Files 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. // 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) { func (f *Pkger) Abs(p string) (string, error) {
pt, err := f.Parse(p) pt, err := f.Parse(p)

View File

@ -6,6 +6,7 @@ import (
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkging/embed"
"github.com/markbates/pkger/pkging/mem" "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 { if err != nil {
return err return err
} }
b, err := pkg.MarshalEmbed() }
b, err := pkg.MarshalJSON()
if err != nil { if err != nil {
return err return err
} }
_, err = w.Write(b)
b, err = embed.Encode(b)
if err != nil {
return err
} }
_, err = w.Write(b)
return nil return nil
} }