/
example_input_trigger_test.go
executable file
·56 lines (42 loc) · 1.18 KB
/
example_input_trigger_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
// +build examples
package thingiverseio_test
import (
"log"
"github.com/ThingiverseIO/thingiverseio"
)
const descriptor = `
function SayHello(Greeting string) (Answer string)
tag example_tag
`
// SayHelloInput represents the input parameters for the SayHello function.
type SayHelloInput struct {
Greeting string
}
// SayHelloOutput represents the output parameters for the SayHello function.
type SayHelloOutput struct {
Answer string
}
// ExampleInputTrigger demonstrates a simple input using the TRIGGER mechanism.
func Example_inputTrigger() {
// Create and run the input.
i, err := thingiverseio.NewInput(descriptor)
if err != nil {
log.Fatal(err)
}
i.Run()
// Start listen to the SyHello function and get a channel to receive results.
i.StartListen("SayHello")
c, _ := i.ListenResults().AsChan()
// Wait until an output connects.
i.ConnectedFuture().WaitUntilComplete()
// Create the request parameter.
p := SayHelloInput{"Greetings, this is a CALL example"}
// Do the trigger.
i.Trigger("SayHello", p)
// Receive the result.
result := <-c
// Decode and print the result.
var out SayHelloOutput
result.Decode(&out)
log.Println("Received an answer:", out.Answer)
}