-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
170 lines (139 loc) · 3.62 KB
/
Copy pathexample_test.go
File metadata and controls
170 lines (139 loc) · 3.62 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
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
package gloo_test
import (
"context"
"fmt"
"os"
"strings"
"github.com/spf13/afero"
gloo "github.com/gloo-foo/framework"
"github.com/gloo-foo/framework/patterns"
)
func ExampleRun() {
src := gloo.SliceSource([]string{"hello", "world"})
upper := patterns.Map(func(line string) (string, error) {
return strings.ToUpper(line), nil
})
_, _ = gloo.Run(src, gloo.WriteTo(os.Stdout), upper)
// Output:
// HELLO
// WORLD
}
// ExamplePump wires a composed command to stdin/stdout as a Unix filter —
// the bridge from a gloo pipeline to a func main. Nothing is shelled out.
func ExamplePump() {
// grep error | head -2, reading "stdin" and writing "stdout".
cmd := gloo.Compose(
patterns.Filter(func(line string) (bool, error) {
return strings.Contains(line, "error"), nil
}),
).To(patterns.Head[string](2))
stdin := strings.NewReader("error: a\ninfo: b\nerror: c\nerror: d\n")
_, _ = gloo.Pump(context.Background(), cmd, stdin, os.Stdout)
// Output:
// error: a
// error: c
}
func ExampleChain() {
src := gloo.SliceSource([]string{"apple red", "banana yellow", "cherry red"})
grep := patterns.Filter(func(line string) (bool, error) {
return strings.Contains(line, "red"), nil
})
upper := patterns.Map(func(line string) (string, error) {
return strings.ToUpper(line), nil
})
result, _ := gloo.Chain(src).
To(grep).
To(upper).
Collect()
for _, item := range result.([]string) {
fmt.Println(item)
}
// Output:
// APPLE RED
// CHERRY RED
}
func ExampleChain_forEach() {
src := gloo.SliceSource([]string{"one", "two", "three"})
_ = gloo.Chain(src).ForEach(func(line string) error {
fmt.Println(line)
return nil
})
// Output:
// one
// two
// three
}
func ExampleChain_sink() {
src := gloo.SliceSource([]string{"hello", "world"})
upper := patterns.Map(func(line string) (string, error) {
return strings.ToUpper(line), nil
})
count, _ := gloo.Chain(src).To(upper).Sink(gloo.WriteTo(os.Stdout))
fmt.Printf("%d lines written\n", count)
// Output:
// HELLO
// WORLD
// 2 lines written
}
func ExampleCompose() {
src := gloo.SliceSource([]string{"hello", "world"})
addBang := patterns.Map(func(line string) (string, error) {
return line + "!", nil
})
upper := patterns.Map(func(line string) (string, error) {
return strings.ToUpper(line), nil
})
composed := gloo.Compose(addBang).To(upper)
result, _ := gloo.Chain(src).To(composed).Collect()
for _, item := range result.([]string) {
fmt.Println(item)
}
// Output:
// HELLO!
// WORLD!
}
func ExamplePipe() {
ctx := context.Background()
src := gloo.SliceSource([]string{"apple red", "banana yellow", "cherry red"})
grep := patterns.Filter(func(line string) (bool, error) {
return strings.Contains(line, "red"), nil
})
upper := patterns.Map(func(line string) (string, error) {
return strings.ToUpper(line), nil
})
composed := gloo.Pipe(grep, upper)
stream := gloo.From(ctx, src, composed)
results, _ := stream.Collect()
for _, r := range results {
fmt.Println(r)
}
// Output:
// APPLE RED
// CHERRY RED
}
func ExampleFileSource() {
fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "test.txt", []byte("line one\nline two\nline three"), 0o644)
ctx := context.Background()
src := gloo.FileSource(fs, []gloo.File{"test.txt"})
results, _ := src.Stream(ctx).Collect()
for _, r := range results {
fmt.Println(r)
}
// Output:
// line one
// line two
// line three
}
func ExampleSliceSource() {
ctx := context.Background()
src := gloo.SliceSource([]string{"alpha", "bravo", "charlie"})
results, _ := src.Stream(ctx).Collect()
for _, r := range results {
fmt.Println(r)
}
// Output:
// alpha
// bravo
// charlie
}