Introduce a CreateEntityFunc

This commit is contained in:
andrea 2023-11-21 18:24:10 +01:00
parent 4045a9c705
commit 489226d5f5
10 changed files with 32 additions and 20 deletions

View file

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

View file

@ -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)
}

View file

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

View file

@ -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),
}
},
},
)

View file

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

View file

@ -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),
}
},
},
)

View file

@ -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)
}

View file

@ -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),
}
},
},
)
}

View file

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

View file

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