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
Showing
4 changed files
with
139 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
# persist | ||
Persist loads and saves Go objects to files | ||
|
||
## Usage | ||
|
||
First get it: | ||
|
||
``` | ||
go get github.com/matryer/persist | ||
``` | ||
|
||
Then save objects like this: | ||
|
||
``` | ||
var conf Config | ||
if err := persist.Save("./project.conf", &conf); err != nil { | ||
log.Fatalln("failed to save config:", err) | ||
} | ||
``` | ||
|
||
And load them like this: | ||
|
||
|
||
``` | ||
var conf Config | ||
if err := persist.Load("./project.conf", &conf); err != nil { | ||
log.Fatalln("failed to load config:", err) | ||
} | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package persist loads and saves Go objects to files. | ||
package persist |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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) | ||
} | ||
|
||
// 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) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
r, err := Marshal(v) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = io.Copy(f, r) | ||
return err | ||
} | ||
|
||
// 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) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package persist_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cheekybits/is" | ||
"github.com/matryer/persist" | ||
) | ||
|
||
type obj struct { | ||
Name string | ||
Number int | ||
When time.Time | ||
} | ||
|
||
func TestNoFile(t *testing.T) { | ||
is := is.New(t) | ||
|
||
var v interface{} | ||
err := persist.Load("no-such-file.conf", &v) | ||
is.Equal(true, os.IsNotExist(err)) | ||
|
||
} | ||
|
||
func TestPersist(t *testing.T) { | ||
is := is.New(t) | ||
defer os.Remove("./file.tmp") | ||
|
||
o := &obj{ | ||
Name: "Mat", | ||
Number: 47, | ||
When: time.Now(), | ||
} | ||
|
||
// save it | ||
err := persist.Save("./file.tmp", o) | ||
is.NoErr(err) | ||
|
||
// load it | ||
var o2 obj | ||
err = persist.Load("./file.tmp", &o2) | ||
is.NoErr(err) | ||
|
||
is.Equal(o.Name, o2.Name) | ||
is.Equal(o.Number, o2.Number) | ||
is.Equal(o.When, o2.When) | ||
|
||
} |