mirror of
https://github.com/go-task/task.git
synced 2024-12-12 10:45:49 +02:00
54 lines
1013 B
Go
54 lines
1013 B
Go
package taskfile
|
|
|
|
type (
|
|
NodeOption func(*BaseNode)
|
|
// BaseNode is a generic node that implements the Parent() and Optional()
|
|
// methods of the NodeReader interface. It does not implement the Read() method
|
|
// and it designed to be embedded in other node types so that this boilerplate
|
|
// code does not need to be repeated.
|
|
BaseNode struct {
|
|
parent Node
|
|
optional bool
|
|
dir string
|
|
}
|
|
)
|
|
|
|
func NewBaseNode(dir string, opts ...NodeOption) *BaseNode {
|
|
node := &BaseNode{
|
|
parent: nil,
|
|
optional: false,
|
|
dir: dir,
|
|
}
|
|
|
|
// Apply options
|
|
for _, opt := range opts {
|
|
opt(node)
|
|
}
|
|
|
|
return node
|
|
}
|
|
|
|
func WithParent(parent Node) NodeOption {
|
|
return func(node *BaseNode) {
|
|
node.parent = parent
|
|
}
|
|
}
|
|
|
|
func (node *BaseNode) Parent() Node {
|
|
return node.parent
|
|
}
|
|
|
|
func WithOptional(optional bool) NodeOption {
|
|
return func(node *BaseNode) {
|
|
node.optional = optional
|
|
}
|
|
}
|
|
|
|
func (node *BaseNode) Optional() bool {
|
|
return node.optional
|
|
}
|
|
|
|
func (node *BaseNode) Dir() string {
|
|
return node.dir
|
|
}
|