Skip to content

Commit

Permalink
feat: support begin and final hook
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Jan 11, 2025
1 parent e3a8bae commit 979f1dd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
5 changes: 1 addition & 4 deletions examples/helloworld.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
- kind: print
filename: /dev/stdout
ports:
init:
begin:
- name: hello_world
port: in
env:
PORT:
- data: '{{ .PORT }}'

- kind: step
name: good_bye
Expand Down
14 changes: 8 additions & 6 deletions pkg/node/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ import (
// Commonly used port names.
const (
PortInit = "init"
PortBegin = "begin"
PortTerm = "term"
PortFinal = "final"
PortIO = "io"
PortIn = "in"
PortOut = "out"
PortError = "error"
)

var portExp = regexp.MustCompile(`(\w+)\[(\d+)\]`)
var subscript = regexp.MustCompile(`(\w+)\[(\d+)\]`)

// PortWithIndex formats the port name as "name[index]".
func PortWithIndex(name string, index int) string {
return fmt.Sprintf("%s[%d]", name, index)
}

// NameOfPort extracts the base name from a port name formatted as "name[index]".
func NameOfPort(name string) string {
if groups := portExp.FindStringSubmatch(name); groups != nil {
func NameOfPort(key string) string {
if groups := subscript.FindStringSubmatch(key); groups != nil {
return groups[1]
}
return name
return key
}

// IndexOfPort extracts the index from a port name formatted as "name[index]".
func IndexOfPort(name string) (int, bool) {
if groups := portExp.FindStringSubmatch(name); len(groups) == 3 {
func IndexOfPort(key string) (int, bool) {
if groups := subscript.FindStringSubmatch(key); len(groups) == 3 {
if index, err := strconv.Atoi(groups[2]); err == nil {
return index, true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/symbol/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package symbol
import (
"context"
"errors"
"github.com/iancoleman/strcase"
"reflect"

"github.com/iancoleman/strcase"
"github.com/siyul-park/uniflow/pkg/resource"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/spec"
Expand Down
10 changes: 8 additions & 2 deletions pkg/symbol/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (t *Table) Keys() []uuid.UUID {
t.mu.RLock()
defer t.mu.RUnlock()

var ids []uuid.UUID
ids := make([]uuid.UUID, 0, len(t.symbols))
for id := range t.symbols {
ids = append(ids, id)
}
Expand Down Expand Up @@ -272,10 +272,13 @@ func (t *Table) load(sb *Symbol) error {
linked := t.linked(sb)
for _, sb := range linked {
if t.active(sb) {
if err := t.call(sb, node.PortInit); err != nil {
return err
}
if err := t.loadHooks.Load(sb); err != nil {
return err
}
if err := t.call(sb, node.PortInit); err != nil {
if err := t.call(sb, node.PortBegin); err != nil {
return err
}
}
Expand All @@ -294,6 +297,9 @@ func (t *Table) unload(sb *Symbol) error {
if err := t.unloadHooks.Unload(sb); err != nil {
return err
}
if err := t.call(sb, node.PortFinal); err != nil {
return err
}
}
}
return nil
Expand Down

0 comments on commit 979f1dd

Please sign in to comment.