Author:
Hash:
Timestamp:
+26 -19 +/-3 browse
Kevin Schoon [me@kevinschoon.com]
08d4a9b682bbbbe16dc7ab56cd26b451df13dc83
Tue, 31 May 2022 19:03:34 +0000 (3.5 years ago)
| 1 | diff --git a/pkg/hierarchy/editor.go b/pkg/hierarchy/editor.go |
| 2 | new file mode 100644 |
| 3 | index 0000000..bc67a3c |
| 4 | --- /dev/null |
| 5 | +++ b/pkg/hierarchy/editor.go |
| 6 | @@ -0,0 +1 @@ |
| 7 | + package hierarchy |
| 8 | diff --git a/pkg/hierarchy/hierarchy.go b/pkg/hierarchy/hierarchy.go |
| 9 | index d8db856..5786eca 100644 |
| 10 | --- a/pkg/hierarchy/hierarchy.go |
| 11 | +++ b/pkg/hierarchy/hierarchy.go |
| 12 | @@ -3,21 +3,12 @@ package hierarchy |
| 13 | import ( |
| 14 | "database/sql" |
| 15 | "encoding/json" |
| 16 | - "fmt" |
| 17 | "io/ioutil" |
| 18 | "os" |
| 19 | |
| 20 | "kevinschoon.com/hierarchy/pkg/config" |
| 21 | ) |
| 22 | |
| 23 | - type Note struct { |
| 24 | - ID int64 |
| 25 | - Name string |
| 26 | - Content string |
| 27 | - Parent *Note |
| 28 | - Siblings []*Note |
| 29 | - } |
| 30 | - |
| 31 | type dbNote struct { |
| 32 | ID int64 |
| 33 | Name string |
| 34 | @@ -27,7 +18,6 @@ type dbNote struct { |
| 35 | |
| 36 | func Edit(cfg config.Config, notePath string) error { |
| 37 | np := ReadPath(notePath) |
| 38 | - fmt.Println(np) |
| 39 | return With(cfg.Database, func(tx *sql.Tx) error { |
| 40 | raw, err := ioutil.ReadAll(os.Stdin) |
| 41 | if err != nil { |
| 42 | @@ -53,8 +43,9 @@ VALUES (?, ?, ?) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | - func linkNotes(dbNotes []*dbNote, notes []*Note) []*Note { |
| 47 | - var remaining []*dbNote |
| 48 | + // TODO: garbage |
| 49 | + func linkNotes2(dbNotes []*dbNote, notes []*Note) []*Note { |
| 50 | + remaining := []*dbNote{} |
| 51 | for _, note := range dbNotes { |
| 52 | if note.Parent == 0 { |
| 53 | notes = append(notes, &Note{ |
| 54 | @@ -64,23 +55,29 @@ func linkNotes(dbNotes []*dbNote, notes []*Note) []*Note { |
| 55 | Parent: nil, |
| 56 | }) |
| 57 | continue |
| 58 | + } else { |
| 59 | + remaining = append(remaining, note) |
| 60 | } |
| 61 | - loop: |
| 62 | + } |
| 63 | + nextRemaining := []*dbNote{} |
| 64 | + loop: |
| 65 | + for _, note := range remaining { |
| 66 | for _, other := range notes { |
| 67 | - if other.ID == note.Parent { |
| 68 | - other.Siblings = append(other.Siblings, &Note{ |
| 69 | + if note.Parent == other.ID { |
| 70 | + n := &Note{ |
| 71 | ID: note.ID, |
| 72 | Name: note.Name, |
| 73 | Content: note.Content, |
| 74 | Parent: other, |
| 75 | - }) |
| 76 | + } |
| 77 | + other.Children = append(other.Children, n) |
| 78 | continue loop |
| 79 | } |
| 80 | } |
| 81 | - remaining = append(remaining, note) |
| 82 | + nextRemaining = append(nextRemaining, note) |
| 83 | } |
| 84 | if len(remaining) > 0 { |
| 85 | - return linkNotes(remaining, notes) |
| 86 | + notes = linkNotes2(nextRemaining, notes) |
| 87 | } |
| 88 | return notes |
| 89 | } |
| 90 | @@ -108,7 +105,7 @@ FROM notes |
| 91 | } |
| 92 | notes = append(notes, note) |
| 93 | } |
| 94 | - linked := linkNotes(notes, nil) |
| 95 | + linked := linkNotes2(notes, nil) |
| 96 | return json.NewEncoder(os.Stdout).Encode(linked) |
| 97 | }) |
| 98 | } |
| 99 | diff --git a/pkg/hierarchy/note.go b/pkg/hierarchy/note.go |
| 100 | new file mode 100644 |
| 101 | index 0000000..29223bb |
| 102 | --- /dev/null |
| 103 | +++ b/pkg/hierarchy/note.go |
| 104 | @@ -0,0 +1,9 @@ |
| 105 | + package hierarchy |
| 106 | + |
| 107 | + type Note struct { |
| 108 | + ID int64 `json:"id"` |
| 109 | + Name string `json:"name"` |
| 110 | + Content string `json:"content"` |
| 111 | + Parent *Note `json:"-"` |
| 112 | + Children []*Note `json:"children"` |
| 113 | + } |