From fc4cf55e7809e7afe6e5d5c83c4835a69b5aa6f5 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Fri, 20 Aug 2021 09:06:35 +0200 Subject: [PATCH] introduce localtask --- cmd/cli/format/format.go | 4 +-- internal/process/list.go | 8 ++--- internal/process/list_test.go | 14 ++++---- internal/process/projects.go | 2 +- internal/process/sync_test.go | 7 ++-- internal/process/update.go | 2 +- internal/storage/local.go | 8 ++--- internal/storage/memory.go | 24 ++++++------- internal/storage/memory_test.go | 11 +++--- internal/storage/sqlite.go | 62 +++++++++++++++++---------------- internal/task/localtask.go | 5 +++ 11 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 internal/task/localtask.go diff --git a/cmd/cli/format/format.go b/cmd/cli/format/format.go index 383e07c..8c35d84 100644 --- a/cmd/cli/format/format.go +++ b/cmd/cli/format/format.go @@ -11,7 +11,7 @@ func FormatError(err error) string { return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error()) } -func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string { +func FormatTaskTable(local storage.LocalRepository, tasks []*task.LocalTask) string { if len(tasks) == 0 { return "no tasks to display\n" } @@ -29,7 +29,7 @@ func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string { return output } -func FormatTask(id int, t *task.Task) string { +func FormatTask(id int, t *task.LocalTask) string { output := fmt.Sprintf(`folder: %s action: %s project: %s diff --git a/internal/process/list.go b/internal/process/list.go index 708a894..2717d67 100644 --- a/internal/process/list.go +++ b/internal/process/list.go @@ -41,7 +41,7 @@ type List struct { } type ListResult struct { - Tasks []*task.Task + Tasks []*task.LocalTask } func NewList(local storage.LocalRepository, reqs ListReqs) *List { @@ -61,7 +61,7 @@ func (l *List) Process() (*ListResult, error) { folders = []string{l.reqs.Folder} } - var potentialTasks []*task.Task + var potentialTasks []*task.LocalTask for _, folder := range folders { folderTasks, err := l.local.FindAllInFolder(folder) if err != nil { @@ -79,7 +79,7 @@ func (l *List) Process() (*ListResult, error) { } if l.reqs.Project != "" { - var projectTasks []*task.Task + var projectTasks []*task.LocalTask for _, pt := range potentialTasks { if pt.Project == l.reqs.Project { projectTasks = append(projectTasks, pt) @@ -95,7 +95,7 @@ func (l *List) Process() (*ListResult, error) { }, nil } - dueTasks := []*task.Task{} + dueTasks := []*task.LocalTask{} for _, t := range potentialTasks { switch { case t.Due.IsZero(): diff --git a/internal/process/list_test.go b/internal/process/list_test.go index f28cec6..56a24b4 100644 --- a/internal/process/list_test.go +++ b/internal/process/list_test.go @@ -47,7 +47,9 @@ func TestListProcess(t *testing.T) { Project: "project2", } allTasks := []*task.Task{task1, task2, task3, task4} - + localTask2 := &task.LocalTask{Task: *task2} + localTask3 := &task.LocalTask{Task: *task3} + localTask4 := &task.LocalTask{Task: *task4} local := storage.NewMemory() test.OK(t, local.SetTasks(allTasks)) @@ -60,14 +62,14 @@ func TestListProcess(t *testing.T) { for _, tc := range []struct { name string reqs process.ListReqs - exp []*task.Task + exp []*task.LocalTask }{ { name: "due", reqs: process.ListReqs{ Due: date2, }, - exp: []*task.Task{task3}, + exp: []*task.LocalTask{localTask3}, }, { name: "due and before", @@ -75,21 +77,21 @@ func TestListProcess(t *testing.T) { Due: date2, IncludeBefore: true, }, - exp: []*task.Task{task2, task3}, + exp: []*task.LocalTask{localTask2, localTask3}, }, { name: "folder", reqs: process.ListReqs{ Folder: task.FOLDER_PLANNED, }, - exp: []*task.Task{task2, task3, task4}, + exp: []*task.LocalTask{localTask2, localTask3, localTask4}, }, { name: "project", reqs: process.ListReqs{ Project: "project2", }, - exp: []*task.Task{task2, task4}, + exp: []*task.LocalTask{localTask2, localTask4}, }, } { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/process/projects.go b/internal/process/projects.go index 547a6b7..f37e8e8 100644 --- a/internal/process/projects.go +++ b/internal/process/projects.go @@ -24,7 +24,7 @@ func NewProjects(local storage.LocalRepository) *Projects { } func (p *Projects) Process() ([]string, error) { - allTasks := []*task.Task{} + allTasks := []*task.LocalTask{} for _, folder := range []string{task.FOLDER_NEW, task.FOLDER_PLANNED, task.FOLDER_UNPLANNED} { folderTasks, err := p.local.FindAllInFolder(folder) if err != nil { diff --git a/internal/process/sync_test.go b/internal/process/sync_test.go index 6ac2aca..7645e0d 100644 --- a/internal/process/sync_test.go +++ b/internal/process/sync_test.go @@ -24,6 +24,9 @@ func TestSyncProcess(t *testing.T) { Folder: task.FOLDER_UNPLANNED, } + localTask1 := &task.LocalTask{Task: *task1} + localTask2 := &task.LocalTask{Task: *task2} + mstorer, err := mstore.NewMemory(task.KnownFolders) test.OK(t, err) test.OK(t, mstorer.Add(task1.Folder, task1.FormatSubject(), task1.FormatBody())) @@ -37,8 +40,8 @@ func TestSyncProcess(t *testing.T) { test.Equals(t, 2, actResult.Count) actTasks1, err := local.FindAllInFolder(task.FOLDER_NEW) test.OK(t, err) - test.Equals(t, []*task.Task{task1}, actTasks1) + test.Equals(t, []*task.LocalTask{localTask1}, actTasks1) actTasks2, err := local.FindAllInFolder(task.FOLDER_UNPLANNED) test.OK(t, err) - test.Equals(t, []*task.Task{task2}, actTasks2) + test.Equals(t, []*task.LocalTask{localTask2}, actTasks2) } diff --git a/internal/process/update.go b/internal/process/update.go index 90816a3..7fe04e6 100644 --- a/internal/process/update.go +++ b/internal/process/update.go @@ -52,7 +52,7 @@ func (u *Update) Process() error { } } - if err := u.disp.Dispatch(tsk); err != nil { + if err := u.disp.Dispatch(&tsk.Task); err != nil { return fmt.Errorf("%w: %v", ErrUpdateTask, err) } diff --git a/internal/storage/local.go b/internal/storage/local.go index c2d1958..e79455c 100644 --- a/internal/storage/local.go +++ b/internal/storage/local.go @@ -15,10 +15,10 @@ var ( type LocalRepository interface { LatestSync() (time.Time, error) SetTasks(tasks []*task.Task) error - FindAllInFolder(folder string) ([]*task.Task, error) - FindAllInProject(project string) ([]*task.Task, error) - FindById(id string) (*task.Task, error) - FindByLocalId(id int) (*task.Task, error) + FindAllInFolder(folder string) ([]*task.LocalTask, error) + FindAllInProject(project string) ([]*task.LocalTask, error) + FindById(id string) (*task.LocalTask, error) + FindByLocalId(id int) (*task.LocalTask, error) LocalIds() (map[string]int, error) } diff --git a/internal/storage/memory.go b/internal/storage/memory.go index 066a8eb..0a61d3d 100644 --- a/internal/storage/memory.go +++ b/internal/storage/memory.go @@ -48,47 +48,47 @@ func (m *Memory) setLocalId(id string) { m.localIds[id] = next } -func (m *Memory) FindAllInFolder(folder string) ([]*task.Task, error) { - tasks := []*task.Task{} +func (m *Memory) FindAllInFolder(folder string) ([]*task.LocalTask, error) { + tasks := []*task.LocalTask{} for _, t := range m.tasks { if t.Folder == folder { - tasks = append(tasks, t) + tasks = append(tasks, &task.LocalTask{Task: *t}) } } return tasks, nil } -func (m *Memory) FindAllInProject(project string) ([]*task.Task, error) { - tasks := []*task.Task{} +func (m *Memory) FindAllInProject(project string) ([]*task.LocalTask, error) { + tasks := []*task.LocalTask{} for _, t := range m.tasks { if t.Project == project { - tasks = append(tasks, t) + tasks = append(tasks, &task.LocalTask{Task: *t}) } } return tasks, nil } -func (m *Memory) FindById(id string) (*task.Task, error) { +func (m *Memory) FindById(id string) (*task.LocalTask, error) { for _, t := range m.tasks { if t.Id == id { - return t, nil + return &task.LocalTask{Task: *t}, nil } } - return &task.Task{}, ErrTaskNotFound + return &task.LocalTask{}, ErrTaskNotFound } -func (m *Memory) FindByLocalId(localId int) (*task.Task, error) { +func (m *Memory) FindByLocalId(localId int) (*task.LocalTask, error) { for _, t := range m.tasks { if m.localIds[t.Id] == localId { - return t, nil + return &task.LocalTask{Task: *t}, nil } } - return &task.Task{}, ErrTaskNotFound + return &task.LocalTask{}, ErrTaskNotFound } func (m *Memory) LocalIds() (map[string]int, error) { diff --git a/internal/storage/memory_test.go b/internal/storage/memory_test.go index cfdc534..c460166 100644 --- a/internal/storage/memory_test.go +++ b/internal/storage/memory_test.go @@ -41,6 +41,9 @@ func TestMemory(t *testing.T) { }, } tasks := []*task.Task{task1, task2, task3} + localTask1 := &task.LocalTask{Task: *task1} + localTask2 := &task.LocalTask{Task: *task2} + localTask3 := &task.LocalTask{Task: *task3} t.Run("sync", func(t *testing.T) { mem := storage.NewMemory() @@ -60,7 +63,7 @@ func TestMemory(t *testing.T) { test.OK(t, mem.SetTasks(tasks)) act, err := mem.FindAllInFolder(folder1) test.OK(t, err) - exp := []*task.Task{task1, task2} + exp := []*task.LocalTask{localTask1, localTask2} for _, tsk := range exp { tsk.Message = nil } @@ -72,7 +75,7 @@ func TestMemory(t *testing.T) { test.OK(t, mem.SetTasks(tasks)) act, err := mem.FindAllInProject(project1) test.OK(t, err) - exp := []*task.Task{task1, task3} + exp := []*task.LocalTask{localTask1, localTask3} for _, tsk := range exp { tsk.Message = nil } @@ -84,7 +87,7 @@ func TestMemory(t *testing.T) { test.OK(t, mem.SetTasks(tasks)) act, err := mem.FindById("id-2") test.OK(t, err) - test.Equals(t, task2, act) + test.Equals(t, localTask2, act) }) t.Run("findbylocalid", func(t *testing.T) { @@ -92,7 +95,7 @@ func TestMemory(t *testing.T) { test.OK(t, mem.SetTasks(tasks)) act, err := mem.FindByLocalId(2) test.OK(t, err) - test.Equals(t, task2, act) + test.Equals(t, localTask2, act) }) t.Run("localids", func(t *testing.T) { diff --git a/internal/storage/sqlite.go b/internal/storage/sqlite.go index 4e414d6..a415376 100644 --- a/internal/storage/sqlite.go +++ b/internal/storage/sqlite.go @@ -150,31 +150,31 @@ SET latest_sync = ?`, return nil } -func (s *Sqlite) FindAllInFolder(folder string) ([]*task.Task, error) { +func (s *Sqlite) FindAllInFolder(folder string) ([]*task.LocalTask, error) { rows, err := s.db.Query(` SELECT id, version, folder, action, project, due, recur FROM task WHERE folder = ?`, folder) if err != nil { - return []*task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) + return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } return tasksFromRows(rows) } -func (s *Sqlite) FindAllInProject(project string) ([]*task.Task, error) { +func (s *Sqlite) FindAllInProject(project string) ([]*task.LocalTask, error) { rows, err := s.db.Query(` SELECT id, version, folder, action, project, due, recur FROM task WHERE project = ?`, project) if err != nil { - return []*task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) + return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } return tasksFromRows(rows) } -func (s *Sqlite) FindById(id string) (*task.Task, error) { +func (s *Sqlite) FindById(id string) (*task.LocalTask, error) { var folder, action, project, due, recur string var version int row := s.db.QueryRow(` @@ -183,30 +183,31 @@ FROM task WHERE id = ? LIMIT 1`, id) if err := row.Scan(&version, &folder, &action, &project, &due, &recur); err != nil { - return &task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) + return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } - return &task.Task{ - Id: id, - Version: version, - Folder: folder, - Action: action, - Project: project, - Due: task.NewDateFromString(due), - Recur: task.NewRecurrer(recur), - }, nil + return &task.LocalTask{ + Task: task.Task{ + Id: id, + Version: version, + Folder: folder, + Action: action, + Project: project, + Due: task.NewDateFromString(due), + Recur: task.NewRecurrer(recur), + }}, nil } -func (s *Sqlite) FindByLocalId(localId int) (*task.Task, error) { +func (s *Sqlite) FindByLocalId(localId int) (*task.LocalTask, error) { var id string row := s.db.QueryRow(`SELECT id FROM local_id WHERE local_id = ?`, localId) if err := row.Scan(&id); err != nil { - return &task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) + return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } t, err := s.FindById(id) if err != nil { - return &task.Task{}, nil + return &task.LocalTask{}, nil } return t, nil @@ -232,25 +233,26 @@ func (s *Sqlite) LocalIds() (map[string]int, error) { return idMap, nil } -func tasksFromRows(rows *sql.Rows) ([]*task.Task, error) { - tasks := []*task.Task{} +func tasksFromRows(rows *sql.Rows) ([]*task.LocalTask, error) { + tasks := []*task.LocalTask{} defer rows.Close() for rows.Next() { var id, folder, action, project, due, recur string var version int if err := rows.Scan(&id, &version, &folder, &action, &project, &due, &recur); err != nil { - return []*task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) + return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } - tasks = append(tasks, &task.Task{ - Id: id, - Version: version, - Folder: folder, - Action: action, - Project: project, - Due: task.NewDateFromString(due), - Recur: task.NewRecurrer(recur), - }) + tasks = append(tasks, &task.LocalTask{ + Task: task.Task{ + Id: id, + Version: version, + Folder: folder, + Action: action, + Project: project, + Due: task.NewDateFromString(due), + Recur: task.NewRecurrer(recur), + }}) } return tasks, nil diff --git a/internal/task/localtask.go b/internal/task/localtask.go new file mode 100644 index 0000000..656a759 --- /dev/null +++ b/internal/task/localtask.go @@ -0,0 +1,5 @@ +package task + +type LocalTask struct { + Task +}