Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
accidentally in love
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Aug 2, 2019
1 parent 71d9708 commit 974364a
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 44 deletions.
19 changes: 18 additions & 1 deletion file.go
Expand Up @@ -11,6 +11,8 @@ import (
"time"

"github.com/gobuffalo/here"
"github.com/markbates/hepa"
"github.com/markbates/hepa/filters"
)

const timeFmt = time.RFC3339Nano
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}

Expand Down
85 changes: 43 additions & 42 deletions file_info.go
Expand Up @@ -2,6 +2,7 @@ package pkger

import (
"encoding/json"
"fmt"
"os"
"time"
)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions file_test.go
@@ -1,6 +1,7 @@
package pkger

import (
"encoding/json"
"io"
"io/ioutil"
"strings"
Expand Down Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -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=
Expand Down
12 changes: 11 additions & 1 deletion index.go
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/gobuffalo/here"
"github.com/markbates/hepa"
"github.com/markbates/hepa/filters"
)

type index struct {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 974364a

Please sign in to comment.