summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-05-27 09:20:49 +0300
committerPaul Buetow <paul@buetow.org>2024-05-27 09:20:49 +0300
commit11788abad75c0d4920ed4f7797febd7d36569a66 (patch)
tree1432a50e82c5db81c1c2d33461a7c25ebacc258a
parent9be6694a4a70e06be713033145f763e9d7f86295 (diff)
move the interface to the client
-rw-r--r--internal/server/repository/repository.go14
-rw-r--r--internal/types/entry.go23
-rw-r--r--internal/vfs/memoryfs_test.go22
-rw-r--r--internal/vfs/vfs.go8
4 files changed, 35 insertions, 32 deletions
diff --git a/internal/server/repository/repository.go b/internal/server/repository/repository.go
index fa9a5f4..b98ccfa 100644
--- a/internal/server/repository/repository.go
+++ b/internal/server/repository/repository.go
@@ -15,6 +15,12 @@ var (
once sync.Once
)
+type fs interface {
+ ReadFile(name string) ([]byte, error)
+ WriteFile(filePath string, bytes []byte) error
+ FindFiles(dataPath, suffix string) ([]string, error)
+}
+
// Contains an Entry ID and its checksumm, for the list and merge operations.
type EntryPair struct {
ID, Checksum string
@@ -24,7 +30,7 @@ type Repository struct {
dataDir string
entries map[string]types.Entry
mu *sync.Mutex
- vfs vfs.VFS
+ fs fs
}
func Instance(dataDir string) *Repository {
@@ -33,7 +39,7 @@ func Instance(dataDir string) *Repository {
dataDir: dataDir,
entries: make(map[string]types.Entry),
mu: &sync.Mutex{},
- vfs: vfs.RealFS{},
+ fs: vfs.RealFS{},
}
})
return instance
@@ -47,7 +53,7 @@ func (r Repository) add(entry types.Entry) {
// Load repository into memory
func (r Repository) load() error {
- filePaths, err := r.vfs.FindFiles(r.dataDir, ".json")
+ filePaths, err := r.fs.FindFiles(r.dataDir, ".json")
if err != nil {
return err
}
@@ -80,7 +86,7 @@ func (r Repository) List() ([]byte, error) {
}
func (r Repository) Get(id string) ([]byte, error) {
- return r.vfs.ReadFile(fmt.Sprintf("%s/%s", r.dataDir, id))
+ return r.fs.ReadFile(fmt.Sprintf("%s/%s", r.dataDir, id))
}
func (r Repository) HasSameEntry(pair EntryPair) bool {
diff --git a/internal/types/entry.go b/internal/types/entry.go
index af1e991..eca7b91 100644
--- a/internal/types/entry.go
+++ b/internal/types/entry.go
@@ -10,6 +10,11 @@ import (
"codeberg.org/snonux/gos/internal/vfs"
)
+type fs interface {
+ ReadFile(name string) ([]byte, error)
+ WriteFile(filePath string, bytes []byte) error
+}
+
// Tells me whether the entry was shared to the sm platform named Name
type Shared struct {
Name string `json:"name"`
@@ -37,7 +42,7 @@ type Entry struct {
Body string `json:"body"`
Shared []Shared `json:"shared,omitempty"`
Epoch int `json:"epoch,omitempty"`
- vfs vfs.VFS
+ fs fs
// The checksum of the whole entry, can change depending on the state.
checksum string
@@ -57,23 +62,23 @@ func NewEntry(bytes []byte) (Entry, error) {
return e, nil
}
-func NewEntryFromFile(filePath string, vfsToUse ...vfs.VFS) (Entry, error) {
+func NewEntryFromFile(filePath string, fsToUse ...fs) (Entry, error) {
var (
bytes []byte
err error
- vfs vfs.VFS = vfs.RealFS{}
+ fs fs = vfs.RealFS{}
)
- if len(vfsToUse) > 0 {
- vfs = vfsToUse[0]
+ if len(fsToUse) > 0 {
+ fs = fsToUse[0]
}
- bytes, err = vfs.ReadFile(filePath)
+ bytes, err = fs.ReadFile(filePath)
if err != err {
return Entry{}, err
}
e, err := NewEntry(bytes)
- e.vfs = vfs
+ e.fs = fs
return e, err
}
@@ -86,7 +91,7 @@ func NewEntryFromCopy(other Entry) (Entry, error) {
func (e *Entry) initialize() {
e.mu = &sync.Mutex{}
e.checksumDirty = true
- e.vfs = vfs.RealFS{}
+ e.fs = vfs.RealFS{}
}
func (e Entry) Equals(other Entry) bool {
@@ -170,7 +175,7 @@ func (e Entry) SaveFile(filePath string) error {
return err
}
- return e.vfs.WriteFile(filePath, jsonStr)
+ return e.fs.WriteFile(filePath, jsonStr)
}
func (e Entry) String() string {
diff --git a/internal/vfs/memoryfs_test.go b/internal/vfs/memoryfs_test.go
index a1d392e..4d75500 100644
--- a/internal/vfs/memoryfs_test.go
+++ b/internal/vfs/memoryfs_test.go
@@ -8,7 +8,7 @@ import (
func TestMemoryFS(t *testing.T) {
t.Parallel()
- var vfs VFS = make(MemoryFS)
+ fs := make(MemoryFS)
writeFiles := map[string]string{
"/data/dir/foo.json": "hello world",
@@ -19,26 +19,26 @@ func TestMemoryFS(t *testing.T) {
for path, content := range writeFiles {
bytes := []byte(content)
- _ = vfs.WriteFile(path, bytes)
+ _ = fs.WriteFile(path, bytes)
}
t.Run("files are there", func(t *testing.T) {
- testFilesAreThere(t, vfs, writeFiles)
+ testFilesAreThere(t, fs, writeFiles)
})
t.Run("file is not there", func(t *testing.T) {
- testFileNotThere(t, vfs, "/dennis.rodman.txt")
+ testFileNotThere(t, fs, "/dennis.rodman.txt")
})
t.Run("find json files", func(t *testing.T) {
- testFindFiles(t, vfs, writeFiles, "/data/dir/subdir", ".json", 2)
+ testFindFiles(t, fs, writeFiles, "/data/dir/subdir", ".json", 2)
})
}
-func testFilesAreThere(t *testing.T, vfs VFS, writeFiles map[string]string) {
+func testFilesAreThere(t *testing.T, fs MemoryFS, writeFiles map[string]string) {
for path, content := range writeFiles {
- bytes, err := vfs.ReadFile(path)
+ bytes, err := fs.ReadFile(path)
if err != nil {
t.Error(err)
return
@@ -50,8 +50,8 @@ func testFilesAreThere(t *testing.T, vfs VFS, writeFiles map[string]string) {
}
}
-func testFileNotThere(t *testing.T, vfs VFS, filePath string) {
- _, err := vfs.ReadFile(filePath)
+func testFileNotThere(t *testing.T, fs MemoryFS, filePath string) {
+ _, err := fs.ReadFile(filePath)
if err == nil {
t.Error("expected file", filePath, "not to be there, but it is")
return
@@ -59,8 +59,8 @@ func testFileNotThere(t *testing.T, vfs VFS, filePath string) {
t.Log("file", filePath, "not there as expected:", err)
}
-func testFindFiles(t *testing.T, vfs VFS, writeFiles map[string]string, dataDir, suffix string, count int) {
- filePaths, err := vfs.FindFiles(dataDir, suffix)
+func testFindFiles(t *testing.T, fs MemoryFS, writeFiles map[string]string, dataDir, suffix string, count int) {
+ filePaths, err := fs.FindFiles(dataDir, suffix)
if err != nil {
t.Error(err)
return
diff --git a/internal/vfs/vfs.go b/internal/vfs/vfs.go
deleted file mode 100644
index c31a458..0000000
--- a/internal/vfs/vfs.go
+++ /dev/null
@@ -1,8 +0,0 @@
-package vfs
-
-// virtual file system - useful for testing as well
-type VFS interface {
- ReadFile(name string) ([]byte, error)
- WriteFile(filePath string, bytes []byte) error
- FindFiles(dataPath, suffix string) ([]string, error)
-}