From 489226d5f53b5aea154ae18af481d2a4d6e1ba78 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 21 Nov 2023 18:24:10 +0100 Subject: [PATCH] Introduce a CreateEntityFunc --- models/collection.go | 5 ----- models/group.go | 2 +- models/participant.go | 6 ++++++ store/file/collection.go | 5 +++++ store/file/file.go | 13 ++++++++----- store/file/group.go | 5 +++++ store/file/group_test.go | 1 + store/file/participant.go | 5 +++++ store/file/participant_test.go | 8 +------- .../group_96a300bb-0b29-4b32-93cf-5bcde7fcef61.csv | 2 -- 10 files changed, 32 insertions(+), 20 deletions(-) delete mode 100644 store/file/testdata/groups/group_96a300bb-0b29-4b32-93cf-5bcde7fcef61.csv diff --git a/models/collection.go b/models/collection.go index d9593e5..10cad13 100644 --- a/models/collection.go +++ b/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{} -} diff --git a/models/group.go b/models/group.go index c48beb2..92bbf36 100644 --- a/models/group.go +++ b/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) } diff --git a/models/participant.go b/models/participant.go index b0e5570..d5837d9 100644 --- a/models/participant.go +++ b/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 { diff --git a/store/file/collection.go b/store/file/collection.go index 6250e7b..a980a53 100644 --- a/store/file/collection.go +++ b/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), + } + }, }, ) diff --git a/store/file/file.go b/store/file/file.go index d854ce8..02f629b 100644 --- a/store/file/file.go +++ b/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 { diff --git a/store/file/group.go b/store/file/group.go index 9fc71f4..0c2f431 100644 --- a/store/file/group.go +++ b/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), + } + }, }, ) diff --git a/store/file/group_test.go b/store/file/group_test.go index 72ee304..d475856 100644 --- a/store/file/group_test.go +++ b/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) } diff --git a/store/file/participant.go b/store/file/participant.go index 3185b9f..7e05483 100644 --- a/store/file/participant.go +++ b/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), + } + }, }, ) } diff --git a/store/file/participant_test.go b/store/file/participant_test.go index 8d9c36e..1ea41dd 100644 --- a/store/file/participant_test.go +++ b/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() { diff --git a/store/file/testdata/groups/group_96a300bb-0b29-4b32-93cf-5bcde7fcef61.csv b/store/file/testdata/groups/group_96a300bb-0b29-4b32-93cf-5bcde7fcef61.csv deleted file mode 100644 index 564e88d..0000000 --- a/store/file/testdata/groups/group_96a300bb-0b29-4b32-93cf-5bcde7fcef61.csv +++ /dev/null @@ -1,2 +0,0 @@ -id,firstname,lastname,token,attributes -1234,John,Smith,111222,class:1 D LIN