fs use binary (um)marshaler
This commit is contained in:
+24
-5
@@ -1,6 +1,7 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -74,7 +75,11 @@ func (f *fs) Lock() (func() error, error) {
|
||||
return unlock, nil
|
||||
}
|
||||
|
||||
func (f *fs) Write(path string, data []byte) error {
|
||||
func (f *fs) Write(path string, v encoding.BinaryMarshaler) error {
|
||||
if v == nil {
|
||||
return errors.New("cant't marshal from nil object")
|
||||
}
|
||||
|
||||
if f.lock == nil {
|
||||
return errors.New("lock must be acquired before write")
|
||||
}
|
||||
@@ -84,6 +89,11 @@ func (f *fs) Write(path string, data []byte) error {
|
||||
return errors.New("can't write to the lock file")
|
||||
}
|
||||
|
||||
data, err := v.MarshalBinary()
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal data: %s", err)
|
||||
}
|
||||
|
||||
targetDir := filepath.Dir(targetFilePath)
|
||||
if targetDir != f.root {
|
||||
ok, err := dirExists(targetDir)
|
||||
@@ -146,17 +156,26 @@ func (f *fs) Write(path string, data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fs) Read(path string) ([]byte, error) {
|
||||
func (f *fs) Read(path string, v encoding.BinaryUnmarshaler) error {
|
||||
if v == nil {
|
||||
return errors.New("can't unmarshal to a nil object")
|
||||
}
|
||||
|
||||
if f.lock != nil {
|
||||
return nil, errors.New("lock must be released before read")
|
||||
return errors.New("lock must be released before read")
|
||||
}
|
||||
|
||||
targetFilePath := filepath.Join(f.root, path)
|
||||
if targetFilePath == f.lockFilePath() {
|
||||
return nil, errors.New("can't read from the lock file")
|
||||
return errors.New("can't read from the lock file")
|
||||
}
|
||||
|
||||
return os.ReadFile(targetFilePath)
|
||||
data, err := os.ReadFile(targetFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading data file: %s", err)
|
||||
}
|
||||
|
||||
return v.UnmarshalBinary(data)
|
||||
}
|
||||
|
||||
func (f *fs) lockFilePath() string {
|
||||
|
||||
@@ -58,7 +58,8 @@ func TestWrite(t *testing.T) {
|
||||
{path: "/" + dirName, err: "wrong type"},
|
||||
} {
|
||||
t.Run(tc.path, func(t *testing.T) {
|
||||
err = fs.Write(tc.path, []byte{0, 1, 2, 3})
|
||||
sd := &sampleData{[]byte{0, 1, 2, 3}}
|
||||
err = fs.Write(tc.path, sd)
|
||||
if tc.err == "" {
|
||||
if err != nil {
|
||||
assert.Fail(t, "not expecting an error", "write to file %s: %s", tc.path, err)
|
||||
@@ -122,7 +123,8 @@ func TestRead(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
_, err = fs.Read(tc.path)
|
||||
sd := new(sampleData)
|
||||
err = fs.Read(tc.path, sd)
|
||||
if tc.err == "" {
|
||||
if err != nil {
|
||||
assert.Fail(t, "read: not expecting an error, got: "+err.Error())
|
||||
@@ -147,7 +149,8 @@ func TestWriteErrorWithoutLock(t *testing.T) {
|
||||
defer cleanup()
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage")
|
||||
err = fs.Write("some/path", []byte{0, 1, 2, 3})
|
||||
sd := &sampleData{[]byte{0, 1, 2, 3}}
|
||||
err = fs.Write("some/path", sd)
|
||||
assert.Error(t, err, "should return error when no lock acquired")
|
||||
assert.True(t, strings.Contains(err.Error(), "lock must be acquired"), "should return missing lock error")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -21,3 +22,16 @@ func createWorkDir(t *testing.T) (string, func()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type sampleData struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (sd *sampleData) UnmarshalBinary(data []byte) error {
|
||||
sd.data = slices.Clone(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sd sampleData) MarshalBinary() (data []byte, err error) {
|
||||
return sd.data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user