Introduce a CreateEntityFunc
This commit is contained in:
parent
4045a9c705
commit
489226d5f5
10 changed files with 32 additions and 20 deletions
|
@ -29,13 +29,8 @@ func (c *Collection) GetHash() string {
|
||||||
|
|
||||||
func (c *Collection) Marshal() ([]byte, error) {
|
func (c *Collection) Marshal() ([]byte, error) {
|
||||||
return json.Marshal(c)
|
return json.Marshal(c)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collection) Unmarshal(data []byte) error {
|
func (c *Collection) Unmarshal(data []byte) error {
|
||||||
return json.Unmarshal(data, c)
|
return json.Unmarshal(data, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collection) Create() *Collection {
|
|
||||||
return &Collection{}
|
|
||||||
}
|
|
||||||
|
|
|
@ -31,5 +31,5 @@ func (g *Group) Marshal() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Unmarshal(data []byte) error {
|
func (g *Group) Unmarshal(data []byte) error {
|
||||||
return gocsv.UnmarshalBytes(data, g.Participants)
|
return gocsv.UnmarshalBytes(data, &g.Participants)
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,12 @@ func (al AttributeList) MarshalCSV() (string, error) {
|
||||||
return result, nil
|
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 {
|
func convertMapToKeyValueOrderedString(m map[string]string) string {
|
||||||
keys := make([]string, 0, len(m))
|
keys := make([]string, 0, len(m))
|
||||||
for key := range m {
|
for key := range m {
|
||||||
|
|
|
@ -16,6 +16,11 @@ func NewDefaultCollectionFileStore() (*CollectionFileStore, error) {
|
||||||
&FileStoreConfig[*models.Collection, *store.CollectionStore]{
|
&FileStoreConfig[*models.Collection, *store.CollectionStore]{
|
||||||
FilePathConfig: FilePathConfig{GetDefaultCollectionsDir(), "collection", ".json"},
|
FilePathConfig: FilePathConfig{GetDefaultCollectionsDir(), "collection", ".json"},
|
||||||
IndexDirFunc: DefaultIndexDirFunc[*models.Collection, *store.CollectionStore],
|
IndexDirFunc: DefaultIndexDirFunc[*models.Collection, *store.CollectionStore],
|
||||||
|
CreateEntityFunc: func() *models.Collection {
|
||||||
|
return &models.Collection{
|
||||||
|
Quizzes: make([]*models.Quiz, 0),
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,6 @@ type FileStorable interface {
|
||||||
|
|
||||||
Marshal() ([]byte, error)
|
Marshal() ([]byte, error)
|
||||||
Unmarshal([]byte) error
|
Unmarshal([]byte) error
|
||||||
|
|
||||||
Create() FileStorable
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Storer[T store.Storable] interface {
|
type Storer[T store.Storable] interface {
|
||||||
|
@ -40,6 +38,7 @@ type FilePathConfig struct {
|
||||||
type FileStoreConfig[T FileStorable, K Storer[T]] struct {
|
type FileStoreConfig[T FileStorable, K Storer[T]] struct {
|
||||||
FilePathConfig
|
FilePathConfig
|
||||||
IndexDirFunc func(*FileStore[T, K]) error
|
IndexDirFunc func(*FileStore[T, K]) error
|
||||||
|
CreateEntityFunc func() T
|
||||||
NoIndexOnCreate bool
|
NoIndexOnCreate bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +52,10 @@ type FileStore[T FileStorable, K Storer[T]] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultIndexDirFunc[T FileStorable, K Storer[T]](s *FileStore[T, K]) error {
|
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)
|
files, err := os.ReadDir(s.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -76,7 +79,7 @@ func DefaultIndexDirFunc[T FileStorable, K Storer[T]](s *FileStore[T, K]) error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var entity T
|
entity := s.CreateEntityFunc()
|
||||||
|
|
||||||
err = entity.Unmarshal(content)
|
err = entity.Unmarshal(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -16,6 +16,11 @@ func NewDefaultGroupFileStore() (*GroupFileStore, error) {
|
||||||
&FileStoreConfig[*models.Group, *store.GroupStore]{
|
&FileStoreConfig[*models.Group, *store.GroupStore]{
|
||||||
FilePathConfig: FilePathConfig{GetDefaultGroupsDir(), "group", ".csv"},
|
FilePathConfig: FilePathConfig{GetDefaultGroupsDir(), "group", ".csv"},
|
||||||
IndexDirFunc: DefaultIndexDirFunc[*models.Group, *store.GroupStore],
|
IndexDirFunc: DefaultIndexDirFunc[*models.Group, *store.GroupStore],
|
||||||
|
CreateEntityFunc: func() *models.Group {
|
||||||
|
return &models.Group{
|
||||||
|
Participants: make([]*models.Participant, 0),
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ func (t *groupTestSuite) TestCreate() {
|
||||||
|
|
||||||
participantsFromDisk, err := readGroupFromCSV(g.GetID())
|
participantsFromDisk, err := readGroupFromCSV(g.GetID())
|
||||||
t.Nil(err)
|
t.Nil(err)
|
||||||
|
|
||||||
if !t.Failed() {
|
if !t.Failed() {
|
||||||
t.Equal("Smith", participantsFromDisk[0].Lastname)
|
t.Equal("Smith", participantsFromDisk[0].Lastname)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,11 @@ func NewParticipantDefaultFileStore() (*ParticipantFileStore, error) {
|
||||||
&FileStoreConfig[*models.Participant, *store.ParticipantStore]{
|
&FileStoreConfig[*models.Participant, *store.ParticipantStore]{
|
||||||
FilePathConfig: FilePathConfig{GetDefaultParticipantsDir(), "participant", ".json"},
|
FilePathConfig: FilePathConfig{GetDefaultParticipantsDir(), "participant", ".json"},
|
||||||
IndexDirFunc: DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
|
IndexDirFunc: DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
|
||||||
|
CreateEntityFunc: func() *models.Participant {
|
||||||
|
return &models.Participant{
|
||||||
|
Attributes: make(map[string]string),
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.andreafazzi.eu/andrea/probo/models"
|
"git.andreafazzi.eu/andrea/probo/models"
|
||||||
"git.andreafazzi.eu/andrea/probo/store"
|
|
||||||
"github.com/remogatto/prettytest"
|
"github.com/remogatto/prettytest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,12 +12,7 @@ type participantTestSuite struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *participantTestSuite) TestCreate() {
|
func (t *participantTestSuite) TestCreate() {
|
||||||
filePathConfig := FilePathConfig{"testdata/participants", "participant", ".json"}
|
pStore, err := NewParticipantDefaultFileStore()
|
||||||
pStore, err := NewParticipantFileStore(
|
|
||||||
&FileStoreConfig[*models.Participant, *store.ParticipantStore]{
|
|
||||||
FilePathConfig: filePathConfig,
|
|
||||||
IndexDirFunc: DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
|
|
||||||
})
|
|
||||||
t.Nil(err)
|
t.Nil(err)
|
||||||
|
|
||||||
if !t.Failed() {
|
if !t.Failed() {
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
id,firstname,lastname,token,attributes
|
|
||||||
1234,John,Smith,111222,class:1 D LIN
|
|
|
Loading…
Reference in a new issue