-
Notifications
You must be signed in to change notification settings - Fork 4
/
processor.go
70 lines (58 loc) · 1.78 KB
/
processor.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
package cmds
import (
"github.com/go-go-golems/glazed/pkg/formatters"
"github.com/go-go-golems/glazed/pkg/middlewares"
"github.com/go-go-golems/glazed/pkg/types"
)
type Processor interface {
ProcessInputObject(obj map[string]interface{}) error
OutputFormatter() formatters.OutputFormatter
}
type GlazeProcessor struct {
of formatters.OutputFormatter
oms []middlewares.ObjectMiddleware
}
func (gp *GlazeProcessor) OutputFormatter() formatters.OutputFormatter {
return gp.of
}
func NewGlazeProcessor(of formatters.OutputFormatter, oms ...middlewares.ObjectMiddleware) *GlazeProcessor {
ret := &GlazeProcessor{
of: of,
oms: oms,
}
return ret
}
// TODO(2022-12-18, manuel) we should actually make it possible to order the columns
// https://github.com/go-go-golems/glazed/issues/56
func (gp *GlazeProcessor) ProcessInputObject(obj map[string]interface{}) error {
currentObjects := []map[string]interface{}{obj}
for _, om := range gp.oms {
nextObjects := []map[string]interface{}{}
for _, obj := range currentObjects {
objs, err := om.Process(obj)
if err != nil {
return err
}
nextObjects = append(nextObjects, objs...)
}
currentObjects = nextObjects
}
gp.of.AddRow(&types.SimpleRow{Hash: obj})
return nil
}
// SimpleGlazeProcessor only collects the output and returns it as a types.Table
type SimpleGlazeProcessor struct {
*GlazeProcessor
formatter *formatters.TableOutputFormatter
}
func NewSimpleGlazeProcessor(oms ...middlewares.ObjectMiddleware) *SimpleGlazeProcessor {
formatter := formatters.NewTableOutputFormatter("csv")
return &SimpleGlazeProcessor{
GlazeProcessor: NewGlazeProcessor(formatter, oms...),
formatter: formatter,
}
}
func (gp *SimpleGlazeProcessor) GetTable() *types.Table {
gp.formatter.Table.Finalize()
return gp.formatter.Table
}