From 69eb868db61317c627aa6aa8043015ddc1f8b537 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sat, 1 Jan 2022 17:43:20 -0500 Subject: [PATCH] add root MergeIn --- pttodo/root.go | 18 +++++++++++++++++ pttodo/root_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/pttodo/root.go b/pttodo/root.go index eabba1d..076cb7f 100644 --- a/pttodo/root.go +++ b/pttodo/root.go @@ -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) + } + } + } +} diff --git a/pttodo/root_test.go b/pttodo/root_test.go index da2df78..953957d 100644 --- a/pttodo/root_test.go +++ b/pttodo/root_test.go @@ -6,6 +6,8 @@ import ( "strconv" "testing" "time" + + yaml "gopkg.in/yaml.v2" ) 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) + } +}