JSON Marshal/Unmarshal Tags

This commit is contained in:
Brian Buller 2023-08-23 11:10:03 -05:00
parent 84732f16b9
commit 5eb1d227a1
3 changed files with 44 additions and 44 deletions

10
sort.go
View File

@ -36,8 +36,8 @@ func (todolist *TodoList) Sort(sortFlag int) error {
return nil
}
func (todolist *TodoList) refresh() {
todolist.Sort(todolist.sortFlag)
for i, t := range todolist.todos {
todolist.Sort(todolist.SortFlag)
for i, t := range todolist.Todos {
t.Id = i
}
}
@ -48,15 +48,15 @@ type todolistSort struct {
}
func (ts *todolistSort) Len() int {
return len(ts.todolists.todos)
return len(ts.todolists.Todos)
}
func (ts *todolistSort) Swap(l, r int) {
ts.todolists.todos[l], ts.todolists.todos[r] = ts.todolists.todos[r], ts.todolists.todos[l]
ts.todolists.Todos[l], ts.todolists.Todos[r] = ts.todolists.Todos[r], ts.todolists.Todos[l]
}
func (ts *todolistSort) Less(l, r int) bool {
return ts.by(ts.todolists.todos[l], ts.todolists.todos[r])
return ts.by(ts.todolists.Todos[l], ts.todolists.Todos[r])
}
func (todolist *TodoList) sortBy(by func(t1, t2 *Todo) bool) *TodoList {

22
todo.go
View File

@ -24,17 +24,17 @@ var (
// Todo represents a todo.txt task entry.
type Todo struct {
Id int // Internal todo id.
Original string // Original raw todo text.
Todo string // Todo part of todo text.
Priority string
Projects []string
Contexts []string
AdditionalTags map[string]string // Addon tags will be available here.
CreatedDate time.Time
DueDate time.Time
CompletedDate time.Time
Completed bool
Id int `json:"id"` // Internal todo id.
Original string `json:"original"` // Original raw todo text.
Todo string `json:"todo"` // Todo part of todo text.
Priority string `json:"priority"`
Projects []string `json:"projects"`
Contexts []string `json:"contexts"`
AdditionalTags map[string]string `json:"additionalTags"` // Addon tags will be available here.
CreatedDate time.Time `json:"createdDate"`
DueDate time.Time `json:"dueDate"`
CompletedDate time.Time `json:"completedDate"`
Completed bool `json:"completed"`
}
// String returns a complete todo string in todo.txt format.

View File

@ -11,17 +11,17 @@ import (
)
type TodoList struct {
todos []*Todo
sortFlag int
Todos []*Todo `json:"todos"`
SortFlag int `json:"sortFlag"`
}
// Newtodolist creates a new empty todolist.
func NewTodoList() *TodoList { return &TodoList{} }
func (todolist *TodoList) Size() int { return len(todolist.todos) }
func (todolist *TodoList) GetTaskSlice() []*Todo { return todolist.todos }
func (todolist *TodoList) Size() int { return len(todolist.Todos) }
func (todolist *TodoList) GetTaskSlice() []*Todo { return todolist.Todos }
func (todolist *TodoList) Contains(t *Todo) bool {
for _, tsk := range todolist.todos {
for _, tsk := range todolist.Todos {
if tsk == t {
return true
}
@ -44,7 +44,7 @@ func (todolist *TodoList) GetTasksWithProject(project string) *TodoList {
func (todolist *TodoList) GetContexts() []string {
var ret []string
added := make(map[string]bool)
for _, tsk := range todolist.todos {
for _, tsk := range todolist.Todos {
for _, c := range tsk.Contexts {
if !added[c] {
ret = append(ret, c)
@ -58,7 +58,7 @@ func (todolist *TodoList) GetContexts() []string {
func (todolist *TodoList) GetProjects() []string {
var ret []string
added := make(map[string]bool)
for _, tsk := range todolist.todos {
for _, tsk := range todolist.Todos {
for _, p := range tsk.Projects {
if !added[p] {
ret = append(ret, p)
@ -72,7 +72,7 @@ func (todolist *TodoList) GetProjects() []string {
func (todolist *TodoList) GetTagKVList() []string {
var ret []string
added := make(map[string]bool)
for _, tsk := range todolist.todos {
for _, tsk := range todolist.Todos {
for k, v := range tsk.AdditionalTags {
tag := fmt.Sprintf("%s:%s", k, v)
if !added[tag] {
@ -87,7 +87,7 @@ func (todolist *TodoList) GetTagKVList() []string {
func (todolist *TodoList) GetTagKeys() []string {
var ret []string
added := make(map[string]bool)
for _, tsk := range todolist.todos {
for _, tsk := range todolist.Todos {
for k := range tsk.AdditionalTags {
if !added[k] {
ret = append(ret, k)
@ -101,7 +101,7 @@ func (todolist *TodoList) GetTagKeys() []string {
func (todolist *TodoList) GetTagValuesForKey(key string) []string {
var ret []string
added := make(map[string]bool)
for _, tsk := range todolist.todos {
for _, tsk := range todolist.Todos {
if v, ok := tsk.AdditionalTags[key]; ok && !added[v] {
ret = append(ret, v)
added[v] = true
@ -113,9 +113,9 @@ func (todolist *TodoList) GetTagValuesForKey(key string) []string {
func (todolist *TodoList) GetIncompleteTasks() *TodoList {
t := *NewTodoList()
for _, v := range todolist.todos {
for _, v := range todolist.Todos {
if !v.Completed {
t.todos = append(t.todos, v)
t.Todos = append(t.Todos, v)
}
}
return &t
@ -124,7 +124,7 @@ func (todolist *TodoList) GetIncompleteTasks() *TodoList {
// String returns a complete list of tasks in todo.txt format.
func (todolist *TodoList) String() string {
var ret string
for _, todo := range todolist.todos {
for _, todo := range todolist.Todos {
ret += fmt.Sprintf("%s\n", todo.String())
}
return ret
@ -132,23 +132,23 @@ func (todolist *TodoList) String() string {
// AddTodo prepends a Todo to the current TodoList and takes care to set the Todo.Id correctly
func (todolist *TodoList) AddTodo(todo *Todo) {
todolist.todos = append(todolist.todos, todo)
todolist.Todos = append(todolist.Todos, todo)
todolist.refresh()
}
// AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values.
func (todolist *TodoList) AddTodos(todos []*Todo) {
todolist.todos = append(todolist.todos, todos...)
todolist.Sort(SORT_COMPLETED_DATE_DESC)
todolist.Todos = append(todolist.Todos, todos...)
todolist.Sort(SortCompletedDateDesc)
}
func (todolist *TodoList) Combine(other *TodoList) { todolist.AddTodos(other.todos) }
func (todolist *TodoList) Combine(other *TodoList) { todolist.AddTodos(other.Todos) }
// GetTodo returns the Todo with the given todo 'id' from the TodoList.
// Returns an error if Todo could not be found.
func (todolist *TodoList) GetTodo(id int) (*Todo, error) {
for i := range todolist.todos {
if todolist.todos[i].Id == id {
return todolist.todos[i], nil
for i := range todolist.Todos {
if todolist.Todos[i].Id == id {
return todolist.Todos[i], nil
}
}
return nil, errors.New("todo not found")
@ -160,7 +160,7 @@ func (todolist *TodoList) RemoveTodoById(id int) error {
found := false
var remIdx int
var t *Todo
for remIdx, t = range todolist.todos {
for remIdx, t = range todolist.Todos {
if t.Id == id {
found = true
break
@ -169,7 +169,7 @@ func (todolist *TodoList) RemoveTodoById(id int) error {
if !found {
return errors.New("todo not found")
}
todolist.todos = append(todolist.todos[:remIdx], todolist.todos[remIdx+1:]...)
todolist.Todos = append(todolist.Todos[:remIdx], todolist.Todos[remIdx+1:]...)
todolist.refresh()
return nil
}
@ -180,7 +180,7 @@ func (todolist *TodoList) RemoveTodo(todo Todo) error {
found := false
var remIdx int
var t *Todo
for remIdx, t = range todolist.todos {
for remIdx, t = range todolist.Todos {
if t.String() == todo.String() {
found = true
break
@ -189,7 +189,7 @@ func (todolist *TodoList) RemoveTodo(todo Todo) error {
if !found {
return errors.New("todo not found")
}
todolist.todos = append(todolist.todos[:remIdx], todolist.todos[remIdx+1:]...)
todolist.Todos = append(todolist.Todos[:remIdx], todolist.Todos[remIdx+1:]...)
todolist.refresh()
return nil
}
@ -214,9 +214,9 @@ func (todolist *TodoList) ArchiveTodoToFile(todo Todo, filename string) error {
// bool), and returns a new TodoList. The original TodoList is not modified.
func (todolist *TodoList) Filter(predicate func(*Todo) bool) *TodoList {
var newList TodoList
for _, t := range todolist.todos {
for _, t := range todolist.Todos {
if predicate(t) {
newList.todos = append(newList.todos, t)
newList.Todos = append(newList.Todos, t)
}
}
return &newList
@ -225,7 +225,7 @@ func (todolist *TodoList) Filter(predicate func(*Todo) bool) *TodoList {
// LoadFromFile loads a TodoList from *os.File.
// Note: This will clear the current TodoList and overwrite it's contents with whatever is in *os.File.
func (todolist *TodoList) LoadFromFile(file *os.File) error {
todolist.todos = []*Todo{} // Empty todolist
todolist.Todos = []*Todo{} // Empty todolist
todoId := 1
scanner := bufio.NewScanner(file)
for scanner.Scan() {
@ -240,7 +240,7 @@ func (todolist *TodoList) LoadFromFile(file *os.File) error {
}
todo.Id = todoId
todoId++
todolist.todos = append(todolist.todos, todo)
todolist.Todos = append(todolist.Todos, todo)
}
if err := scanner.Err(); err != nil {
return err