...

Source file src/cuelang.org/go/tools/flow/example_basic_test.go

Documentation: cuelang.org/go/tools/flow

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

View as plain text