/
zSliceAppend.go
55 lines (42 loc) · 960 Bytes
/
zSliceAppend.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
package netroutine
import (
"context"
"encoding/json"
"fmt"
)
func init() {
blocks[idSliceAppend] = &SliceAppend{}
}
const idSliceAppend = "SliceAppend"
type SliceAppend struct {
ToKey string
FromKey string
}
func (b *SliceAppend) toBytes() ([]byte, error) {
return json.Marshal(b)
}
func (b *SliceAppend) fromBytes(bytes []byte) error {
return json.Unmarshal(bytes, b)
}
func (b *SliceAppend) kind() string {
return idSliceAppend
}
func (b *SliceAppend) Run(ctx context.Context, wce *Environment) (string, Status) {
v, ok := wce.getData(b.ToKey)
if !ok {
var newS []interface{}
wce.setData(b.ToKey, newS)
v = newS
}
sl, ok := v.([]interface{})
if !ok {
return log(b, reportWrongType(b.ToKey), Error)
}
v, ok = wce.getData(b.FromKey)
if !ok {
return log(b, missingWorkingData(b.FromKey), Error)
}
sl = append(sl, v)
wce.setData(b.ToKey, sl)
return log(b, setWorkingData(b.ToKey, fmt.Sprintf("%v", sl)), Success)
}