Forráskód Böngészése

Introduce a CreateEntityFunc

andrea 6 hónapja
szülő
commit
489226d5f5

+ 0 - 5
models/collection.go

@@ -29,13 +29,8 @@ func (c *Collection) GetHash() string {
 
 func (c *Collection) Marshal() ([]byte, error) {
 	return json.Marshal(c)
-
 }
 
 func (c *Collection) Unmarshal(data []byte) error {
 	return json.Unmarshal(data, c)
 }
-
-func (c *Collection) Create() *Collection {
-	return &Collection{}
-}

+ 1 - 1
models/group.go

@@ -31,5 +31,5 @@ func (g *Group) Marshal() ([]byte, error) {
 }
 
 func (g *Group) Unmarshal(data []byte) error {
-	return gocsv.UnmarshalBytes(data, g.Participants)
+	return gocsv.UnmarshalBytes(data, &g.Participants)
 }

+ 6 - 0
models/participant.go

@@ -61,6 +61,12 @@ func (al AttributeList) MarshalCSV() (string, error) {
 	return result, nil
 }
 
+func (al AttributeList) UnmarshalCSV(csv string) error {
+	al = make(map[string]string)
+	al["foo"] = "bar"
+	return nil
+}
+
 func convertMapToKeyValueOrderedString(m map[string]string) string {
 	keys := make([]string, 0, len(m))
 	for key := range m {

+ 5 - 0
store/file/collection.go

@@ -16,6 +16,11 @@ func NewDefaultCollectionFileStore() (*CollectionFileStore, error) {
 		&FileStoreConfig[*models.Collection, *store.CollectionStore]{
 			FilePathConfig: FilePathConfig{GetDefaultCollectionsDir(), "collection", ".json"},
 			IndexDirFunc:   DefaultIndexDirFunc[*models.Collection, *store.CollectionStore],
+			CreateEntityFunc: func() *models.Collection {
+				return &models.Collection{
+					Quizzes: make([]*models.Quiz, 0),
+				}
+			},
 		},
 	)
 

+ 8 - 5
store/file/file.go

@@ -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 {

+ 5 - 0
store/file/group.go

@@ -16,6 +16,11 @@ func NewDefaultGroupFileStore() (*GroupFileStore, error) {
 		&FileStoreConfig[*models.Group, *store.GroupStore]{
 			FilePathConfig: FilePathConfig{GetDefaultGroupsDir(), "group", ".csv"},
 			IndexDirFunc:   DefaultIndexDirFunc[*models.Group, *store.GroupStore],
+			CreateEntityFunc: func() *models.Group {
+				return &models.Group{
+					Participants: make([]*models.Participant, 0),
+				}
+			},
 		},
 	)
 

+ 1 - 0
store/file/group_test.go

@@ -53,6 +53,7 @@ func (t *groupTestSuite) TestCreate() {
 
 		participantsFromDisk, err := readGroupFromCSV(g.GetID())
 		t.Nil(err)
+
 		if !t.Failed() {
 			t.Equal("Smith", participantsFromDisk[0].Lastname)
 		}

+ 5 - 0
store/file/participant.go

@@ -16,6 +16,11 @@ func NewParticipantDefaultFileStore() (*ParticipantFileStore, error) {
 		&FileStoreConfig[*models.Participant, *store.ParticipantStore]{
 			FilePathConfig: FilePathConfig{GetDefaultParticipantsDir(), "participant", ".json"},
 			IndexDirFunc:   DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
+			CreateEntityFunc: func() *models.Participant {
+				return &models.Participant{
+					Attributes: make(map[string]string),
+				}
+			},
 		},
 	)
 }

+ 1 - 7
store/file/participant_test.go

@@ -4,7 +4,6 @@ import (
 	"os"
 
 	"git.andreafazzi.eu/andrea/probo/models"
-	"git.andreafazzi.eu/andrea/probo/store"
 	"github.com/remogatto/prettytest"
 )
 
@@ -13,12 +12,7 @@ type participantTestSuite struct {
 }
 
 func (t *participantTestSuite) TestCreate() {
-	filePathConfig := FilePathConfig{"testdata/participants", "participant", ".json"}
-	pStore, err := NewParticipantFileStore(
-		&FileStoreConfig[*models.Participant, *store.ParticipantStore]{
-			FilePathConfig: filePathConfig,
-			IndexDirFunc:   DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
-		})
+	pStore, err := NewParticipantDefaultFileStore()
 	t.Nil(err)
 
 	if !t.Failed() {

+ 0 - 2
store/file/testdata/groups/group_96a300bb-0b29-4b32-93cf-5bcde7fcef61.csv

@@ -1,2 +0,0 @@
-id,firstname,lastname,token,attributes
-1234,John,Smith,111222,class:1 D LIN