accept just a string for a todo task that converts to a struct on parse
parent
c1ed21ce19
commit
ce7bd17e72
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
|
|||
Loading…
Reference in New Issue