trustmaster / goflow

Flow-based and dataflow programming library for Go (golang)
MIT License
1.6k stars 125 forks source link

Run-time component instantiation #10

Closed trustmaster closed 10 years ago

trustmaster commented 10 years ago

In order to implement #7 with loading graph at run-time, it is required that new processes are created at run-time without having to write and compile Go code like this:

sorter := foo.NewSorter()
printer := foo.NewPrinter()
net.Add(sorter, "sorter")
net.Add(printer, "printer")

What we need is something like a component registry and a run-time process factory, that would spawn a new process given just its string component name, e.g.:

net.Add("foo.Sorter", "sorter")
net.Add("foo.Printer", "printer")
trustmaster commented 10 years ago

New processes are actually instantiated this way:

net.AddNew("ComponentName", "processName", initialPacket)

Where initialPacket can be nil or any type accepted by constructor and ComponentName is a registered process constructor:

func newComponentName(iip interface{}) interface{} {
    c  := new(ComponentName)
    c.Foo = iip.(PacketType).Foo
    return c
}

func init() {
    flow.Register("ComponentName", newComponentName)
}