/
stream_transform.go
124 lines (114 loc) · 2.64 KB
/
stream_transform.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
package elem
import (
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
var streamTransformCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: uuid.MustParse("dce082cb-7272-4e85-b4fa-740778e8ba8d"),
Meta: core.BlueprintMetaDef{
Name: "transform stream",
ShortDescription: "transforms a stream by iterating it using an iterator delegate",
Icon: "code-commit",
Tags: []string{"stream"},
DocURL: "https://bitspark.de/slang/docs/operator/map-to-stream",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"initial": {
Type: "generic",
Generic: "stateType",
},
"items": {
Type: "stream",
Stream: &core.TypeDef{
Type: "generic",
Generic: "inItemType",
},
},
},
},
Out: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"result": {
Type: "generic",
Generic: "stateType",
},
"items": {
Type: "stream",
Stream: &core.TypeDef{
Type: "generic",
Generic: "outItemType",
},
},
},
},
},
},
DelegateDefs: map[string]*core.DelegateDef{
"iterator": {
Out: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"state": {
Type: "generic",
Generic: "stateType",
},
"item": {
Type: "generic",
Generic: "inItemType",
},
},
},
In: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"state": {
Type: "generic",
Generic: "stateType",
},
"item": {
Type: "generic",
Generic: "outItemType",
},
},
},
},
},
PropertyDefs: map[string]*core.TypeDef{},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
iterator := op.Delegate("iterator")
iteratorOut := iterator.Out()
iteratorIn := iterator.In()
for !op.CheckStop() {
state := in.Map("initial").Pull()
if core.IsMarker(state) {
in.Map("items").Pull()
out.Push(state)
}
in.Map("items").PullBOS()
out.Map("items").PushBOS()
for {
item := in.Map("items").Stream().Pull()
if in.Map("items").OwnEOS(item) {
break
}
iteratorOut.Map("item").Push(item)
iteratorOut.Map("state").Push(state)
state = iteratorIn.Map("state").Pull()
outItem := iteratorIn.Map("item").Pull()
out.Map("items").Stream().Push(outItem)
}
out.Map("items").PushEOS()
out.Map("result").Push(state)
}
},
}