-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
63 lines (50 loc) · 1.02 KB
/
component.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gestalt
import (
"fmt"
"github.com/ovrclk/gestalt/vars"
)
type Action func(Evaluator) error
type Component interface {
Name() string
IsPassThrough() bool
Eval(Evaluator) error
WithMeta(vars.Meta) Component
Meta() vars.Meta
}
type CompositeComponent interface {
Component
Children() []Component
}
type component struct {
name string
action Action
meta vars.Meta
}
func NewComponent(name string, action Action) *component {
return &component{name: name, action: action, meta: vars.NewMeta()}
}
func NoopComponent(name string) *component {
return NewComponent(name, func(_ Evaluator) error {
return nil
})
}
func (c *component) Name() string {
return c.name
}
func (c *component) IsPassThrough() bool {
return false
}
func (c *component) WithMeta(m vars.Meta) Component {
c.meta = c.meta.Merge(m)
return c
}
func (c *component) Meta() vars.Meta {
return c.meta
}
func (c *component) Eval(e Evaluator) error {
if c.action == nil {
return fmt.Errorf("empty node")
} else {
return c.action(e)
}
}