-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_call_test.go
82 lines (76 loc) · 2.12 KB
/
engine_call_test.go
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
package nu_test
import (
"context"
"fmt"
"github.com/ainvaltin/nu-plugin"
"github.com/ainvaltin/nu-plugin/syntaxshape"
)
// example of a command which sends list stream as a input to closure
func ExampleInputListStream() {
_ = &nu.Command{
Signature: nu.PluginSignature{
Name: "demo",
RequiredPositional: nu.PositionalArgs{
nu.PositionalArg{
Name: "closure",
Desc: "Closure to be evaluated",
Shape: syntaxshape.Closure(),
},
},
},
Examples: nu.Examples{
{Description: `Closure which adds +1 to each item in input stream and returns stream`, Example: `demo { $in | each {|n| $n + 1} }`},
},
OnRun: func(ctx context.Context, call *nu.ExecCommand) error {
// EvalClosure will block until the closure returns something so generate the
// input stream in goroutine
closureIn := make(chan nu.Value)
go func() {
defer close(closureIn)
for v := range 10 {
closureIn <- nu.Value{Value: v}
}
}()
closureOut, err := call.EvalClosure(ctx, call.Positional[0], nu.InputListStream(closureIn))
if err != nil {
return fmt.Errorf("evaluating closure: %w", err)
}
switch data := closureOut.(type) {
case <-chan nu.Value:
out, err := call.ReturnListStream(ctx)
if err != nil {
return fmt.Errorf("opening output stream: %w", err)
}
for v := range data {
out <- v
}
close(out)
default:
return fmt.Errorf("unexpected closure output type %T", data)
}
return nil
},
}
}
func ExampleDeclaration_Call() {
// command's OnRun handler
_ = func(ctx context.Context, call *nu.ExecCommand) error {
// find the builtin "into int" command (Conversions)
dec, err := call.FindDeclaration(ctx, "into int")
if err != nil {
return err
}
// following call is the same as executing
// 'FF' | into int --radix 16
response, err := dec.Call(ctx, nu.InputValue(nu.Value{Value: "FF"}), nu.NamedParams{"radix": nu.Value{Value: 16}})
if err != nil {
return err
}
switch data := response.(type) {
case nu.Value:
return call.ReturnValue(ctx, data)
default:
return fmt.Errorf("unsupported return type %#v", response)
}
}
}