add root MergeIn
parent
0c80162394
commit
69eb868db6
|
|
@ -18,3 +18,21 @@ func (root *Root) MoveScheduledToTodo() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (root *Root) MergeIn(root2 Root) {
|
||||||
|
for _, listPair := range [][2]*[]Todo{
|
||||||
|
[2]*[]Todo{&root.Todo, &root2.Todo},
|
||||||
|
[2]*[]Todo{&root.Scheduled, &root2.Scheduled},
|
||||||
|
[2]*[]Todo{&root.Done, &root2.Done},
|
||||||
|
} {
|
||||||
|
for _, candidate := range *listPair[1] {
|
||||||
|
found := false
|
||||||
|
for i := range *listPair[0] {
|
||||||
|
found = found || ((*listPair[0])[i].Todo == candidate.Todo)
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
*listPair[0] = append(*listPair[0], candidate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJSONRoot(t *testing.T) {
|
func TestJSONRoot(t *testing.T) {
|
||||||
|
|
@ -96,3 +98,50 @@ func TestRootMoveScheduledToTodo(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMergeRoots(t *testing.T) {
|
||||||
|
root0yaml := `
|
||||||
|
todo:
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
- todo: c
|
||||||
|
- todo: d
|
||||||
|
tags: a
|
||||||
|
- exclusive to 0
|
||||||
|
`
|
||||||
|
root1yaml := `
|
||||||
|
todo:
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
- todo: c
|
||||||
|
- todo: d
|
||||||
|
tags: b
|
||||||
|
- exclusive to 1
|
||||||
|
`
|
||||||
|
rootWantyaml := `
|
||||||
|
todo:
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
- todo: c
|
||||||
|
- todo: d
|
||||||
|
tags: a
|
||||||
|
- exclusive to 0
|
||||||
|
- exclusive to 1
|
||||||
|
`
|
||||||
|
|
||||||
|
var root0, root1, rootWant Root
|
||||||
|
if err := yaml.Unmarshal([]byte(root0yaml), &root0); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal([]byte(root1yaml), &root1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal([]byte(rootWantyaml), &rootWant); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
root0.MergeIn(root1)
|
||||||
|
if fmt.Sprintf("%+v", root0) != fmt.Sprintf("%+v", rootWant) {
|
||||||
|
t.Fatalf("want \n\t%+v, got \n\t%+v", rootWant, root0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue