/
string_format.go
71 lines (66 loc) · 1.5 KB
/
string_format.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
package elem
import (
"fmt"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
var stringFormatCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: uuid.MustParse("21dbddf2-2d07-494e-8950-3ac0224a3ff5"),
Meta: core.BlueprintMetaDef{
Name: "format",
ShortDescription: "places values formatted in a C-like manner inside a string",
Icon: "edit",
Tags: []string{"string"},
DocURL: "https://bitspark.de/slang/docs/operator/format",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"format": {
Type: "string",
},
"{variables}": {
Type: "primitive",
},
},
},
Out: core.TypeDef{
Type: "string",
},
},
},
DelegateDefs: map[string]*core.DelegateDef{},
PropertyDefs: map[string]*core.TypeDef{
"variables": {
Type: "stream",
Stream: &core.TypeDef{
Type: "string",
},
},
},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
vars := op.Property("variables").([]interface{})
for !op.CheckStop() {
i := in.Pull()
if core.IsMarker(i) {
out.Push(i)
continue
}
data := i.(map[string]interface{})
format := data["format"].(string)
var vals []interface{}
for _, v := range vars {
val := data[v.(string)]
vals = append(vals, val)
}
out.Push(fmt.Sprintf(format, vals...))
}
},
}