forked from mirror/pkger
accidentally in love
This commit is contained in:
parent
71d9708097
commit
974364ac0d
19
file.go
19
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
|
||||
}
|
||||
|
||||
|
|
85
file_info.go
85
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
|
||||
|
|
26
file_test.go
26
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))
|
||||
}
|
||||
|
|
1
go.mod
1
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
|
||||
)
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
12
index.go
12
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 {
|
||||
|
|
Loading…
Reference in New Issue