forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.go
190 lines (153 loc) · 3.9 KB
/
pipe.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package pipe
import (
"errors"
"fmt"
"strings"
"github.com/yaoapp/gou/application"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
)
var pipes = map[string]*Pipe{}
// Load the pipe
func Load(cfg config.Config) error {
exts := []string{"*.pip.yao", "*.pipe.yao"}
errs := []error{}
err := application.App.Walk("pipes", func(root, file string, isdir bool) error {
if isdir {
return nil
}
id := share.ID(root, file)
pipe, err := NewFile(file, root)
if err != nil {
errs = append(errs, err)
return err
}
Set(id, pipe)
return err
}, exts...)
if len(errs) > 0 {
return errors.Join(errs...)
}
return err
}
// New create Pipe
func New(source []byte) (*Pipe, error) {
pipe := Pipe{}
err := application.Parse("<source>.yao", source, &pipe)
if err != nil {
return nil, fmt.Errorf("parse pipe: %s", err)
}
err = (&pipe).build()
if err != nil {
return nil, fmt.Errorf("build pipe: %s", err)
}
return &pipe, nil
}
// NewFile create pipe from file
func NewFile(file string, root string) (*Pipe, error) {
source, err := application.App.Read(file)
if err != nil {
return nil, err
}
id := share.ID(root, file)
pipe := Pipe{ID: id}
err = application.Parse(file, source, &pipe)
if err != nil {
return nil, err
}
err = (&pipe).build()
if err != nil {
return nil, err
}
return &pipe, nil
}
// Set pipe to
func Set(id string, pipe *Pipe) {
pipes[id] = pipe
}
// Remove the pipe
func Remove(id string) {
if _, has := pipes[id]; has {
delete(pipes, id)
}
}
// Get the pipe
func Get(id string) (*Pipe, error) {
if pipe, has := pipes[id]; has {
return pipe, nil
}
return nil, fmt.Errorf("pipe %s not found", id)
}
// Build the pipe
func (pipe *Pipe) build() error {
if pipe.Nodes == nil || len(pipe.Nodes) == 0 {
return fmt.Errorf("pipe: %s nodes is required", pipe.Name)
}
return pipe._build()
}
// HasNodes check if the pipe has nodes
func (pipe *Pipe) HasNodes() bool {
return pipe.Nodes != nil && len(pipe.Nodes) > 0
}
func (pipe *Pipe) _build() error {
pipe.mapping = map[string]*Node{}
if pipe.Nodes == nil {
return nil
}
for i, node := range pipe.Nodes {
if node.Name == "" {
return fmt.Errorf("pipe: %s nodes[%d] name is required", pipe.Name, i)
}
pipe.Nodes[i].index = i
pipe.mapping[node.Name] = &pipe.Nodes[i]
// Set the label of the node
if node.Label == "" {
pipe.Nodes[i].Label = strings.ToUpper(node.Name)
}
// Set the type of the node
if node.Process != nil {
pipe.Nodes[i].Type = "process"
// Validate the process
if node.Process.Name == "" {
return fmt.Errorf("pipe: %s nodes[%d] process name is required", pipe.Name, i)
}
// Security check
if pipe.Whitelist != nil {
if _, has := pipe.Whitelist[node.Process.Name]; !has {
return fmt.Errorf("pipe: %s nodes[%d] process %s is not in the whitelist", pipe.Name, i, node.Process.Name)
}
}
continue
} else if node.Request != nil {
pipe.Nodes[i].Type = "request"
continue
} else if node.Prompts != nil {
pipe.Nodes[i].Type = "ai"
continue
} else if node.UI != "" {
pipe.Nodes[i].Type = "user-input"
if node.UI != "cli" && node.UI != "web" && node.UI != "app" && node.UI != "wxapp" { // Vaildate the UI type
return fmt.Errorf("pipe: %s nodes[%d] the type of the UI must be cli, web, app, wxapp", pipe.Name, i)
}
continue
} else if node.Switch != nil {
pipe.Nodes[i].Type = "switch"
for key, pip := range node.Switch {
key = ref(key)
pip.Whitelist = pipe.Whitelist // Copy the whitelist
pip.namespace = node.Name
pip.parent = pipe
if pip.ID == "" {
pip.ID = fmt.Sprintf("%s.%s#%s", pipe.ID, node.Name, key)
}
if pip.Name == "" {
pip.Name = fmt.Sprintf("%s(%s#%s)", pipe.Name, node.Name, key)
}
pip._build()
}
continue
}
return fmt.Errorf("pipe: %s nodes[%d] process, request, case, prompts or ui is required at least one", pipe.Name, i)
}
return nil
}