1
0

node_base.go 780 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package taskfile
  2. type (
  3. NodeOption func(*BaseNode)
  4. // BaseNode is a generic node that implements the Parent() methods of the
  5. // NodeReader interface. It does not implement the Read() method and it
  6. // designed to be embedded in other node types so that this boilerplate code
  7. // does not need to be repeated.
  8. BaseNode struct {
  9. parent Node
  10. dir string
  11. }
  12. )
  13. func NewBaseNode(dir string, opts ...NodeOption) *BaseNode {
  14. node := &BaseNode{
  15. parent: nil,
  16. dir: dir,
  17. }
  18. // Apply options
  19. for _, opt := range opts {
  20. opt(node)
  21. }
  22. return node
  23. }
  24. func WithParent(parent Node) NodeOption {
  25. return func(node *BaseNode) {
  26. node.parent = parent
  27. }
  28. }
  29. func (node *BaseNode) Parent() Node {
  30. return node.parent
  31. }
  32. func (node *BaseNode) Dir() string {
  33. return node.dir
  34. }