ruby soho

This commit is contained in:
Mark Bates 2019-08-02 17:37:27 -04:00
parent 974364ac0d
commit be03e3492e
7 changed files with 139 additions and 26 deletions

View File

@ -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

View File

@ -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{}
}
})
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)

View File

@ -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
}

View File

@ -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()))
}

View File

@ -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)

View File

@ -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

View File

@ -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)