github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/tools/flow/example_basic_test.go (about) 1 package flow_test 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "github.com/joomcode/cue/cue" 9 "github.com/joomcode/cue/tools/flow" 10 ) 11 12 func Example() { 13 var r cue.Runtime 14 inst, err := r.Compile("example.cue", ` 15 a: { 16 input: "world" 17 output: string 18 } 19 b: { 20 input: a.output 21 output: string 22 } 23 `) 24 if err != nil { 25 log.Fatal(err) 26 } 27 controller := flow.New(nil, inst, ioTaskFunc) 28 if err := controller.Run(context.Background()); err != nil { 29 log.Fatal(err) 30 } 31 // Output: 32 // setting a.output to "hello world" 33 // setting b.output to "hello hello world" 34 } 35 36 func ioTaskFunc(v cue.Value) (flow.Runner, error) { 37 inputPath := cue.ParsePath("input") 38 39 input := v.LookupPath(inputPath) 40 if !input.Exists() { 41 return nil, nil 42 } 43 44 return flow.RunnerFunc(func(t *flow.Task) error { 45 inputVal, err := t.Value().LookupPath(inputPath).String() 46 if err != nil { 47 return fmt.Errorf("input not of type string") 48 } 49 50 outputVal := fmt.Sprintf("hello %s", inputVal) 51 fmt.Printf("setting %s.output to %q\n", t.Path(), outputVal) 52 53 return t.Fill(map[string]string{ 54 "output": outputVal, 55 }) 56 }), nil 57 }