forked from matryer/persist
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
keks
committed
Jul 31, 2018
1 parent
2b80fc9
commit 0550843
Showing
3 changed files
with
31 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,32 @@ | ||
package persist | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"os" | ||
"sync" | ||
) | ||
|
||
var lock sync.Mutex | ||
|
||
// Marshal is a function that marshals the object into an | ||
// io.Reader. | ||
// By default, it uses the JSON marshaller. | ||
var Marshal = func(v interface{}) (io.Reader, error) { | ||
b, err := json.MarshalIndent(v, "", "\t") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewReader(b), nil | ||
} | ||
|
||
// Unmarshal is a function that unmarshals the data from the | ||
// reader into the specified value. | ||
// By default, it uses the JSON unmarshaller. | ||
var Unmarshal = func(r io.Reader, v interface{}) error { | ||
return json.NewDecoder(r).Decode(v) | ||
} | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Save saves a representation of v to the file at path. | ||
func Save(path string, v interface{}) error { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
f, err := os.Create(path) | ||
func Save(f *os.File, v interface{}) error { | ||
data, err := json.MarshalIndent(v, "", "\t") | ||
if err != nil { | ||
return err | ||
return errors.Wrapf(err, "error marshaling value of type %T", v) | ||
} | ||
defer f.Close() | ||
r, err := Marshal(v) | ||
|
||
_, err = f.Write(data) | ||
if err != nil { | ||
return err | ||
return errors.Wrap(err, "error copying data into file") | ||
} | ||
_, err = io.Copy(f, r) | ||
return err | ||
|
||
_, err = f.Seek(0, 0) | ||
return errors.Wrap(err, "error seeking to beginning of file") | ||
} | ||
|
||
// Load loads the file at path into v. | ||
// Use os.IsNotExist() to see if the returned error is due | ||
// to the file being missing. | ||
func Load(path string, v interface{}) error { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
return Unmarshal(f, v) | ||
func Load(f *os.File, v interface{}) error { | ||
err := json.NewDecoder(f).Decode(v) | ||
return errors.Wrap(err, "error decoding value") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters