|
@@ -23,8 +23,6 @@ type FileStorable interface {
|
|
|
|
|
|
Marshal() ([]byte, error)
|
|
|
Unmarshal([]byte) error
|
|
|
-
|
|
|
- Create() FileStorable
|
|
|
}
|
|
|
|
|
|
type Storer[T store.Storable] interface {
|
|
@@ -39,8 +37,9 @@ type FilePathConfig struct {
|
|
|
|
|
|
type FileStoreConfig[T FileStorable, K Storer[T]] struct {
|
|
|
FilePathConfig
|
|
|
- IndexDirFunc func(*FileStore[T, K]) error
|
|
|
- NoIndexOnCreate bool
|
|
|
+ IndexDirFunc func(*FileStore[T, K]) error
|
|
|
+ CreateEntityFunc func() T
|
|
|
+ NoIndexOnCreate bool
|
|
|
}
|
|
|
|
|
|
type FileStore[T FileStorable, K Storer[T]] struct {
|
|
@@ -53,6 +52,10 @@ type FileStore[T FileStorable, K Storer[T]] struct {
|
|
|
}
|
|
|
|
|
|
func DefaultIndexDirFunc[T FileStorable, K Storer[T]](s *FileStore[T, K]) error {
|
|
|
+ if s.CreateEntityFunc == nil {
|
|
|
+ return errors.New("CreateEntityFunc cannot be nil!")
|
|
|
+ }
|
|
|
+
|
|
|
files, err := os.ReadDir(s.Dir)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -76,7 +79,7 @@ func DefaultIndexDirFunc[T FileStorable, K Storer[T]](s *FileStore[T, K]) error
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- var entity T
|
|
|
+ entity := s.CreateEntityFunc()
|
|
|
|
|
|
err = entity.Unmarshal(content)
|
|
|
if err != nil {
|