/
node_base.go
47 lines (39 loc) · 905 Bytes
/
node_base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package read
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
}
)
func NewBaseNode(opts ...NodeOption) *BaseNode {
node := &BaseNode{
parent: nil,
optional: false,
}
// 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
}