-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathelement.go
More file actions
132 lines (102 loc) · 2.67 KB
/
Copy pathelement.go
File metadata and controls
132 lines (102 loc) · 2.67 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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"`
Priority int `json:"Priority,omitempty"`
WrapperAlias map[string]string `json:"WrapperAlias,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) WrapByAlias(wrapper string, alias string) *Element {
if e.WrapperAlias == nil {
e.WrapperAlias = make(map[string]string)
}
e.WrapperAlias[alias] = wrapper
e.Wrap(alias)
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
}
func (e *Element) SetPriority(priority int) *Element {
e.Priority = priority
return e
}
func (e *Element) GetPriority() int {
return e.Priority
}
type PGraph = internal.Graph[*Element]
func NewElement(name string) *Element {
return &Element{Name: name}
}