-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvar.go
106 lines (85 loc) · 1.58 KB
/
var.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
package loong
import (
"context"
"fmt"
"strings"
)
type Setter interface {
Set(string, any)
}
type Getter interface {
Get(k string) (v any, ok bool)
}
type Putter interface {
Put(string, any)
}
type Var map[string]any
func NewVar() Var {
return make(Var)
}
func (b Var) Set(k string, v any) {
b.Put(k, v)
}
func (b Var) Put(k string, v any) Var {
b[k] = v
return b
}
func (b Var) Get(k string) (v any, ok bool) {
v, ok = b[k]
return
}
func Merge(dest, src Var) Var {
if dest == nil {
dest = NewVar()
}
if len(src) == 0 {
return dest
}
for k, v := range src {
dest[k] = v
}
return dest
}
type Variable struct {
Param Var
Input Var
Exec
BpmnElement
isVarChanged bool
}
func (v *Variable) Put(key string, val any) {
part := strings.Split(key, ".")
if len(part) < 2 {
panic("设置变量时必须指定作用域")
}
v.PutScope(part[0], part[1], val)
}
func (v *Variable) PutScope(s, k string, val any) {
switch s {
case "Var":
v.PutVar(k, val)
case "Input":
v.PutInput(k, val)
default:
panic(fmt.Errorf("作用域: %s 不支持写入", s))
}
}
func (v *Variable) PutVar(key string, val any) {
v.isVarChanged = true
v.Exec.ProcInst.Var.Set(key, val)
}
func (v *Variable) PutInput(key string, val any) {
v.Input.Set(key, val)
}
func (v *Variable) PutParam(key string, val any) {
v.Param.Set(key, val)
}
func (v Variable) Eval(ctx context.Context, el string) (any, error) {
return v.Evaluator.Eval(ctx, el, v)
}
func (v Variable) saveTo(ctx context.Context, s Storer) error {
if v.isVarChanged {
return s.SaveVar(ctx, v.ProcInst)
}
return nil
}