-
Notifications
You must be signed in to change notification settings - Fork 16
/
sync.go
189 lines (170 loc) · 5.11 KB
/
sync.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
package dynaml
import (
"fmt"
"github.com/mandelsoft/spiff/debug"
"github.com/mandelsoft/spiff/yaml"
"time"
)
type SyncExpr struct {
A Expression
Cond Expression
Value Expression
Timeout Expression
function bool
first time.Time
last time.Time
}
func (e SyncExpr) Evaluate(binding Binding, locally bool) (interface{}, EvaluationInfo, bool) {
resolved := true
var value interface{}
var info EvaluationInfo
if e.first.IsZero() {
e.first = time.Now()
}
errmsg := ""
timeout := 5 * time.Minute
if e.Timeout != nil {
if _, ok := e.Timeout.(DefaultExpr); !ok {
t, infot, ok := ResolveIntegerExpressionOrPushEvaluation(&e.Timeout, &resolved, nil, binding, false)
if !ok {
return nil, infot, ok
}
if !resolved {
return e, info, true
}
timeout = time.Second * time.Duration(t)
}
}
result := map[string]yaml.Node{}
expr := e.A
debug.Debug("sync value: %s\n", expr)
value, infoe, ok := ResolveExpressionOrPushEvaluation(&expr, &resolved, nil, binding, false)
if !ok {
errmsg = infoe.Issue.Issue
debug.Debug("sync arg failed: %s\n", errmsg)
result[CATCH_VALID] = NewNode(false, binding)
result[CATCH_ERROR] = NewNode(errmsg, binding)
} else {
if !resolved {
return e, info, true
}
debug.Debug("sync arg succeeded\n")
result[CATCH_VALID] = NewNode(true, binding)
result[CATCH_ERROR] = NewNode("", binding)
result[CATCH_VALUE] = NewNode(value, binding)
}
condbinding := binding
if e.function {
condbinding = binding.WithLocalScope(result)
}
inline := isInline(e.Cond) && !e.function
cond, infoc, ok := e.Cond.Evaluate(condbinding, false)
if !ok {
return info.AnnotateError(infoc, "condition evaluation failed)")
}
if lambda, ok := cond.(LambdaValue); ok {
args := []interface{}{}
if result[CATCH_VALUE] != nil {
args = append(args, result[CATCH_VALUE].Value())
} else {
args = append(args, nil)
}
switch len(lambda.lambda.Parameters) {
case 1:
case 2:
debug.Debug("setting 2nd condition arg to error: %s\n", result[CATCH_ERROR].Value())
args = append(args, result[CATCH_ERROR].Value())
default:
return info.Error("lambda expression for sync condition must take one or two arguments, found %d", len(lambda.lambda.Parameters))
}
resolved, result, sub, ok := lambda.Evaluate(inline, false, false, nil, args, binding, locally)
if !resolved {
return e, sub, ok
}
cond = result
} else {
if !e.function {
return info.Error("sync condition must evaluate to a lambda value")
}
}
switch v := cond.(type) {
case bool:
if !v {
e.last = time.Now()
if e.last.Before(e.first.Add(timeout)) {
debug.Debug("sync failed but timeout not reached -> try again\n")
return e, infoe, true
}
if errmsg != "" {
debug.Debug("sync condition is finally false, err: %s\n", infoe.Issue.Issue)
return nil, infoe, false
} else {
debug.Debug("sync condition is finally false\n")
return info.Error("sync timeout reached")
}
} else {
debug.Debug("sync condition is true\n")
}
case Expression:
return e, info, true
default:
return info.Error("condition must evaluate to bool")
}
if !isDefaulted(e.Value) {
debug.Debug("evaluating sync value\n")
inline = isInline(e.Value) && !e.function
value, infov, ok := e.Value.Evaluate(binding.WithLocalScope(result), false)
if !ok {
return info.AnnotateError(infoc, "value expression failed)")
}
if IsExpression(value) {
return e, infov, true
}
if lambda, ok := value.(LambdaValue); ok && !e.function {
args := []interface{}{}
if result[CATCH_VALUE] != nil {
debug.Debug("setting first value arg to value: %v\n", result[CATCH_VALUE].Value())
args = append(args, result[CATCH_VALUE].Value())
} else {
debug.Debug("setting first value arg to nil\n")
args = append(args, nil)
}
switch len(lambda.lambda.Parameters) {
case 1:
case 2:
debug.Debug("setting 2nd value arg to error: %s\n", result[CATCH_ERROR].Value())
args = append(args, result[CATCH_ERROR].Value())
default:
return info.Error("lambda expression for sync value must take one or two arguments, found %d", len(lambda.lambda.Parameters))
}
resolved, result, sub, ok := lambda.Evaluate(inline, false, false, nil, args, binding, locally)
if !resolved {
return e, sub, ok
}
return result, sub, ok
} else {
if !e.function {
return info.Error("sync value expression must evaluate to lambda expression")
}
}
return value, infov, ok
}
debug.Debug("returning sync value\n")
return value, infoe, ok
}
func (e SyncExpr) String() string {
return fmt.Sprintf("sync(%s)", e.A)
}
func (e CallExpr) sync(binding Binding) (interface{}, EvaluationInfo, bool) {
var info EvaluationInfo
switch len(e.Arguments) {
case 2:
return &SyncExpr{A: e.Arguments[0], Cond: e.Arguments[1], function: true}, info, true
case 3:
return &SyncExpr{A: e.Arguments[0], Cond: e.Arguments[1], Value: e.Arguments[2], function: true}, info, true
case 4:
return &SyncExpr{A: e.Arguments[0], Cond: e.Arguments[1], Value: e.Arguments[2], Timeout: e.Arguments[3], function: true}, info, true
default:
return info.Error("2 or 3 arguments required for sync")
}
}