Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
use generic tree node
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 6, 2022
1 parent c21f0e0 commit 4a575c4
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 253 deletions.
26 changes: 0 additions & 26 deletions group/group.go

This file was deleted.

54 changes: 0 additions & 54 deletions group/group_test.go

This file was deleted.

26 changes: 0 additions & 26 deletions group/node.go

This file was deleted.

111 changes: 0 additions & 111 deletions group/service.go

This file was deleted.

34 changes: 34 additions & 0 deletions host/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package host

import (
"errors"
)

var (
ErrGroupExist = errors.New("nodes is already existed")
)

type Group struct {
ID uint `boltholdKey:"ID"`
ParentID uint
Name string
Desc string
}

func (g Group) Title() string {
return g.Name
}
func (g Group) Description() string {
return g.Desc
}
func (g Group) FilterValue() string {
return g.Name
}

func (g Group) GetID() uint {
return g.ID
}

func (g Group) GetParentID() uint {
return g.ParentID
}
40 changes: 40 additions & 0 deletions host/group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package host

import (
"fmt"
"github.com/stretchr/testify/assert"
"gohost/store"
"testing"
)

func TestGroupService_Save(t *testing.T) {
s := store.Store()
defer s.Close()
a := assert.New(t)
service := GetService()
groups := []Group{
{ID: 1, ParentID: 0, Name: "g-1", Desc: "desc1"},
{ID: 11, ParentID: 1, Name: "g-1-1", Desc: "desc11"},
{ID: 111, ParentID: 11, Name: "g-1-1-1", Desc: "desc111"},
{ID: 112, ParentID: 11, Name: "g-1-1-2", Desc: "desc112"},
{ID: 12, ParentID: 1, Name: "g-1-2", Desc: "desc12"},
{ID: 13, ParentID: 1, Name: "g-1-3", Desc: "desc13"},
{ID: 131, ParentID: 13, Name: "g-1-3-1", Desc: "desc131"},
{ID: 132, ParentID: 13, Name: "g-1-3-2", Desc: "desc132"},
{ID: 2, ParentID: 0, Name: "g-2", Desc: "desc2"},
{ID: 3, ParentID: 0, Name: "g-3", Desc: "desc3"},
{ID: 31, ParentID: 3, Name: "g-3-1", Desc: "desc31"},
{ID: 32, ParentID: 3, Name: "g-3-2", Desc: "desc32"},
{ID: 4, ParentID: 0, Name: "g-4", Desc: "desc4"},
}
for _, g := range groups {
if err := service.SaveGroup(g); err != nil {
a.NoError(err)
}
}
savedGroups, err := service.loadGroups()
a.NoError(err)
for i := range savedGroups {
fmt.Println("wtf", savedGroups[i])
}
}
2 changes: 1 addition & 1 deletion host/store.go → host/local.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package host

type DBHost struct {
type Local struct {
Name string
Content string
Group int
Expand Down
46 changes: 46 additions & 0 deletions host/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package host

import "github.com/charmbracelet/bubbles/list"

type Type uint

const (
GroupType Type = iota
LocalHostType
RemoteHostType
)

type TreeNode interface {
list.Item
GetID() uint
GetParentID() uint
}

type Node[T TreeNode] struct {
Data T
Children []*Node[T]
Depth int
Type Type
IsFold bool
}

func NewNode[T TreeNode](data T, depth int) *Node[T] {
return &Node[T]{
Data: data,
Children: make([]*Node[T], 0),
Depth: depth,
IsFold: true,
}
}

func (n *Node[T]) FilterValue() string {
return n.FilterValue()
}

func (n *Node[T]) GetID() uint {
return n.Data.GetID()
}

func (n *Node[T]) GetParentID() uint {
return n.Data.GetParentID()
}

0 comments on commit 4a575c4

Please sign in to comment.