/
encoding_csv_write.go
96 lines (90 loc) · 2.13 KB
/
encoding_csv_write.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
package elem
import (
"bytes"
"encoding/csv"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
var encodingCSVWriteId = uuid.MustParse("fdd1e8e5-6959-4511-bf44-54c1bcbebc12")
var encodingCSVWriteCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: encodingCSVWriteId,
Meta: core.BlueprintMetaDef{
Name: "write CSV",
ShortDescription: "encodes streams into a single string",
Icon: "file-csv",
Tags: []string{"csv", "encoding"},
DocURL: "https://bitspark.de/slang/docs/operator/write-csv",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
Out: core.TypeDef{
Type: "string",
},
In: core.TypeDef{
Type: "stream",
Stream: &core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"col_{columns}": {
Type: "string",
},
},
},
},
},
},
DelegateDefs: map[string]*core.DelegateDef{},
PropertyDefs: map[string]*core.TypeDef{
"includeHeader": {
Type: "boolean",
},
"delimiter": {
Type: "string",
},
"columns": {
Type: "stream",
Stream: &core.TypeDef{
Type: "string",
},
},
},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
for !op.CheckStop() {
rows := in.Pull()
if core.IsMarker(rows) {
out.Push(rows)
continue
}
colNames := op.Property("columns").([]interface{})
includeHeader := op.Property("includeHeader").(bool)
var buf bytes.Buffer
writer := csv.NewWriter(&buf)
writer.Comma = rune(op.Property("delimiter").(string)[0])
if includeHeader {
header := []string{}
for _, c := range colNames {
header = append(header, c.(string))
}
writer.Write(header)
}
for _, r := range rows.([]interface{}) {
cells := r.(map[string]interface{})
record := []string{}
for _, c := range colNames {
record = append(record, cells["col_"+c.(string)].(string))
}
writer.Write(record)
}
writer.Flush()
out.Push(buf.String())
}
},
opConnFunc: func(op *core.Operator, dst, src *core.Port) error {
return nil
},
}