accidentally in love

This commit is contained in:
Mark Bates 2019-08-02 15:14:11 -04:00
parent 71d9708097
commit 974364ac0d
6 changed files with 101 additions and 44 deletions

19
file.go
View File

@ -11,6 +11,8 @@ import (
"time" "time"
"github.com/gobuffalo/here" "github.com/gobuffalo/here"
"github.com/markbates/hepa"
"github.com/markbates/hepa/filters"
) )
const timeFmt = time.RFC3339Nano const timeFmt = time.RFC3339Nano
@ -105,7 +107,16 @@ func (f File) MarshalJSON() ([]byte, error) {
} }
m["data"] = b 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 { func (f *File) UnmarshalJSON(b []byte) error {
@ -118,6 +129,8 @@ func (f *File) UnmarshalJSON(b []byte) error {
if !ok { if !ok {
return fmt.Errorf("missing info") return fmt.Errorf("missing info")
} }
fmt.Println(string(info))
f.info = &FileInfo{} f.info = &FileInfo{}
if err := json.Unmarshal(info, f.info); err != nil { if err := json.Unmarshal(info, f.info); err != nil {
return err return err
@ -139,6 +152,10 @@ func (f *File) UnmarshalJSON(b []byte) error {
return err return err
} }
if err := json.Unmarshal(m["data"], &f.data); err != nil {
return err
}
return nil return nil
} }

View File

@ -2,6 +2,7 @@ package pkger
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"time" "time"
) )
@ -31,48 +32,48 @@ func (f *FileInfo) MarshalJSON() ([]byte, error) {
}) })
} }
// func (f *FileInfo) UnmarshalJSON(b []byte) error { func (f *FileInfo) UnmarshalJSON(b []byte) error {
// m := map[string]interface{}{} m := map[string]interface{}{}
// if err := json.Unmarshal(b, &m); err != nil { if err := json.Unmarshal(b, &m); err != nil {
// return err return err
// } }
//
// var ok bool var ok bool
//
// f.name, ok = m["name"].(string) f.name, ok = m["name"].(string)
// if !ok { if !ok {
// return fmt.Errorf("could not determine name %q", m["name"]) return fmt.Errorf("could not determine name %q", m["name"])
// } }
//
// size, ok := m["size"].(float64) size, ok := m["size"].(float64)
// if !ok { if !ok {
// return fmt.Errorf("could not determine size %q", m["size"]) return fmt.Errorf("could not determine size %q", m["size"])
// } }
// f.size = int64(size) f.size = int64(size)
//
// mode, ok := m["mode"].(float64) mode, ok := m["mode"].(float64)
// if !ok { if !ok {
// return fmt.Errorf("could not determine mode %q", m["mode"]) return fmt.Errorf("could not determine mode %q", m["mode"])
// } }
// f.mode = os.FileMode(mode) f.mode = os.FileMode(mode)
//
// modTime, ok := m["modTime"].(string) modTime, ok := m["modTime"].(string)
// if !ok { if !ok {
// return fmt.Errorf("could not determine modTime %q", m["modTime"]) return fmt.Errorf("could not determine modTime %q", m["modTime"])
// } }
// t, err := time.Parse(timeFmt, modTime) t, err := time.Parse(timeFmt, modTime)
// if err != nil { if err != nil {
// return err return err
// } }
// f.modTime = t f.modTime = t
//
// f.isDir, ok = m["isDir"].(bool) f.isDir, ok = m["isDir"].(bool)
// if !ok { if !ok {
// return fmt.Errorf("could not determine isDir %q", m["isDir"]) return fmt.Errorf("could not determine isDir %q", m["isDir"])
// } }
// f.sys = m["sys"] f.sys = m["sys"]
// return nil return nil
// } }
func (f *FileInfo) Name() string { func (f *FileInfo) Name() string {
return f.name return f.name

View File

@ -1,6 +1,7 @@
package pkger package pkger
import ( import (
"encoding/json"
"io" "io"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -78,3 +79,28 @@ func Test_File_Write(t *testing.T) {
r.NotZero(fi.ModTime()) r.NotZero(fi.ModTime())
r.NotEqual(mt, 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
go.mod
View File

@ -6,5 +6,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobuffalo/here v0.2.2 github.com/gobuffalo/here v0.2.2
github.com/markbates/errx v1.1.0 github.com/markbates/errx v1.1.0
github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b
github.com/stretchr/testify v1.3.0 github.com/stretchr/testify v1.3.0
) )

2
go.sum
View File

@ -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/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 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI=
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -10,6 +10,8 @@ import (
"time" "time"
"github.com/gobuffalo/here" "github.com/gobuffalo/here"
"github.com/markbates/hepa"
"github.com/markbates/hepa/filters"
) )
type index struct { type index struct {
@ -85,7 +87,15 @@ func (i *index) MarshalJSON() ([]byte, error) {
m["infos"] = i.Infos m["infos"] = i.Infos
m["current"] = i.current 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 { func (i *index) UnmarshalJSON(b []byte) error {