-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (56 loc) · 1.84 KB
/
main.go
File metadata and controls
66 lines (56 loc) · 1.84 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
// Package main demonstrates the simplest possible gographgo graph.
//
// This example builds a linear pipeline:
//
// Start → greet → shout → End
//
// State is a typed Go struct. Each node reads the current state and returns
// channel writes that are merged back into the state before the next node runs.
package main
import (
"context"
"fmt"
"log"
"strings"
"github.com/SkinnyPeteTheGiraffe/gographgo/pkg/graph"
)
// State holds all mutable data that flows through the graph.
type State struct {
Message string
Steps int
}
func main() {
// 1. Create a builder for our State type.
builder := graph.NewStateGraph[State]()
// 2. Define nodes. Each node receives the current state and returns
// writes that update specific fields.
builder.AddNode("greet", func(ctx context.Context, s State) (graph.NodeResult, error) {
return graph.NodeWrites(map[string]graph.Dynamic{
"Message": graph.Dyn("Hello, gographgo!"),
"Steps": graph.Dyn(s.Steps + 1),
}), nil
})
builder.AddNode("shout", func(ctx context.Context, s State) (graph.NodeResult, error) {
return graph.NodeWrites(map[string]graph.Dynamic{
"Message": graph.Dyn(strings.ToUpper(s.Message)),
"Steps": graph.Dyn(s.Steps + 1),
}), nil
})
// 3. Wire up edges: Start → greet → shout → End.
builder.AddEdge(graph.Start, "greet")
builder.AddEdge("greet", "shout")
builder.AddEdge("shout", graph.End)
// 4. Compile validates the graph topology and returns an executable graph.
compiled, err := builder.Compile()
if err != nil {
log.Fatalf("compile: %v", err)
}
// 5. Invoke runs the graph to completion and returns the final state.
result, err := compiled.Invoke(context.Background(), State{})
if err != nil {
log.Fatalf("invoke: %v", err)
}
final := result.Value.(State)
fmt.Printf("Message: %s\n", final.Message)
fmt.Printf("Steps: %d\n", final.Steps)
}