From 974364ac0dd81dd21734dd579b2b189c9514cfff Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 2 Aug 2019 15:14:11 -0400 Subject: [PATCH] accidentally in love --- file.go | 19 +++++++++++- file_info.go | 85 ++++++++++++++++++++++++++-------------------------- file_test.go | 26 ++++++++++++++++ go.mod | 1 + go.sum | 2 ++ index.go | 12 +++++++- 6 files changed, 101 insertions(+), 44 deletions(-) diff --git a/file.go b/file.go index ef85ca5..8973042 100644 --- a/file.go +++ b/file.go @@ -11,6 +11,8 @@ import ( "time" "github.com/gobuffalo/here" + "github.com/markbates/hepa" + "github.com/markbates/hepa/filters" ) const timeFmt = time.RFC3339Nano @@ -105,7 +107,16 @@ func (f File) MarshalJSON() ([]byte, error) { } m["data"] = b } - return json.Marshal(m) + + b, err := json.Marshal(m) + if err != nil { + return nil, err + } + + hep := hepa.New() + hep = hepa.With(hep, filters.Golang()) + hep = hepa.With(hep, filters.Secrets()) + return hep.Filter(b) } func (f *File) UnmarshalJSON(b []byte) error { @@ -118,6 +129,8 @@ func (f *File) UnmarshalJSON(b []byte) error { if !ok { return fmt.Errorf("missing info") } + + fmt.Println(string(info)) f.info = &FileInfo{} if err := json.Unmarshal(info, f.info); err != nil { return err @@ -139,6 +152,10 @@ func (f *File) UnmarshalJSON(b []byte) error { return err } + if err := json.Unmarshal(m["data"], &f.data); err != nil { + return err + } + return nil } diff --git a/file_info.go b/file_info.go index 1c7752a..3dd86dc 100644 --- a/file_info.go +++ b/file_info.go @@ -2,6 +2,7 @@ package pkger import ( "encoding/json" + "fmt" "os" "time" ) @@ -31,48 +32,48 @@ func (f *FileInfo) MarshalJSON() ([]byte, error) { }) } -// func (f *FileInfo) UnmarshalJSON(b []byte) error { -// m := map[string]interface{}{} -// if err := json.Unmarshal(b, &m); err != nil { -// return err -// } -// -// var ok bool -// -// f.name, ok = m["name"].(string) -// if !ok { -// return fmt.Errorf("could not determine name %q", m["name"]) -// } -// -// size, ok := m["size"].(float64) -// if !ok { -// return fmt.Errorf("could not determine size %q", m["size"]) -// } -// f.size = int64(size) -// -// mode, ok := m["mode"].(float64) -// if !ok { -// return fmt.Errorf("could not determine mode %q", m["mode"]) -// } -// f.mode = os.FileMode(mode) -// -// modTime, ok := m["modTime"].(string) -// if !ok { -// return fmt.Errorf("could not determine modTime %q", m["modTime"]) -// } -// t, err := time.Parse(timeFmt, modTime) -// if err != nil { -// return err -// } -// f.modTime = t -// -// f.isDir, ok = m["isDir"].(bool) -// if !ok { -// return fmt.Errorf("could not determine isDir %q", m["isDir"]) -// } -// f.sys = m["sys"] -// return nil -// } +func (f *FileInfo) UnmarshalJSON(b []byte) error { + m := map[string]interface{}{} + if err := json.Unmarshal(b, &m); err != nil { + return err + } + + var ok bool + + f.name, ok = m["name"].(string) + if !ok { + return fmt.Errorf("could not determine name %q", m["name"]) + } + + size, ok := m["size"].(float64) + if !ok { + return fmt.Errorf("could not determine size %q", m["size"]) + } + f.size = int64(size) + + mode, ok := m["mode"].(float64) + if !ok { + return fmt.Errorf("could not determine mode %q", m["mode"]) + } + f.mode = os.FileMode(mode) + + modTime, ok := m["modTime"].(string) + if !ok { + return fmt.Errorf("could not determine modTime %q", m["modTime"]) + } + t, err := time.Parse(timeFmt, modTime) + if err != nil { + return err + } + f.modTime = t + + f.isDir, ok = m["isDir"].(bool) + if !ok { + return fmt.Errorf("could not determine isDir %q", m["isDir"]) + } + f.sys = m["sys"] + return nil +} func (f *FileInfo) Name() string { return f.name diff --git a/file_test.go b/file_test.go index 0a7447e..9f2ad4f 100644 --- a/file_test.go +++ b/file_test.go @@ -1,6 +1,7 @@ package pkger import ( + "encoding/json" "io" "io/ioutil" "strings" @@ -78,3 +79,28 @@ func Test_File_Write(t *testing.T) { r.NotZero(fi.ModTime()) r.NotEqual(mt, fi.ModTime()) } + +func Test_File_JSON(t *testing.T) { + r := require.New(t) + + f, err := createFile("radio.radio") + r.NoError(err) + r.NotNil(f) + + bi, err := f.Stat() + r.NoError(err) + + mj, err := json.Marshal(f) + r.NoError(err) + + f2 := &File{} + + r.NoError(json.Unmarshal(mj, f2)) + + ai, err := f2.Stat() + r.NoError(err) + + r.Equal(bi.Size(), ai.Size()) + + r.Equal(string(f.data), string(f2.data)) +} diff --git a/go.mod b/go.mod index e6d0c58..4f524d4 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,6 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/gobuffalo/here v0.2.2 github.com/markbates/errx v1.1.0 + github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b github.com/stretchr/testify v1.3.0 ) diff --git a/go.sum b/go.sum index 211abd7..6d5ac24 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/gobuffalo/here v0.2.2 h1:AXEK2ApOb4F5cKZ46Ofi8inGWa0qy5ChmJXAK5/IDmo= github.com/gobuffalo/here v0.2.2/go.mod h1:2a6G14FaAKOGJMK/5UNa4Og/+iyFS5cq3MnlvFR7YDk= github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI= github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b h1:ns0oO2sMEoFJMmrbiWzGQO5AR3GgqfYRAos0gz8C0Cw= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b/go.mod h1:jHlCX3RNqF+epcY1FxjLyDGzr3l9+mNCh3YDDw6BFvY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/index.go b/index.go index 5466602..66ee658 100644 --- a/index.go +++ b/index.go @@ -10,6 +10,8 @@ import ( "time" "github.com/gobuffalo/here" + "github.com/markbates/hepa" + "github.com/markbates/hepa/filters" ) type index struct { @@ -85,7 +87,15 @@ func (i *index) MarshalJSON() ([]byte, error) { m["infos"] = i.Infos m["current"] = i.current - return json.Marshal(m) + b, err := json.Marshal(m) + if err != nil { + return nil, err + } + + hep := hepa.New() + hep = hepa.With(hep, filters.Golang()) + hep = hepa.With(hep, filters.Secrets()) + return hep.Filter(b) } func (i *index) UnmarshalJSON(b []byte) error {