tools/flow: replace package doc example with real example
Ultimately done to fix the fact the original example had the arguments
in the wrong order.
Change-Id: I4d7f1ae279090bd0bc391ec73dd732b78b419f27
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8421
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/tools/flow/example_basic_test.go b/tools/flow/example_basic_test.go
new file mode 100644
index 0000000..b0eeb6c
--- /dev/null
+++ b/tools/flow/example_basic_test.go
@@ -0,0 +1,57 @@
+package flow_test
+
+import (
+ "context"
+ "fmt"
+ "log"
+
+ "cuelang.org/go/cue"
+ "cuelang.org/go/tools/flow"
+)
+
+func Example() {
+ var r cue.Runtime
+ inst, err := r.Compile("example.cue", `
+ a: {
+ input: "world"
+ output: string
+ }
+ b: {
+ input: a.output
+ output: string
+ }
+ `)
+ if err != nil {
+ log.Fatal(err)
+ }
+ controller := flow.New(nil, inst, ioTaskFunc)
+ if err := controller.Run(context.Background()); err != nil {
+ log.Fatal(err)
+ }
+ // Output:
+ // setting a.output to "hello world"
+ // setting b.output to "hello hello world"
+}
+
+func ioTaskFunc(v cue.Value) (flow.Runner, error) {
+ inputPath := cue.ParsePath("input")
+
+ input := v.LookupPath(inputPath)
+ if !input.Exists() {
+ return nil, nil
+ }
+
+ return flow.RunnerFunc(func(t *flow.Task) error {
+ inputVal, err := t.Value().LookupPath(inputPath).String()
+ if err != nil {
+ return fmt.Errorf("input not of type string")
+ }
+
+ outputVal := fmt.Sprintf("hello %s", inputVal)
+ fmt.Printf("setting %s.output to %q\n", t.Path(), outputVal)
+
+ return t.Fill(map[string]string{
+ "output": outputVal,
+ })
+ }), nil
+}
diff --git a/tools/flow/flow.go b/tools/flow/flow.go
index 4d39949..3dee619 100644
--- a/tools/flow/flow.go
+++ b/tools/flow/flow.go
@@ -22,18 +22,6 @@
// Tasks may depend on other tasks. Cyclic dependencies are thereby not allowed.
// A Task A depends on another Task B if A, directly or indirectly, has a
// reference to any field of Task B, including its root.
-//
-// Example:
-// var inst cue.Instance
-//
-// // taskFunc takes a Value v and returns a Runner if v is a Task.
-// w := flow.New(inst, taskFunc, nil)
-//
-// err := w.Run(context.Background())
-// if err != nil {
-// ...
-// }
-//
package flow
// TODO: Add hooks. This would allow UIs, for instance, to report on progress.