/
stream_map_access.go
86 lines (79 loc) · 1.86 KB
/
stream_map_access.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
package elem
import (
"reflect"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
var streamMapAccessId = uuid.MustParse("618c4007-70fc-44ac-9443-184df77ab730")
var streamMapAccessCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: streamMapAccessId,
Meta: core.BlueprintMetaDef{
Name: "access map",
ShortDescription: "takes a stream of key-value pairs and emits the value with the given key",
Icon: "envelope",
Tags: []string{"stream"},
DocURL: "https://bitspark.de/slang/docs/operator/access-map",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"key": {
Type: "generic",
Generic: "keyType",
},
"stream": {
Type: "stream",
Stream: &core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"key": {
Type: "generic",
Generic: "keyType",
},
"value": {
Type: "generic",
Generic: "valueType",
},
},
},
},
},
},
Out: core.TypeDef{
Type: "generic",
Generic: "valueType",
},
},
},
DelegateDefs: map[string]*core.DelegateDef{},
PropertyDefs: core.TypeDefMap{},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
for !op.CheckStop() {
start:
i := in.Pull()
if core.IsMarker(i) {
out.Push(i)
continue
}
im := i.(map[string]interface{})
key := core.CleanValue(im["key"])
stream := im["stream"].([]interface{})
for _, el := range stream {
elm := el.(map[string]interface{})
ckey := core.CleanValue(elm["key"])
if reflect.DeepEqual(key, ckey) {
out.Push(elm["value"])
goto start
}
}
out.Push(nil)
}
},
}