/
builder.go
484 lines (422 loc) · 12.9 KB
/
builder.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// MIT License
//
// Copyright (c) 2023 Lack
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package flow
import (
"reflect"
"strings"
json "github.com/json-iterator/go"
"github.com/olive-io/bpmn/schema"
"github.com/vine-io/flow/api"
"github.com/vine-io/flow/builder"
"github.com/vine-io/pkg/xname"
)
type IStepBuilder interface {
Id() string
Entity() Entity
Build() (any, error)
}
// WorkflowStepBuilder the builder pattern is used to separate the construction of a complex object of its
// representation so that the same construction process can create different representations.
type WorkflowStepBuilder struct {
step *api.WorkflowStep
worker string
entity Entity
}
func NewStepBuilder(step Step, worker string, entity Entity) *WorkflowStepBuilder {
s := StepToWorkStep(step, worker)
s.EntityId = entity.GetEID()
if s.Uid != "" {
s.Uid = "Step_" + HashName(GetTypePkgName(reflect.TypeOf(step))) + "_" + HashName(entity.GetEID())
}
return &WorkflowStepBuilder{step: s, worker: worker, entity: entity}
}
func (b *WorkflowStepBuilder) SetId(id string) *WorkflowStepBuilder {
b.step.Uid = id
return b
}
func (b *WorkflowStepBuilder) Id() string {
return b.step.Uid
}
func (b *WorkflowStepBuilder) Entity() Entity {
return b.entity
}
func (b *WorkflowStepBuilder) Build() (any, error) {
return b.step.DeepCopy(), nil
}
// SubWorkflowBuilder the builder pattern is used to separate the construction of a complex object of its
// representation so that the same construction sub process can create different representations.
type SubWorkflowBuilder struct {
options *api.WorkflowOption
entities map[string]string
items map[string]string
steps []IStepBuilder
entity Entity
}
// NewSubWorkflowBuilder returns a new instance of the SubWorkflowBuilder struct.
// The builder can be used to construct a Workflow struct with specific options.
func NewSubWorkflowBuilder(entity Entity, opts ...Option) *SubWorkflowBuilder {
options := NewOptions(opts...)
wb := &SubWorkflowBuilder{
options: options,
entities: map[string]string{},
items: map[string]string{},
steps: []IStepBuilder{},
entity: entity,
}
return wb
}
//func FromSpec(spec *api.Workflow) *WorkflowBuilder {
// return &WorkflowBuilder{spec: spec}
//}
// Items adds a map of key-value pairs to the Workflow struct.
func (b *SubWorkflowBuilder) Items(items map[string]any) *SubWorkflowBuilder {
for k, v := range items {
b.Item(k, v)
}
return b
}
// Item adds a key-value pairs to the Workflow struct.
func (b *SubWorkflowBuilder) Item(key string, value any) *SubWorkflowBuilder {
if b.items == nil {
b.items = map[string]string{}
}
var vv string
switch tv := value.(type) {
case []byte:
vv = string(tv)
case string:
vv = tv
default:
data, _ := json.Marshal(value)
vv = string(data)
}
b.items[key] = vv
return b
}
// Step adds Step interface implementations to the Workflow struct.
func (b *SubWorkflowBuilder) Step(sb IStepBuilder) *SubWorkflowBuilder {
if b.steps == nil {
b.steps = make([]IStepBuilder, 0)
}
if b.entities == nil {
b.entities = map[string]string{}
}
b.steps = append(b.steps, sb)
vv, _ := json.Marshal(sb.Entity())
b.entities[sb.Id()] = string(vv)
return b
}
// Steps adds a slice of Step interface implementations to the Workflow struct.
func (b *SubWorkflowBuilder) Steps(sbs ...IStepBuilder) *SubWorkflowBuilder {
for i := range sbs {
sb := sbs[i]
b.Step(sb)
}
return b
}
type subProcessWorkflow struct {
builder *builder.SubProcessBuilder
dataObjects map[string]string
properties map[string]string
}
func (b *SubWorkflowBuilder) Id() string {
return b.options.Wid
}
func (b *SubWorkflowBuilder) Entity() Entity {
return b.entity
}
func (b *SubWorkflowBuilder) Build() (any, error) {
pb := builder.NewSubProcessDefinitionsBuilder(b.options.Name)
pb.Id(b.options.Wid)
pb.Start()
for key, item := range b.items {
keyText := OliveEscape(key)
pb.SetProperty(keyText, item)
}
dataObjects := make(map[string]string)
for key, value := range b.entities {
dataObjects[key] = value
}
mappingPrefix := "__step_mapping__"
for idx := range b.steps {
sb := b.steps[idx]
out, err := sb.Build()
if err != nil {
return nil, err
}
if step, ok := out.(*api.WorkflowStep); ok {
task := builder.NewServiceTaskBuilder(step.Describe, "dr-service")
task.SetId(step.Uid)
name := OliveEscape(step.Name)
task.SetHeader("stepName", name)
task.SetHeader("worker", step.Worker)
task.SetHeader("entity", step.Entity)
task.SetHeader("entityId", step.EntityId)
task.SetHeader("describe", step.Describe)
task.SetHeader("injects", strings.Join(step.Injects, ","))
pb.SetProperty(mappingPrefix+step.Uid, step.Worker)
task.SetProperty(mappingPrefix+step.Uid, "")
task.SetProperty("action", "")
pb.AppendElem(task.Out())
}
}
if b.entity != nil {
pb.SetProperty("entityId", b.entity.GetEID())
}
pb.SetProperty("entity", GetTypePkgName(reflect.TypeOf(b.entity)))
pb.SetProperty("action", api.StepAction_SC_PREPARE.Readably())
pb.End()
properties := pb.PopProperty()
process := pb.Out()
out := &subProcessWorkflow{
builder: process,
dataObjects: dataObjects,
properties: properties,
}
return out, nil
}
// WorkflowBuilder the builder pattern is used to separate the construction of a complex object of its
// representation so that the same construction process can create different representations.
type WorkflowBuilder struct {
options *api.WorkflowOption
entities map[string]string
items map[string]string
entity Entity
steps []IStepBuilder
}
// NewWorkFlowBuilder returns a new instance of the WorkflowBuilder struct.
// The builder can be used to construct a Workflow struct with specific options.
func NewWorkFlowBuilder(opts ...Option) *WorkflowBuilder {
options := NewOptions(opts...)
wb := &WorkflowBuilder{
options: options,
entities: map[string]string{},
items: map[string]string{},
steps: []IStepBuilder{},
}
return wb
}
// Items adds a map of key-value pairs to the Workflow struct.
func (b *WorkflowBuilder) Items(items map[string]any) *WorkflowBuilder {
for k, v := range items {
b.Item(k, v)
}
return b
}
// Item adds a key-value pairs to the Workflow struct.
func (b *WorkflowBuilder) Item(key string, value any) *WorkflowBuilder {
if b.items == nil {
b.items = map[string]string{}
}
var vv string
switch tv := value.(type) {
case []byte:
vv = string(tv)
case string:
vv = tv
default:
data, _ := json.Marshal(value)
vv = string(data)
}
b.items[key] = vv
return b
}
// Step adds Step interface implementations to the Workflow struct.
func (b *WorkflowBuilder) Step(sb IStepBuilder) *WorkflowBuilder {
if b.steps == nil {
b.steps = make([]IStepBuilder, 0)
}
if b.entities == nil {
b.entities = map[string]string{}
}
b.steps = append(b.steps, sb)
vv, _ := json.Marshal(sb.Entity())
b.entities[sb.Id()] = string(vv)
return b
}
// Steps adds a slice of Step interface implementations to the Workflow struct.
func (b *WorkflowBuilder) Steps(sbs ...IStepBuilder) *WorkflowBuilder {
for i := range sbs {
sb := sbs[i]
b.Step(sb)
}
return b
}
func (b *WorkflowBuilder) ToProcessDefinitions() (*schema.Definitions, map[string]string, map[string]string, error) {
pb := builder.NewProcessDefinitionsBuilder(b.options.Name)
pb.Id(b.options.Wid)
pb.Start()
for key, item := range b.items {
keyText := OliveEscape(key)
pb.SetProperty(keyText, item)
}
dataObjects := make(map[string]string)
for key, value := range b.entities {
dataObjects[key] = value
}
properties := make(map[string]string)
mappingPrefix := "__step_mapping__"
for idx := range b.steps {
sb := b.steps[idx]
out, err := sb.Build()
if err != nil {
return nil, nil, nil, err
}
if step, ok := out.(*api.WorkflowStep); ok {
task := builder.NewServiceTaskBuilder(step.Describe, "dr-service")
task.SetId(step.Uid)
name := OliveEscape(step.Name)
task.SetHeader("stepName", name)
task.SetHeader("worker", step.Worker)
task.SetHeader("entity", step.Entity)
task.SetHeader("entityId", step.EntityId)
task.SetHeader("describe", step.Describe)
task.SetHeader("injects", strings.Join(step.Injects, ","))
pb.SetProperty(mappingPrefix+step.Uid, step.Worker)
task.SetProperty(mappingPrefix+step.Uid, "")
task.SetProperty("action", "")
pb.AppendElem(task.Out())
}
if sp, ok := out.(*subProcessWorkflow); ok {
pb.AppendElem(sp.builder)
for key, ds := range sp.dataObjects {
if _, ok1 := dataObjects[key]; !ok1 {
dataObjects[key] = ds
}
}
for key, property := range sp.properties {
if _, ok1 := properties[key]; !ok1 {
properties[key] = property
}
}
}
}
pb.SetProperty("action", api.StepAction_SC_PREPARE.Readably())
pb.End()
for key, property := range pb.PopProperty() {
properties[key] = property
}
d, err := pb.ToDefinitions()
if err != nil {
return nil, nil, nil, err
}
return d, dataObjects, properties, nil
}
func (b *WorkflowBuilder) SetEntity(entity Entity) *WorkflowBuilder {
b.entity = entity
return b
}
func (b *WorkflowBuilder) ToSubProcessDefinitions() (*schema.Definitions, map[string]string, map[string]string, error) {
pb := builder.NewSubProcessDefinitionsBuilder(b.options.Name)
pb.Id(b.options.Wid)
pb.Start()
for key, item := range b.items {
keyText := OliveEscape(key)
pb.SetProperty(keyText, item)
}
dataObjects := make(map[string]string, 0)
for key, value := range b.entities {
dataObjects[key] = value
}
properties := make(map[string]string)
mappingPrefix := "__step_mapping__"
for idx := range b.steps {
sb := b.steps[idx]
out, err := sb.Build()
if err != nil {
return nil, nil, nil, err
}
if step, ok := out.(*api.WorkflowStep); ok {
task := builder.NewServiceTaskBuilder(step.Describe, "dr-service")
task.SetId(step.Uid)
name := OliveEscape(step.Name)
task.SetHeader("stepName", name)
task.SetHeader("worker", step.Worker)
task.SetHeader("entity", step.Entity)
task.SetHeader("entityId", step.EntityId)
task.SetHeader("describe", step.Describe)
task.SetHeader("injects", strings.Join(step.Injects, ","))
pb.SetProperty(mappingPrefix+step.Uid, step.Worker)
task.SetProperty(mappingPrefix+step.Uid, "")
task.SetProperty("action", "")
pb.AppendElem(task.Out())
}
if sp, ok := out.(*subProcessWorkflow); ok {
pb.AppendElem(sp.builder)
for key, ds := range sp.dataObjects {
if _, ok1 := dataObjects[key]; !ok1 {
dataObjects[key] = ds
}
}
for key, property := range sp.properties {
if _, ok1 := properties[key]; !ok1 {
properties[key] = property
}
}
}
}
if b.entity != nil {
pb.SetProperty("entityId", b.entity.GetEID())
}
pb.SetProperty("entity", GetTypePkgName(reflect.TypeOf(b.entity)))
pb.SetProperty("action", api.StepAction_SC_PREPARE.Readably())
pb.End()
for key, property := range pb.PopProperty() {
properties[key] = property
}
d := pb.ToDefinitions()
return d, dataObjects, properties, nil
}
// Option represents a configuration option for Workflow struct.
// It provides a way to modify the fields of the Workflow struct a flexible ways.
type Option func(option *api.WorkflowOption)
func NewOptions(opts ...Option) *api.WorkflowOption {
var options api.WorkflowOption
for _, o := range opts {
o(&options)
}
if options.Wid == "" {
options.Wid = "WF_" + xname.Gen6()
}
if options.Name == "" {
options.Name = GetTypePkgName(reflect.TypeOf(&api.Workflow{}))
}
return &options
}
// WithName sets the Name field of *api.WorkflowOption to the specified value.
func WithName(name string) Option {
return func(o *api.WorkflowOption) {
o.Name = name
}
}
// WithId sets the Wid field of *api.WorkflowOption to the specified value.
func WithId(id string) Option {
return func(o *api.WorkflowOption) {
o.Wid = id
}
}
// WithMaxRetry sets the MaxRetries field of *api.WorkflowOption to the specified value.
func WithMaxRetry(retry int32) Option {
return func(o *api.WorkflowOption) {
o.MaxRetries = retry
}
}