@@ -31,93 +31,93 @@ import (
31
31
// When this node is serialized, only the id and parent id, along with
32
32
// associated data are serialized. The internal pointers to parent and children
33
33
// are recreated with the nodes are deserialized.
34
- type Node interface {
34
+ type Node [ T any ] interface {
35
35
// GetID returns the primary key of this node.
36
36
GetID () uint
37
37
// GetParentID returns the primary key of this node's parent.
38
38
GetParentID () uint
39
39
40
40
// GetChildren returns an array of pointers to all children of this node.
41
- GetChildren () []Node
41
+ GetChildren () []Node [ T ]
42
42
// GetParent returns a pointer to the parent node of this node.
43
- GetParent () Node
43
+ GetParent () Node [ T ]
44
44
45
45
// AddChildren adds a list of Nodes as children of this node.
46
- AddChildren (... Node )
46
+ AddChildren (... Node [ T ] )
47
47
// ReplaceChildren replaces the current list of children with a new list of
48
48
// Nodes.
49
- ReplaceChildren (... Node )
49
+ ReplaceChildren (... Node [ T ] )
50
50
51
- setParent (n Node )
51
+ setParent (n Node [ T ] )
52
52
53
53
// GetData retruns this node's internal data.
54
- GetData () any
54
+ GetData () T
55
55
// SetData replaces this nodes data with the argument. The argument may be any
56
56
// type, but must be serializable via json.
57
57
//
58
58
// This function does not attempt to test json encoding when the data is set;
59
59
// any error with encoding will only occur when the data is serialized
60
60
// to a repository.
61
- SetData (any )
61
+ SetData (T )
62
62
}
63
63
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 ]
70
70
}
71
71
72
- func (n * node ) GetID () uint {
73
- return n .Primary
72
+ func (n * node [ T ] ) GetID () uint {
73
+ return n .primary
74
74
}
75
75
76
- func (n * node ) GetParentID () uint {
77
- return n .ParentID
76
+ func (n * node [ T ] ) GetParentID () uint {
77
+ return n .parentID
78
78
}
79
79
80
- func (n * node ) GetChildren () []Node {
80
+ func (n * node [ T ] ) GetChildren () []Node [ T ] {
81
81
return n .children
82
82
}
83
83
84
- func (n * node ) GetParent () Node {
84
+ func (n * node [ T ] ) GetParent () Node [ T ] {
85
85
return n .parent
86
86
}
87
87
88
- func (n * node ) AddChildren (children ... Node ) {
88
+ func (n * node [ T ] ) AddChildren (children ... Node [ T ] ) {
89
89
if n .children == nil {
90
- n .children = []Node {}
90
+ n .children = []Node [ T ] {}
91
91
}
92
92
n .children = append (n .children , children [:]... )
93
93
}
94
94
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 ] {}
97
97
n .AddChildren (children ... )
98
98
}
99
99
100
- func (n * node ) setParent (parent Node ) {
100
+ func (n * node [ T ] ) setParent (parent Node [ T ] ) {
101
101
if parent == nil || parent .GetID () == n .GetID () {
102
102
return
103
103
}
104
104
n .parent = parent
105
- n .ParentID = parent .GetID ()
105
+ n .parentID = parent .GetID ()
106
106
107
107
}
108
108
109
- func (n * node ) GetData () any {
110
- return n .Data
109
+ func (n * node [ T ] ) GetData () T {
110
+ return n .data
111
111
}
112
112
113
- func (n * node ) SetData (newdata any ) {
114
- n .Data = newdata
113
+ func (n * node [ T ] ) SetData (newdata T ) {
114
+ n .data = newdata
115
115
}
116
116
117
- func (n * node ) Format (f fmt.State , verb rune ) {
117
+ func (n * node [ T ] ) Format (f fmt.State , verb rune ) {
118
118
switch verb {
119
119
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 )
121
121
for i , n := range n .children {
122
122
if i != 0 {
123
123
fmt .Fprint (f , " " )
0 commit comments