-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpipeline_tree_spreader.go
194 lines (166 loc) · 3.79 KB
/
pipeline_tree_spreader.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//go:build !tinywasm
package gtree
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"sync"
toml "github.com/pelletier/go-toml/v2"
"gopkg.in/yaml.v3"
)
func newSpreaderPipeline(encode encode) spreaderPipeline {
switch encode {
case encodeJSON:
return newJSONSpreaderPipeline()
case encodeYAML:
return newYAMLSpreaderPipeline()
case encodeTOML:
return newTOMLSpreaderPipeline()
default:
return &defaultSpreaderPipeline{
defaultSpreaderSimple: &defaultSpreaderSimple{},
}
}
}
type defaultSpreaderPipeline struct {
*defaultSpreaderSimple
sync.Mutex
}
const workerSpreadNum = 10
func (ds *defaultSpreaderPipeline) spread(ctx context.Context, w io.Writer, roots <-chan *Node) <-chan error {
errc := make(chan error, 1)
go func() {
defer func() {
close(errc)
}()
ds.w = w
wg := &sync.WaitGroup{}
for i := 0; i < workerSpreadNum; i++ {
wg.Add(1)
go ds.worker(ctx, wg, roots, errc)
}
wg.Wait()
}()
return errc
}
func (ds *defaultSpreaderPipeline) worker(ctx context.Context, wg *sync.WaitGroup, roots <-chan *Node, _ chan<- error) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case root, ok := <-roots:
if !ok {
return
}
ds.Lock()
ds.spreadBranch(root)
ds.Unlock()
}
}
}
type formattedSpreaderPipeline[T sitter] struct {
formattedRoot func(string) T
encode func(io.Writer) func(any) error
}
func newJSONSpreaderPipeline() *formattedSpreaderPipeline[*jsonNode] {
return &formattedSpreaderPipeline[*jsonNode]{
formattedRoot: func(name string) *jsonNode {
return &jsonNode{Name: name}
},
encode: func(w io.Writer) func(any) error {
return json.NewEncoder(w).Encode
},
}
}
func newYAMLSpreaderPipeline() *formattedSpreaderPipeline[*yamlNode] {
return &formattedSpreaderPipeline[*yamlNode]{
formattedRoot: func(name string) *yamlNode {
return &yamlNode{Name: name}
},
encode: func(w io.Writer) func(any) error {
return yaml.NewEncoder(w).Encode
},
}
}
func newTOMLSpreaderPipeline() *formattedSpreaderPipeline[*tomlNode] {
return &formattedSpreaderPipeline[*tomlNode]{
formattedRoot: func(name string) *tomlNode {
return &tomlNode{Name: name}
},
encode: func(w io.Writer) func(any) error {
return toml.NewEncoder(w).Encode
},
}
}
func (f *formattedSpreaderPipeline[T]) spread(ctx context.Context, w io.Writer, roots <-chan *Node) <-chan error {
errc := make(chan error, 1)
go func() {
defer close(errc)
encode := f.encode(w)
BREAK:
for {
select {
case <-ctx.Done():
return
case root, ok := <-roots:
if !ok {
break BREAK
}
if err := encode(toFormattedNode(root, f.formattedRoot(root.name))); err != nil {
errc <- err
}
}
}
}()
return errc
}
func newColorizeSpreaderPipeline(fileExtensions []string) spreaderPipeline {
return &colorizeSpreaderPipeline{
colorizeSpreaderSimple: newColorizeSpreaderSimple(fileExtensions).(*colorizeSpreaderSimple),
}
}
type colorizeSpreaderPipeline struct {
*colorizeSpreaderSimple
}
func (cs *colorizeSpreaderPipeline) spread(ctx context.Context, w io.Writer, roots <-chan *Node) <-chan error {
errc := make(chan error, 1)
go func() {
defer close(errc)
bw := bufio.NewWriter(w)
BREAK:
for {
select {
case <-ctx.Done():
return
case root, ok := <-roots:
if !ok {
break BREAK
}
cs.fileCounter.reset()
cs.dirCounter.reset()
if _, err := bw.WriteString(
fmt.Sprintf(
"%s\n%s\n",
cs.spreadBranch(root),
cs.summary()),
); err != nil {
errc <- err
return
}
}
if err := bw.Flush(); err != nil {
errc <- err
return
}
}
}()
return errc
}
var (
_ spreaderPipeline = (*defaultSpreaderPipeline)(nil)
_ spreaderPipeline = (*formattedSpreaderPipeline[sitter])(nil)
_ spreaderPipeline = (*colorizeSpreaderPipeline)(nil)
)