fix: Fixed decoding for empty files and added temporary directories to tests

This commit is contained in:
Nicholas Novak
2023-10-29 01:23:38 -07:00
parent 511d494cd9
commit 08e5cd80cc
3 changed files with 35 additions and 4 deletions

View File

@@ -7,6 +7,10 @@ import (
"git.nicholasnovak.io/nnovak/spatial-db/world"
)
var (
ChunkFileDirectory = "./persistence"
)
func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) {
var chunkData world.ChunkData

View File

@@ -5,6 +5,7 @@ import (
"errors"
"io/fs"
"os"
"path/filepath"
"git.nicholasnovak.io/nnovak/spatial-db/world"
)
@@ -17,7 +18,7 @@ type SimpleServer struct {
// Filesystem operations
func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
chunkFileName := pos.ToFileName()
chunkFileName := filepath.Join(ChunkFileDirectory, pos.ToFileName())
var chunkData world.ChunkData
@@ -34,6 +35,10 @@ func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
if err := json.NewEncoder(chunkFile).Encode(chunkData); err != nil {
return chunkData, err
}
if _, err := chunkFile.Seek(0, 0); err != nil {
return chunkData, err
}
} else {
return chunkData, err
}