Extract persistence code into own class
This commit is contained in:
parent
4c76530832
commit
c5ebbc3c40
9 changed files with 163 additions and 149 deletions
90
pkg/persistence/persistence.go
Normal file
90
pkg/persistence/persistence.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
package persistence
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
// dataPath global path for persistence
|
||||
var dataPath = os.TempDir()
|
||||
|
||||
// SetPath sets the persistent path for the data storage
|
||||
func SetPath(path string) error {
|
||||
if info, err := os.Stat(path); err != nil {
|
||||
return err
|
||||
} else if !info.IsDir() {
|
||||
return fmt.Errorf("path for persistence is not directory")
|
||||
}
|
||||
dataPath = path
|
||||
return nil
|
||||
}
|
||||
|
||||
// Converts name to a full path
|
||||
func jsonPath(name string) string {
|
||||
return path.Join(dataPath, fmt.Sprintf("rove-%s.json", name))
|
||||
}
|
||||
|
||||
// Save will serialise the interface into a json file
|
||||
func Save(name string, data interface{}) error {
|
||||
if b, err := json.MarshalIndent(data, "", "\t"); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := ioutil.WriteFile(jsonPath(name), b, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load will load the interface from the json file
|
||||
func Load(name string, data interface{}) error {
|
||||
path := jsonPath(name)
|
||||
// Don't load anything if the file doesn't exist
|
||||
_, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Printf("File %s didn't exist, loading with fresh data\n", path)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read and unmarshal the json
|
||||
if b, err := ioutil.ReadFile(path); err != nil {
|
||||
return err
|
||||
} else if err := json.Unmarshal(b, data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// saveLoadFunc defines a type of function to save or load an interface
|
||||
type saveLoadFunc func(string, interface{}) error
|
||||
|
||||
func doAll(f saveLoadFunc, args ...interface{}) error {
|
||||
var name string
|
||||
for i, a := range args {
|
||||
if i%2 == 0 {
|
||||
var ok bool
|
||||
name, ok = a.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("Incorrect args")
|
||||
}
|
||||
} else {
|
||||
if err := f(name, a); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveAll allows for saving multiple structures in a single call
|
||||
func SaveAll(args ...interface{}) error {
|
||||
return doAll(Save, args...)
|
||||
}
|
||||
|
||||
// LoadAll allows for loading multiple structures in a single call
|
||||
func LoadAll(args ...interface{}) error {
|
||||
return doAll(Load, args...)
|
||||
}
|
52
pkg/persistence/persistence_test.go
Normal file
52
pkg/persistence/persistence_test.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package persistence
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type Dummy struct {
|
||||
Success bool
|
||||
Value int
|
||||
}
|
||||
|
||||
func TestPersistence_LoadSave(t *testing.T) {
|
||||
tmp, err := ioutil.TempDir(os.TempDir(), "rove_persistence_test")
|
||||
assert.NoError(t, err, "Failed to get tempdir path")
|
||||
|
||||
assert.NoError(t, SetPath(tmp), "Failed to get set tempdir to persistence path")
|
||||
|
||||
// Try and save out the dummy
|
||||
var dummy Dummy
|
||||
dummy.Success = true
|
||||
assert.NoError(t, Save("test", dummy), "Failed to save out dummy file")
|
||||
|
||||
// Load back the dummy
|
||||
dummy = Dummy{}
|
||||
assert.NoError(t, Load("test", &dummy), "Failed to load in dummy file")
|
||||
assert.Equal(t, true, dummy.Success, "Did not successfully load true value from file")
|
||||
}
|
||||
|
||||
func TestPersistence_LoadSaveAll(t *testing.T) {
|
||||
tmp, err := ioutil.TempDir(os.TempDir(), "rove_persistence_test")
|
||||
assert.NoError(t, err, "Failed to get tempdir path")
|
||||
|
||||
assert.NoError(t, SetPath(tmp), "Failed to get set tempdir to persistence path")
|
||||
|
||||
// Try and save out the dummy
|
||||
var dummyA Dummy
|
||||
var dummyB Dummy
|
||||
dummyA.Value = 1
|
||||
dummyB.Value = 2
|
||||
assert.NoError(t, SaveAll("a", dummyA, "b", dummyB), "Failed to save out dummy file")
|
||||
|
||||
// Load back the dummy
|
||||
dummyA = Dummy{}
|
||||
dummyB = Dummy{}
|
||||
assert.NoError(t, LoadAll("a", &dummyA, "b", &dummyB), "Failed to load in dummy file")
|
||||
assert.Equal(t, 1, dummyA.Value, "Did not successfully load int value from file")
|
||||
assert.Equal(t, 2, dummyB.Value, "Did not successfully load int value from file")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue