accept just a string for a todo task that converts to a struct on parse

master
Bel LaPointe 2021-12-31 16:39:54 -05:00
parent c1ed21ce19
commit ce7bd17e72
3 changed files with 57 additions and 0 deletions

View File

@ -15,3 +15,13 @@ func (todo Todo) Triggered() bool {
next, err := todo.Schedule.Next(last.time())
return err == nil && time.Now().After(next)
}
func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error {
*todo = Todo{}
if err := unmarshal(&todo.Todo); err == nil {
return nil
}
type Alt Todo
alt := (*Alt)(todo)
return unmarshal(alt)
}

View File

@ -3,6 +3,8 @@ package pttodo
import (
"encoding/json"
"testing"
yaml "gopkg.in/yaml.v2"
)
func TestJSONTodo(t *testing.T) {
@ -41,3 +43,42 @@ func TestJSONTodo(t *testing.T) {
}
})
}
func TestTodoUnmarshalYAML(t *testing.T) {
t.Run("just a string", func(t *testing.T) {
var littleRoot struct {
Todo Todo
}
if err := yaml.Unmarshal([]byte(`
todo: my full task here
`), &littleRoot); err != nil {
t.Fatal(err)
}
if littleRoot.Todo.Todo != "my full task here" {
t.Fatal(littleRoot)
}
})
t.Run("full struct", func(t *testing.T) {
var littleRoot struct {
Todo Todo
}
if err := yaml.Unmarshal([]byte(`
todo:
todo: the todo part of my task
detail: whatever
schedule: "* * * * *"
`), &littleRoot); err != nil {
t.Fatal(err)
}
if littleRoot.Todo.Todo != "the todo part of my task" {
t.Fatal(littleRoot)
}
if littleRoot.Todo.Detail != "whatever" {
t.Fatal(littleRoot)
}
if littleRoot.Todo.Schedule != "* * * * *" {
t.Fatal(littleRoot)
}
})
}

View File

@ -9,6 +9,12 @@ todo:
todo: task that appeared when 'when'
schedule: "* * * * *" # or ([0-9]*[a-z])+ for durations, or int for scheduled/deferred
ts: 222
- todo: short 1
- todo: short 2
- todo: short 3
- todo: short 4
- todo: short 5
- and this teeny line which is just the line
scheduled:
-
todo: task that will appear when 'when'