Skip to content

Commit

Permalink
enable using load multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptix committed Sep 24, 2019
1 parent 3af502d commit 861f057
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func Save(f *os.File, v interface{}) error {
// Use os.IsNotExist() to see if the returned error is due
// to the file being missing.
func Load(f *os.File, v interface{}) error {
err := json.NewDecoder(f).Decode(v)
_, err := f.Seek(0, 0)
if err != nil {
return errors.Wrap(err, "error reetting reader")
}
err = json.NewDecoder(f).Decode(v)
return errors.Wrap(err, "error decoding value")
}
10 changes: 10 additions & 0 deletions persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ func TestPersist(t *testing.T) {
is.Equal(o.Name, o2.Name)
is.Equal(o.Number, o2.Number)
is.True(o.When.Equal(o2.When))

// load it, twice
o2.Name = ""
o2.Number = 0
err = persist.Load(f, &o2)
is.NoErr(err)

is.Equal(o.Name, o2.Name)
is.Equal(o.Number, o2.Number)
is.True(o.When.Equal(o2.When))
}

0 comments on commit 861f057

Please sign in to comment.