Skip to content

Commit e255e1c

Browse files
committed
Added generic type parameter for Tree to represent node data. Tests not passing for deserialize
1 parent 10601a5 commit e255e1c

File tree

9 files changed

+323
-263
lines changed

9 files changed

+323
-263
lines changed

tree/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package tree
22

33
import "log"
44

5-
type index map[uint]Node
5+
type index[T any] map[uint]Node[T]
66

7-
func (idx *index) find(id uint) Node {
7+
func (idx *index[T]) find(id uint) Node[T] {
88
if idx == nil { // do we need an error check here?
99
log.Println("Attempting to find in an undefined index")
1010
return nil
@@ -17,7 +17,7 @@ func (idx *index) find(id uint) Node {
1717
return val
1818
}
1919

20-
func (idx *index) insert(id uint, node Node) bool {
20+
func (idx *index[T]) insert(id uint, node Node[T]) bool {
2121
if idx == nil { // do we need an error check here?
2222
log.Println("Attempting to insert in an undefined index")
2323
return false

tree/index_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ import (
88

99
func TestIndexFind(t *testing.T) {
1010

11-
node1 := &node{Primary: 1}
12-
node2 := &node{Primary: 2}
11+
node1 := &node[int]{primary: 1}
12+
node2 := &node[int]{primary: 2}
1313

1414
tests := map[string]struct {
15-
index index
15+
index index[int]
1616
argID uint
17-
expNode Node
17+
expNode Node[int]
1818
}{
1919
"nil index": {
2020
index: nil,
2121
argID: 1,
2222
expNode: nil,
2323
},
2424
"not in index": {
25-
index: index{
25+
index: index[int]{
2626
1: node1,
2727
2: node2,
2828
},
2929
argID: 3,
3030
expNode: nil,
3131
},
3232
"success": {
33-
index: index{
33+
index: index[int]{
3434
1: node1,
3535
2: node2,
3636
},

tree/node.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,93 +31,93 @@ import (
3131
// When this node is serialized, only the id and parent id, along with
3232
// associated data are serialized. The internal pointers to parent and children
3333
// are recreated with the nodes are deserialized.
34-
type Node interface {
34+
type Node[T any] interface {
3535
// GetID returns the primary key of this node.
3636
GetID() uint
3737
// GetParentID returns the primary key of this node's parent.
3838
GetParentID() uint
3939

4040
// GetChildren returns an array of pointers to all children of this node.
41-
GetChildren() []Node
41+
GetChildren() []Node[T]
4242
// GetParent returns a pointer to the parent node of this node.
43-
GetParent() Node
43+
GetParent() Node[T]
4444

4545
// AddChildren adds a list of Nodes as children of this node.
46-
AddChildren(...Node)
46+
AddChildren(...Node[T])
4747
// ReplaceChildren replaces the current list of children with a new list of
4848
// Nodes.
49-
ReplaceChildren(...Node)
49+
ReplaceChildren(...Node[T])
5050

51-
setParent(n Node)
51+
setParent(n Node[T])
5252

5353
// GetData retruns this node's internal data.
54-
GetData() any
54+
GetData() T
5555
// SetData replaces this nodes data with the argument. The argument may be any
5656
// type, but must be serializable via json.
5757
//
5858
// This function does not attempt to test json encoding when the data is set;
5959
// any error with encoding will only occur when the data is serialized
6060
// to a repository.
61-
SetData(any)
61+
SetData(T)
6262
}
6363

64-
type node struct {
65-
Primary uint
66-
ParentID uint
67-
parent Node
68-
Data any
69-
children []Node
64+
type node[T any] struct {
65+
primary uint
66+
parentID uint
67+
parent Node[T]
68+
data T
69+
children []Node[T]
7070
}
7171

72-
func (n *node) GetID() uint {
73-
return n.Primary
72+
func (n *node[T]) GetID() uint {
73+
return n.primary
7474
}
7575

76-
func (n *node) GetParentID() uint {
77-
return n.ParentID
76+
func (n *node[T]) GetParentID() uint {
77+
return n.parentID
7878
}
7979

80-
func (n *node) GetChildren() []Node {
80+
func (n *node[T]) GetChildren() []Node[T] {
8181
return n.children
8282
}
8383

84-
func (n *node) GetParent() Node {
84+
func (n *node[T]) GetParent() Node[T] {
8585
return n.parent
8686
}
8787

88-
func (n *node) AddChildren(children ...Node) {
88+
func (n *node[T]) AddChildren(children ...Node[T]) {
8989
if n.children == nil {
90-
n.children = []Node{}
90+
n.children = []Node[T]{}
9191
}
9292
n.children = append(n.children, children[:]...)
9393
}
9494

95-
func (n *node) ReplaceChildren(children ...Node) {
96-
n.children = []Node{}
95+
func (n *node[T]) ReplaceChildren(children ...Node[T]) {
96+
n.children = []Node[T]{}
9797
n.AddChildren(children...)
9898
}
9999

100-
func (n *node) setParent(parent Node) {
100+
func (n *node[T]) setParent(parent Node[T]) {
101101
if parent == nil || parent.GetID() == n.GetID() {
102102
return
103103
}
104104
n.parent = parent
105-
n.ParentID = parent.GetID()
105+
n.parentID = parent.GetID()
106106

107107
}
108108

109-
func (n *node) GetData() any {
110-
return n.Data
109+
func (n *node[T]) GetData() T {
110+
return n.data
111111
}
112112

113-
func (n *node) SetData(newdata any) {
114-
n.Data = newdata
113+
func (n *node[T]) SetData(newdata T) {
114+
n.data = newdata
115115
}
116116

117-
func (n *node) Format(f fmt.State, verb rune) {
117+
func (n *node[T]) Format(f fmt.State, verb rune) {
118118
switch verb {
119119
case 'v':
120-
fmt.Fprintf(f, "{primary: %d parentID: %d data:%+v children:[", n.Primary, n.ParentID, n.Data)
120+
fmt.Fprintf(f, "{primary: %d parentID: %d data:%+v children:[", n.primary, n.parentID, n.data)
121121
for i, n := range n.children {
122122
if i != 0 {
123123
fmt.Fprint(f, " ")

0 commit comments

Comments
 (0)