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 return nil
} }
func (todolist *TodoList) refresh() { func (todolist *TodoList) refresh() {
todolist.Sort(todolist.sortFlag) todolist.Sort(todolist.SortFlag)
for i, t := range todolist.todos { for i, t := range todolist.Todos {
t.Id = i t.Id = i
} }
} }
@ -48,15 +48,15 @@ type todolistSort struct {
} }
func (ts *todolistSort) Len() int { func (ts *todolistSort) Len() int {
return len(ts.todolists.todos) return len(ts.todolists.Todos)
} }
func (ts *todolistSort) Swap(l, r int) { 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 { 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 { 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. // Todo represents a todo.txt task entry.
type Todo struct { type Todo struct {
Id int // Internal todo id. Id int `json:"id"` // Internal todo id.
Original string // Original raw todo text. Original string `json:"original"` // Original raw todo text.
Todo string // Todo part of todo text. Todo string `json:"todo"` // Todo part of todo text.
Priority string Priority string `json:"priority"`
Projects []string Projects []string `json:"projects"`
Contexts []string Contexts []string `json:"contexts"`
AdditionalTags map[string]string // Addon tags will be available here. AdditionalTags map[string]string `json:"additionalTags"` // Addon tags will be available here.
CreatedDate time.Time CreatedDate time.Time `json:"createdDate"`
DueDate time.Time DueDate time.Time `json:"dueDate"`
CompletedDate time.Time CompletedDate time.Time `json:"completedDate"`
Completed bool Completed bool `json:"completed"`
} }
// String returns a complete todo string in todo.txt format. // String returns a complete todo string in todo.txt format.

View File

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