-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connectors tutorial and improved readme
- Loading branch information
Mario Macias
committed
Apr 9, 2022
1 parent
1010673
commit 2067cd2
Showing
7 changed files
with
137 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Example pipeline for the node API | ||
|
||
The pipeline in the main file has two Start nodes that send the data to two destination Middle | ||
nodes (`odds` and `evens`). From there, the data follows their own branches until they | ||
are eventually joined in the `printer` Terminal node. | ||
|
||
Output: | ||
|
||
``` | ||
even number: 2 | ||
odd number: 847 | ||
odd number: 59 | ||
odd number: 81 | ||
odd number: 81 | ||
even number: 0 | ||
odd number: 3 | ||
odd number: 1 | ||
odd number: 887 | ||
even number: 4 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/mariomac/pipes/pkg/graph" | ||
"github.com/mariomac/pipes/pkg/graph/stage" | ||
"github.com/mariomac/pipes/pkg/node" | ||
) | ||
|
||
type GeneratorConfig struct { | ||
stage.Instance | ||
Repeat int | ||
Seed int64 | ||
LowerBound int | ||
UpperBound int | ||
} | ||
|
||
type PrinterConfig stage.Instance | ||
|
||
type Config struct { | ||
graph.Connector | ||
Generator GeneratorConfig | ||
Printer PrinterConfig | ||
} | ||
|
||
func Generator(cfg GeneratorConfig) node.StartFunc[int] { | ||
return func(out chan<- int) { | ||
rand.Seed(cfg.Seed) | ||
for n := 0; n < cfg.Repeat; n++ { | ||
out <- cfg.LowerBound + rand.Intn(cfg.UpperBound-cfg.LowerBound) | ||
} | ||
} | ||
} | ||
|
||
func Printer(_ PrinterConfig) node.TerminalFunc[string] { | ||
return func(in <-chan string) { | ||
for i := range in { | ||
fmt.Println("received: ", i) | ||
} | ||
} | ||
} | ||
|
||
// IntStringCodec just converts ints to string. Since the Generator | ||
// creates integers and the printer only accepts strings, we must | ||
// create and register a codec that will be automatically wired when | ||
// needed | ||
func IntStringCodec(in <-chan int, out chan<- string) { | ||
for i := range in { | ||
out <- strconv.Itoa(i) | ||
} | ||
} | ||
|
||
func main() { | ||
gb := graph.NewBuilder() | ||
graph.RegisterCodec(gb, IntStringCodec) | ||
graph.RegisterStart(gb, Generator) | ||
graph.RegisterTerminal(gb, Printer) | ||
|
||
grp, err := gb.Build(Config{ | ||
Generator: GeneratorConfig{ | ||
Instance: "generator", | ||
LowerBound: -10, | ||
UpperBound: 10, | ||
Seed: time.Now().UnixNano(), | ||
Repeat: 5, | ||
}, | ||
Printer: "printer", | ||
Connector: graph.Connector{ | ||
"generator": []string{"printer"}, | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
grp.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
# Tutorial | ||
|
||
TEMPTATIVE LIST: | ||
01 - Low-Level API: basic nodes | ||
02 - High-Level API: basic nodes | ||
03 - High-Level API: connectors | ||
|
||
1- basic lowlevel API | ||
2- equivalent to 1 in highlevel API | ||
3- connectors | ||
4- buffered channels | ||
5- using HCL as recommended config? | ||
6- default connections/nodes without IDs? (still to be implemented) | ||
TO DO: | ||
|
||
04- buffered channels | ||
05- using HCL as recommended config? | ||
06- default connections/nodes without IDs? (still to be implemented) |