-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry_print.go
178 lines (157 loc) · 4.68 KB
/
registry_print.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package reg
import (
"bytes"
"encoding/json"
"fmt"
"io"
"sort"
"strings"
)
type ProcessingSteps []JsonProcessingStep
func (slice ProcessingSteps) Len() int {
return len(slice)
}
func (slice ProcessingSteps) Less(i, j int) bool {
// Sort the forks after the regular processing steps
return (!slice[i].IsFork && slice[j].IsFork) || slice[i].Name < slice[j].Name
}
func (slice ProcessingSteps) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type JsonProcessingStep struct {
Name string
IsFork bool
IsBatch bool
Description string
Params JsonParameters
}
type JsonParameters []JsonParameter
func (slice JsonParameters) Len() int {
return len(slice)
}
func (slice JsonParameters) Less(i, j int) bool {
return slice[i].Name < slice[j].Name
}
func (slice JsonParameters) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type JsonParameter struct {
Name string
Type string
Default interface{}
Required bool
Description string
}
func makeJsonProcessingStep(reg RegisteredStep, batch, fork bool) JsonProcessingStep {
return JsonProcessingStep{
Name: reg.Name,
Description: reg.Description,
IsFork: fork,
IsBatch: batch,
Params: makeJsonParameters(reg.Params),
}
}
func makeJsonParameters(params RegisteredParameters) JsonParameters {
result := make(JsonParameters, 0, len(params))
for _, param := range params {
result = append(result, JsonParameter{
Name: param.Name,
Type: param.Parser.String(),
Default: param.Default,
Required: param.Required,
Description: param.Description,
})
}
sort.Sort(result)
return result
}
func (s *JsonProcessingStep) formatTo(buf *bytes.Buffer) {
buf.WriteString("\n - ")
buf.WriteString(s.Name)
if s.Description != "" {
descPrefix := "\n Description: "
buf.WriteString(descPrefix)
buf.WriteString(s.indentDescription(descPrefix, s.Description))
}
if len(s.Params) > 0 {
buf.WriteString("\n Parameters:")
for _, param := range s.Params {
requiredOrOptional := "required"
if !param.Required {
requiredOrOptional = fmt.Sprintf("optional, default: %v", param.Default)
}
paramLine := fmt.Sprintf("\n %v (%v), %v", param.Name, param.Type, requiredOrOptional)
buf.WriteString(paramLine)
if param.Description != "" {
descPrefix := ". Description: "
buf.WriteString(descPrefix)
buf.WriteString(s.indentDescription(paramLine+descPrefix, param.Description))
}
}
}
}
func (s *JsonProcessingStep) indentDescription(lineStart, description string) string {
// Replace newline-characters appropriately to ensure correct indentation of multi-line description text
replacement := "\n" + strings.Repeat(" ", len(strings.TrimPrefix(lineStart, "\n")))
return strings.ReplaceAll(description, "\n", replacement)
}
func (r ProcessorRegistry) getSortedProcessingSteps() (steps ProcessingSteps, batchSteps ProcessingSteps, forks ProcessingSteps) {
steps = make(ProcessingSteps, 0, len(r.stepRegistry))
batchSteps = make(ProcessingSteps, 0, len(r.batchRegistry))
forks = make(ProcessingSteps, 0, len(r.forkRegistry))
for _, step := range r.stepRegistry {
if step.Func != nil {
steps = append(steps, makeJsonProcessingStep(step.RegisteredStep, false, false))
}
}
for _, step := range r.batchRegistry {
if step.Func != nil {
batchSteps = append(batchSteps, makeJsonProcessingStep(step.RegisteredStep, true, false))
}
}
for _, step := range r.forkRegistry {
if step.Func != nil {
forks = append(forks, makeJsonProcessingStep(step.RegisteredStep, false, false))
}
}
sort.Sort(steps)
sort.Sort(batchSteps)
sort.Sort(forks)
return
}
func (r ProcessorRegistry) formatSection(buf *bytes.Buffer, steps ProcessingSteps, title string, started bool) bool {
if len(steps) > 0 {
if started {
buf.WriteString("\n")
}
buf.WriteString(title)
for _, step := range steps {
step.formatTo(buf)
}
started = true
}
return started
}
func (r ProcessorRegistry) FormatCapabilities(out io.Writer) error {
steps, batchSteps, forks := r.getSortedProcessingSteps()
started := false
var buf bytes.Buffer
started = r.formatSection(&buf, steps, "Processing steps:", started)
started = r.formatSection(&buf, batchSteps, "Batch processing steps:", started)
started = r.formatSection(&buf, forks, "Forks:", started)
buf.WriteString("\n\n")
_, err := buf.WriteTo(out)
return err
}
func (r ProcessorRegistry) FormatJsonCapabilities(out io.Writer) error {
steps, batchSteps, forks := r.getSortedProcessingSteps()
data, err := json.Marshal(map[string]interface{}{
"steps": steps,
"batch_steps": batchSteps,
"forks": forks,
})
if err == nil {
_, err = out.Write(data)
}
return err
}