Skip to content

Commit

Permalink
feat(tree): set Children on Leafs (converts their Node type)
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Jan 10, 2025
1 parent 0502e1f commit f615974
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tree/children.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (n NodeChildren) Replace(index int, child Node) NodeChildren {
if index < 0 || len(n) < index+1 {
return n
}
return slices.Replace(n, index, index, child)
return slices.Replace(n, index, index+1, child)
}

// Remove removes a child from the list at the given index.
Expand Down
11 changes: 11 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Node interface {
Hidden() bool
SetHidden(bool)
SetValue(any)
Child(...any) *Tree
}

// Leaf is a node without children.
Expand Down Expand Up @@ -78,6 +79,16 @@ func (s *Leaf) SetValue(value any) {
}
}

// Child adds a child to this Tree, converting a Leaf to a Tree in the process.
func (s *Leaf) Child(children ...any) *Tree {
t := &Tree{
value: s.value,
hidden: s.hidden,
children: NodeChildren(nil),
}
return t.Child(children)
}

// Hidden returns whether a Leaf node is hidden.
func (s Leaf) Hidden() bool {
return s.hidden
Expand Down

0 comments on commit f615974

Please sign in to comment.