-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelement.go
111 lines (85 loc) · 2.21 KB
/
element.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ograph
import (
"context"
"strings"
"github.com/symphony09/ograph/internal"
"github.com/symphony09/ograph/ogcore"
)
type Element struct {
Virtual bool `json:"Virtual,omitempty"`
Name string
FactoryName string `json:"FactoryName,omitempty"`
Wrappers []string `json:"Wrappers,omitempty"`
ParamsMap map[string]any `json:"ParamsMap,omitempty"`
DefaultImpl string `json:"DefaultImpl,omitempty"`
Singleton ogcore.Node `json:"-"`
PrivateFactory func() ogcore.Node `json:"-"`
SubElements []*Element `json:"SubElements,omitempty"`
}
func (e *Element) SetVirtual(isVirtual bool) *Element {
e.Virtual = isVirtual
return e
}
func (e *Element) AsVirtual() *Element {
e.Virtual = true
return e
}
func (e *Element) UseFactory(name string, subElements ...*Element) *Element {
e.FactoryName = name
e.SubElements = append(e.SubElements, subElements...)
return e
}
func (e *Element) UsePrivateFactory(factory func() ogcore.Node, subElements ...*Element) *Element {
e.PrivateFactory = factory
e.SubElements = append(e.SubElements, subElements...)
return e
}
func (e *Element) Wrap(wrappers ...string) *Element {
e.Wrappers = append(e.Wrappers, wrappers...)
return e
}
func (e *Element) UseNode(node ogcore.Node) *Element {
e.Singleton = node
return e
}
func (e *Element) UseFn(fn func() error) *Element {
e.Singleton = &BaseNode{
Action: func(ctx context.Context, state ogcore.State) error {
return fn()
}}
return e
}
func (e *Element) Params(key string, val any) *Element {
if e.ParamsMap == nil {
e.ParamsMap = make(map[string]any)
}
e.ParamsMap[key] = val
return e
}
func (e *Element) filterParams(owner string) map[string]any {
if e.ParamsMap == nil {
return nil
}
ret := make(map[string]any)
for key, val := range e.ParamsMap {
if belong, paramKey, ok := strings.Cut(key, "."); ok {
if belong == owner {
ret[paramKey] = val
}
} else {
ret[key] = val
}
}
return ret
}
type ElementOption func(e *Element)
func (e *Element) Apply(options ...ElementOption) *Element {
for _, option := range options {
option(e)
}
return e
}
type PGraph = internal.Graph[*Element]
func NewElement(name string) *Element {
return &Element{Name: name}
}