-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathactions_steps_process.go
More file actions
116 lines (98 loc) · 2.99 KB
/
actions_steps_process.go
File metadata and controls
116 lines (98 loc) · 2.99 KB
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
package diecast
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
// [type=process] Process the output of the previous step by performing a sequence of discrete
//
// operations on the data.
//
// -------------------------------------------------------------------------------------------------
type ProcessStep struct{}
func (step *ProcessStep) Perform(config *StepConfig, w http.ResponseWriter, req *http.Request, prev *StepConfig) (any, error) {
var operations = sliceutil.Sliceify(config.Data)
var data = prev.Output
config.logstep("prev=%v input=%T", prev, data)
for _, o := range operations {
var operation = maputil.M(nil)
var otype string
if typeutil.IsMap(o) {
operation = maputil.M(o)
otype = operation.String(`do`)
} else {
otype = typeutil.String(o)
}
config.logstep("operation=%s", otype)
switch otype {
case `sort`, `rsort`:
if typeutil.IsArray(data) {
var dataS = sliceutil.Sliceify(data)
sort.Slice(dataS, func(i int, j int) bool {
if otype == `rsort` {
return typeutil.String(dataS[i]) > typeutil.String(dataS[j])
} else {
return typeutil.String(dataS[i]) < typeutil.String(dataS[j])
}
})
data = dataS
} else if data == nil {
return make([]any, 0), nil
} else {
return nil, fmt.Errorf("can only sort arrays, got %T", data)
}
case `diffuse`:
var sep = operation.String(`separator`, `.`)
var joiner = operation.String(`joiner`, `=`)
var dataM = make(map[string]any)
if typeutil.IsArray(data) {
for i, item := range sliceutil.Sliceify(data) {
if typeutil.IsScalar(item) {
k, v := stringutil.SplitPair(typeutil.String(item), joiner)
k = strings.TrimLeft(k, sep)
if k == `` {
k = typeutil.String(i)
}
dataM[k] = typeutil.Auto(v)
} else {
dataM[typeutil.String(i)] = item
}
}
} else if typeutil.IsMap(data) {
dataM = maputil.M(data).MapNative()
} else {
return nil, fmt.Errorf("can only diffuse arrays or maps, got %T", data)
}
if diffused, err := maputil.DiffuseMap(dataM, sep); err == nil {
data = diffused
} else {
return nil, err
}
case `join`:
var sep = operation.String(`separator`, "\n")
var kvjoin = operation.String(`joiner`, "=")
var lines []string
if typeutil.IsArray(data) {
for _, item := range sliceutil.Sliceify(data) {
if typeutil.IsMap(item) {
var l = maputil.Join(item, kvjoin, sep)
lines = append(lines, strings.Split(l, sep)...)
} else if typeutil.IsScalar(item) {
lines = append(lines, typeutil.String(item))
}
}
} else if typeutil.IsMap(data) {
return maputil.Join(maputil.M(data).MapNative(), kvjoin, sep), nil
}
return strings.Join(lines, sep), nil
default:
return nil, fmt.Errorf("unrecognized process operation %q", otype)
}
}
return data, nil
}