Skip to content

Commit f3dc65e

Browse files
author
Ryan Field
committed
Operators should return a string
1 parent e29c090 commit f3dc65e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

example.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package goflow
22

33
import (
44
"errors"
5-
"log"
5+
"fmt"
66
"math/rand"
7+
"net/http"
78
)
89

910
// Crunch some numbers
@@ -14,6 +15,10 @@ func complexAnalyticsJob() *Job {
1415
Active: false,
1516
}
1617

18+
j.AddTask(&Task{
19+
Name: "get-google",
20+
Operator: Get{Client: &http.Client{}, URL: "https://google.com"},
21+
})
1722
j.AddTask(&Task{
1823
Name: "sleep-one",
1924
Operator: Command{Cmd: "sleep", Args: []string{"1"}},
@@ -57,6 +62,7 @@ func complexAnalyticsJob() *Job {
5762
TriggerRule: "allDone",
5863
})
5964

65+
j.SetDownstream("get-google", "add-one-one")
6066
j.SetDownstream("sleep-one", "add-one-one")
6167
j.SetDownstream("add-one-one", "sleep-two")
6268
j.SetDownstream("sleep-two", "add-two-four")
@@ -77,14 +83,14 @@ type randomFailure struct{ n int }
7783
var r = rand.New(rand.NewSource(1))
7884

7985
// Run implements failures at random intervals.
80-
func (o randomFailure) Run(e *Execution) (any, error) {
86+
func (o randomFailure) Run(e *Execution) (string, error) {
8187
x := r.Intn(o.n)
8288

8389
if x == o.n-1 {
84-
return nil, errors.New("unlucky")
90+
return "randomly failed", errors.New("unlucky")
8591
}
8692

87-
return x, nil
93+
return fmt.Sprintf("the result is %v", x), nil
8894
}
8995

9096
func randomFailureJob() *Job {
@@ -100,7 +106,7 @@ type summation struct {
100106
}
101107

102108
// Run performs summation.
103-
func (o summation) Run(e *Execution) (any, error) {
109+
func (o summation) Run(e *Execution) (string, error) {
104110

105111
result := o.Value
106112

@@ -112,9 +118,7 @@ func (o summation) Run(e *Execution) (any, error) {
112118
}
113119
}
114120

115-
log.Printf("sum value=%v", result)
116-
117-
return result, nil
121+
return fmt.Sprintf("sum value=%v", result), nil
118122
}
119123

120124
func summationJob() *Job {

operator.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// An Operator implements a Run() method. When a job executes a task that
1111
// uses the operator, the Run() method is called.
1212
type Operator interface {
13-
Run(*Execution) (any, error)
13+
Run(*Execution) (string, error)
1414
}
1515

1616
// Command executes a shell command.
@@ -21,7 +21,7 @@ type Command struct {
2121

2222
// Run passes the command and arguments to exec.Command and captures the
2323
// output.
24-
func (o Command) Run(e *Execution) (any, error) {
24+
func (o Command) Run(e *Execution) (string, error) {
2525
out, err := exec.Command(o.Cmd, o.Args...).Output()
2626
return string(out), err
2727
}
@@ -34,15 +34,15 @@ type Get struct {
3434

3535
// Run sends the request and returns an error if the status code is
3636
// outside the 2xx range.
37-
func (o Get) Run(e *Execution) (any, error) {
37+
func (o Get) Run(e *Execution) (string, error) {
3838
res, err := o.Client.Get(o.URL)
3939
if err != nil {
40-
return nil, err
40+
return "", err
4141
}
4242
defer res.Body.Close()
4343

4444
if res.StatusCode < 200 || res.StatusCode > 299 {
45-
return nil, fmt.Errorf("Received status code %v", res.StatusCode)
45+
return "", fmt.Errorf("Received status code %v", res.StatusCode)
4646
}
4747

4848
content, err := io.ReadAll(res.Body)
@@ -58,15 +58,15 @@ type Post struct {
5858

5959
// Run sends the request and returns an error if the status code is
6060
// outside the 2xx range.
61-
func (o Post) Run(e *Execution) (any, error) {
61+
func (o Post) Run(e *Execution) (string, error) {
6262
res, err := o.Client.Post(o.URL, "application/json", o.Body)
6363
if err != nil {
64-
return nil, err
64+
return "", err
6565
}
6666
defer res.Body.Close()
6767

6868
if res.StatusCode < 200 || res.StatusCode > 299 {
69-
return nil, fmt.Errorf("Received status code %v", res.StatusCode)
69+
return "", fmt.Errorf("Received status code %v", res.StatusCode)
7070
}
7171

7272
content, err := io.ReadAll(res.Body)

0 commit comments

Comments
 (0)