-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
343 lines (295 loc) · 7.98 KB
/
example_test.go
File metadata and controls
343 lines (295 loc) · 7.98 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package pocket_test
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/agentstation/pocket"
)
// Using constants from lifecycle_test.go and options_test.go
// ExampleNode demonstrates using the Prep/Exec/Post lifecycle.
func ExampleNode() {
// Create a node with lifecycle steps
uppercase := pocket.NewNode[any, any]("uppercase",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
// Validate input is a string
text, ok := input.(string)
if !ok {
return nil, fmt.Errorf("expected string, got %T", input)
}
return text, nil
},
Exec: func(ctx context.Context, text any) (any, error) {
// Transform to uppercase
return strings.ToUpper(text.(string)), nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, text, result any) (any, string, error) {
// Return result and routing
return result, doneRoute, nil
},
},
)
store := pocket.NewStore()
graph := pocket.NewGraph(uppercase, store)
result, err := graph.Run(context.Background(), "hello world")
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
// Output: HELLO WORLD
}
// ExampleBuilder demonstrates the fluent builder API.
func ExampleBuilder() {
store := pocket.NewStore()
// Define nodes with lifecycle
validate := pocket.NewNode[any, any]("validate",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
email, ok := input.(string)
if !ok {
return nil, fmt.Errorf("expected string")
}
return email, nil
},
Exec: func(ctx context.Context, email any) (any, error) {
if !strings.Contains(email.(string), "@") {
return nil, fmt.Errorf("invalid email")
}
return email, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, defaultRoute, nil
},
},
)
normalize := pocket.NewNode[any, any]("normalize",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
email := input.(string)
return strings.ToLower(strings.TrimSpace(email)), nil
},
},
)
// Build the graph
graph, err := pocket.NewBuilder(store).
Add(validate).
Add(normalize).
Connect("validate", "default", "normalize").
Start("validate").
Build()
if err != nil {
log.Fatal(err)
}
result, err := graph.Run(context.Background(), " USER@EXAMPLE.COM ")
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
// Output: user@example.com
}
// ExampleNode_routing demonstrates conditional routing between nodes.
func ExampleNode_routing() {
store := pocket.NewStore()
// Router node that checks input
router := pocket.NewNode[any, any]("router",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
value := result.(int)
if value > 100 {
return result, "large", nil
}
return result, "small", nil
},
},
)
// Handler nodes
largeHandler := pocket.NewNode[any, any]("large",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return fmt.Sprintf("Large number: %v", input), nil
},
},
)
smallHandler := pocket.NewNode[any, any]("small",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return fmt.Sprintf("Small number: %v", input), nil
},
},
)
// Connect nodes
router.Connect("large", largeHandler)
router.Connect("small", smallHandler)
// Run with different inputs
graph := pocket.NewGraph(router, store)
result1, _ := graph.Run(context.Background(), 50)
result2, _ := graph.Run(context.Background(), 150)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// Small number: 50
// Large number: 150
}
// ExampleFanOut demonstrates parallel processing of items.
func ExampleFanOut() {
// Create a processor that simulates work
processor := pocket.NewNode[any, any]("process",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
num := input.(int)
return num * num, nil
},
},
)
store := pocket.NewStore()
items := []int{1, 2, 3, 4, 5}
// Process items concurrently
results, err := pocket.FanOut(context.Background(), processor, store, items)
if err != nil {
log.Fatal(err)
}
// Results maintain order
for i, result := range results {
fmt.Printf("%d -> %v\n", items[i], result)
}
// Output:
// 1 -> 1
// 2 -> 4
// 3 -> 9
// 4 -> 16
// 5 -> 25
}
// ExamplePipeline demonstrates sequential processing.
func ExamplePipeline() {
store := pocket.NewStore()
// Create a pipeline of transformations
double := pocket.NewNode[any, any]("double",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input.(int) * 2, nil
},
},
)
addTen := pocket.NewNode[any, any]("addTen",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input.(int) + 10, nil
},
},
)
toString := pocket.NewNode[any, any]("toString",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return fmt.Sprintf("Result: %d", input.(int)), nil
},
},
)
nodes := []pocket.Node{double, addTen, toString}
result, err := pocket.Pipeline(context.Background(), nodes, store, 5)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
// Output: Result: 20
}
// ExampleTypedStore demonstrates type-safe storage.
func ExampleTypedStore() {
type User struct {
ID string
Name string
}
// Create a typed store
store := pocket.NewStore()
userStore := pocket.NewTypedStore[User](store)
ctx := context.Background()
// Store a user
user := User{ID: "123", Name: "Alice"}
err := userStore.Set(ctx, "user:123", user)
if err != nil {
log.Fatal(err)
}
// Retrieve with type safety
retrieved, exists, err := userStore.Get(ctx, "user:123")
if err != nil {
log.Fatal(err)
}
if exists {
fmt.Printf("Found user: %+v\n", retrieved)
}
// Output: Found user: {ID:123 Name:Alice}
}
// ExampleWithRetry demonstrates retry configuration.
func ExampleWithRetry() {
attempts := 0
// Create a node that fails twice before succeeding
flaky := pocket.NewNode[any, any]("flaky",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
attempts++
if attempts < 3 {
return nil, fmt.Errorf("temporary failure %d", attempts)
}
return "success", nil
},
},
pocket.WithRetry(2, 10*time.Millisecond), // Retry up to 2 times
)
store := pocket.NewStore()
graph := pocket.NewGraph(flaky, store)
result, err := graph.Run(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Result after %d attempts: %v\n", attempts, result)
// Output: Result after 3 attempts: success
}
// Example_lifecycle demonstrates the full Prep/Exec/Post lifecycle.
func Example_lifecycle() {
// Create a node that uses all three steps
processor := pocket.NewNode[any, any]("processor",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
// Prepare: validate and transform input
data := input.(map[string]int)
if len(data) == 0 {
return nil, fmt.Errorf("empty data")
}
return data, nil
},
Exec: func(ctx context.Context, data any) (any, error) {
// Execute: calculate sum
m := data.(map[string]int)
sum := 0
for _, v := range m {
sum += v
}
return sum, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, data, sum any) (any, string, error) {
// Post: decide routing based on result
total := sum.(int)
if total > 100 {
return fmt.Sprintf("High total: %d", total), "high", nil
}
return fmt.Sprintf("Low total: %d", total), "low", nil
},
},
)
store := pocket.NewStore()
graph := pocket.NewGraph(processor, store)
result, err := graph.Run(context.Background(), map[string]int{
"a": 10,
"b": 20,
"c": 30,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
// Output: Low total: 60
}