diff --git a/pttodo/todo.go b/pttodo/todo.go index 6144c09..fdc14b2 100644 --- a/pttodo/todo.go +++ b/pttodo/todo.go @@ -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) +} diff --git a/pttodo/todo_test.go b/pttodo/todo_test.go index 6f579ba..e1631a5 100644 --- a/pttodo/todo_test.go +++ b/pttodo/todo_test.go @@ -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) + } + }) +} diff --git a/sample.yaml b/sample.yaml index c4950a5..06fc26b 100644 --- a/sample.yaml +++ b/sample.yaml @@ -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'