diff --git a/file.go b/file.go index 8973042..944a13b 100644 --- a/file.go +++ b/file.go @@ -130,7 +130,6 @@ func (f *File) UnmarshalJSON(b []byte) error { return fmt.Errorf("missing info") } - fmt.Println(string(info)) f.info = &FileInfo{} if err := json.Unmarshal(info, f.info); err != nil { return err diff --git a/files_map.go b/files_map.go index 90eb232..eab1ea1 100644 --- a/files_map.go +++ b/files_map.go @@ -3,6 +3,7 @@ package pkger import ( + "encoding/json" "sort" "sync" ) @@ -12,18 +13,51 @@ import ( // value: *File type filesMap struct { data *sync.Map - init sync.Once } func (m *filesMap) Data() *sync.Map { - m.init.Do(func() { - if m.data == nil { - m.data = &sync.Map{} - } - }) + if m.data == nil { + m.data = &sync.Map{} + } return m.data } +func (m *filesMap) MarshalJSON() ([]byte, error) { + var err error + mm := map[string]interface{}{} + m.data.Range(func(key, value interface{}) bool { + var b []byte + b, err = json.Marshal(key) + if err != nil { + return false + } + mm[string(b)] = value + return true + }) + + if err != nil { + return nil, err + } + + return json.Marshal(mm) +} + +func (m *filesMap) UnmarshalJSON(b []byte) error { + mm := map[string]*File{} + + if err := json.Unmarshal(b, &mm); err != nil { + return err + } + for k, v := range mm { + var pt Path + if err := json.Unmarshal([]byte(k), &pt); err != nil { + return err + } + m.Store(pt, v) + } + return nil +} + // Delete the key from the map func (m *filesMap) Delete(key Path) { m.Data().Delete(key) diff --git a/index.go b/index.go index 66ee658..226dae8 100644 --- a/index.go +++ b/index.go @@ -68,22 +68,7 @@ func (i *index) Create(pt Path) (*File, error) { func (i *index) MarshalJSON() ([]byte, error) { m := map[string]interface{}{} - fm := map[string]json.RawMessage{} - - var err error - i.Files.Range(func(key Path, value *File) bool { - b, err := value.MarshalJSON() - if err != nil { - return false - } - fm[key.String()] = b - return true - }) - if err != nil { - return nil, err - } - - m["files"] = fm + m["files"] = i.Files m["infos"] = i.Infos m["current"] = i.current @@ -99,7 +84,38 @@ func (i *index) MarshalJSON() ([]byte, error) { } func (i *index) UnmarshalJSON(b []byte) error { - // fmt.Println(string(b)) + m := map[string]json.RawMessage{} + + if err := json.Unmarshal(b, &m); err != nil { + return err + } + + infos, ok := m["infos"] + if !ok { + return fmt.Errorf("missing infos") + } + i.Infos = &infosMap{} + if err := json.Unmarshal(infos, i.Infos); err != nil { + return err + } + + files, ok := m["files"] + if !ok { + return fmt.Errorf("missing files") + } + + i.Files = &filesMap{} + if err := json.Unmarshal(files, i.Files); err != nil { + return err + } + + current, ok := m["current"] + if !ok { + return fmt.Errorf("missing current") + } + if err := json.Unmarshal(current, &i.current); err != nil { + return err + } return nil } diff --git a/index_test.go b/index_test.go index 0286b12..574e48c 100644 --- a/index_test.go +++ b/index_test.go @@ -1,6 +1,8 @@ package pkger import ( + "encoding/json" + "fmt" "io" "os" "strings" @@ -61,3 +63,42 @@ func Test_index_Create_Write(t *testing.T) { r.NotZero(fi.ModTime()) r.NotEqual(mt, fi.ModTime()) } + +func Test_index_JSON(t *testing.T) { + r := require.New(t) + + i := newIndex() + + f, err := i.Create(Path{ + Name: "/radio.radio", + }) + r.NoError(err) + r.NotNil(f) + fmt.Fprint(f, radio) + r.NoError(f.Close()) + + c, err := i.Current() + r.NoError(err) + r.Equal(curPkg, c.ImportPath) + + _, err = i.Info("github.com/markbates/hepa") + r.NoError(err) + + r.Equal(1, len(i.Files.Keys())) + r.Equal(1, len(i.Infos.Keys())) + r.NotZero(i.current) + + jason, err := json.Marshal(i) + r.NoError(err) + r.NotZero(jason) + + i2 := &index{} + + r.NoError(json.Unmarshal(jason, i2)) + + r.NotNil(i2.Infos) + r.NotNil(i2.Files) + r.NotZero(i2.current) + r.Equal(1, len(i2.Files.Keys())) + r.Equal(1, len(i2.Infos.Keys())) +} diff --git a/infos_map.go b/infos_map.go index ce3b576..3af414f 100644 --- a/infos_map.go +++ b/infos_map.go @@ -3,6 +3,8 @@ package pkger import ( + "encoding/json" + "fmt" "sort" "sync" @@ -26,6 +28,27 @@ func (m *infosMap) Data() *sync.Map { return m.data } +func (m *infosMap) MarshalJSON() ([]byte, error) { + mm := map[string]interface{}{} + m.data.Range(func(key, value interface{}) bool { + mm[fmt.Sprintf("%s", key)] = value + return true + }) + return json.Marshal(mm) +} + +func (m *infosMap) UnmarshalJSON(b []byte) error { + mm := map[string]here.Info{} + + if err := json.Unmarshal(b, &mm); err != nil { + return err + } + for k, v := range mm { + m.Store(k, v) + } + return nil +} + // Delete the key from the map func (m *infosMap) Delete(key string) { m.Data().Delete(key) diff --git a/parse_test.go b/parse_test.go index 30b6673..d0d089d 100644 --- a/parse_test.go +++ b/parse_test.go @@ -6,8 +6,6 @@ import ( "github.com/stretchr/testify/require" ) -const curPkg = "github.com/markbates/pkger" - func Test_Parse_Happy(t *testing.T) { table := []struct { in string diff --git a/pkger_test.go b/pkger_test.go index 5d7b877..ba597c7 100644 --- a/pkger_test.go +++ b/pkger_test.go @@ -5,6 +5,8 @@ import ( "strings" ) +const curPkg = "github.com/markbates/pkger" + func createFile(p string, body ...string) (*File, error) { if len(body) == 0 { body = append(body, radio)