/
batch.go
70 lines (58 loc) · 1.54 KB
/
batch.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
// Copyright (c) 2023 Blockwatch Data Inc.
// Author: alex@blockwatch.cc, abdul@blockwatch.cc
package task
import (
"fmt"
"blockwatch.cc/tzgo/codec"
"blockwatch.cc/tzgo/internal/compose"
"blockwatch.cc/tzgo/internal/compose/alpha"
"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/signer"
"github.com/pkg/errors"
)
var _ alpha.TaskBuilder = (*BatchTask)(nil)
func init() {
alpha.RegisterTask("batch", NewBatchTask)
}
type BatchTask struct {
BaseTask
}
func NewBatchTask() alpha.TaskBuilder {
return &BatchTask{}
}
func (t *BatchTask) Type() string {
return "batch"
}
func (t *BatchTask) Build(ctx compose.Context, task alpha.Task) (*codec.Op, *rpc.CallOptions, error) {
if err := t.parse(ctx, task); err != nil {
return nil, nil, errors.Wrap(err, "parse")
}
opts := rpc.NewCallOptions()
opts.Signer = signer.NewFromKey(t.Key)
op := codec.NewOp().WithSource(t.Source)
for i, ct := range task.Contents {
// use common source
ct.Source = task.Source
childTask, err := alpha.NewTask(ct.Type)
if err != nil {
return nil, nil, fmt.Errorf("batch[%d]: %v", i, err)
}
childOp, _, err := childTask.Build(ctx, ct)
if err != nil {
return nil, nil, fmt.Errorf("batch[%d]: %v", i, err)
}
for _, o := range childOp.Contents {
op.WithContents(o)
}
}
return op, opts, nil
}
func (t *BatchTask) Validate(ctx compose.Context, task alpha.Task) error {
return t.parse(ctx, task)
}
func (t *BatchTask) parse(ctx compose.Context, task alpha.Task) (err error) {
if err = t.BaseTask.parse(ctx, task); err != nil {
return err
}
return
}