forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.dsl.go
68 lines (56 loc) · 1.63 KB
/
flow.dsl.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
package gou
import (
"fmt"
"path/filepath"
"strings"
jsoniter "github.com/json-iterator/go"
)
// MakeFlow make a flow instance
func MakeFlow() *Flow {
return &Flow{}
}
// DSLCompile compile the FLow
func (flow *Flow) DSLCompile(root string, file string, source map[string]interface{}) error {
data, err := jsoniter.Marshal(source)
if err != nil {
return err
}
fullname, _, _ := flow.nameRouter(root, file)
new := Flow{
Name: fullname,
Source: string(data),
Scripts: map[string]string{},
}
err = jsoniter.Unmarshal(data, &new)
if err != nil {
return err
}
flow.Prepare()
Flows[fullname] = &new
return nil
}
// DSLCheck check the FLow DSL
func (flow *Flow) DSLCheck(source map[string]interface{}) error { return nil }
// DSLRefresh refresh the FLow
func (flow *Flow) DSLRefresh(root string, file string, source map[string]interface{}) error {
fullname, _, _ := flow.nameRouter(root, file)
delete(Flows, fullname)
return flow.DSLCompile(root, file, source)
}
// DSLRemove the DSL
func (flow *Flow) DSLRemove(root string, file string) error {
fullname, _, _ := flow.nameRouter(root, file)
delete(Flows, fullname)
return nil
}
// nameRouter get the model name from router
func (flow *Flow) nameRouter(root string, file string) (fullname string, namespace string, name string) {
dir, filename := filepath.Split(strings.TrimPrefix(file, filepath.Join(root, "flows")))
name = strings.TrimRight(filename, ".flow.yao")
namespace = strings.ReplaceAll(strings.Trim(filepath.ToSlash(dir), "/"), "/", ".")
fullname = name
if namespace != "" {
fullname = fmt.Sprintf("%s.%s", namespace, name)
}
return fullname, namespace, name
}