Skip to content

Commit

Permalink
feat: support default spec
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 18, 2024
1 parent dec83fc commit ec1b260
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 290 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ Try a basic HTTP request handler using [ping.yaml](./examples/ping.yaml):
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
4 changes: 0 additions & 4 deletions README_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ make build
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
25 changes: 21 additions & 4 deletions cmd/pkg/uniflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ func main() {
secretStore = secret.NewStore()
}

specs := map[string]spec.Spec{
network.KindListener: &spec.Unstructured{
Meta: spec.Meta{
Env: map[string][]spec.Value{
"PORT": {
{
Data: "{{ .PORT }}",
},
},
},
},
Fields: map[string]any{
"port": "{{ .PORT }}",
},
},
}

schemeBuilder := scheme.NewBuilder()
hookBuilder := hook.NewBuilder()

Expand All @@ -146,10 +163,10 @@ func main() {
nativeTable.Store(opUpdateSecrets, system.UpdateResource(secretStore))
nativeTable.Store(opDeleteSecrets, system.DeleteResource(secretStore))

schemeBuilder.Register(control.AddToScheme(langs, cel.Language))
schemeBuilder.Register(io.AddToScheme(io.NewOSFileSystem()))
schemeBuilder.Register(network.AddToScheme())
schemeBuilder.Register(system.AddToScheme(nativeTable))
schemeBuilder.Register(control.AddToScheme(langs, cel.Language, specs))
schemeBuilder.Register(io.AddToScheme(io.NewOSFileSystem(), specs))
schemeBuilder.Register(network.AddToScheme(specs))
schemeBuilder.Register(system.AddToScheme(nativeTable, specs))

hookBuilder.Register(control.AddToHook())
hookBuilder.Register(network.AddToHook())
Expand Down
4 changes: 0 additions & 4 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ Users can update node specifications by using a Command-Line Interface (CLI) or
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
error:
- name: catch
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
4 changes: 0 additions & 4 deletions docs/architecture_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
error:
- name: catch
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
4 changes: 0 additions & 4 deletions examples/httpproxy.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: proxy
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: proxy
name: proxy
Expand Down
4 changes: 0 additions & 4 deletions examples/ping.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
4 changes: 0 additions & 4 deletions examples/system.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
error:
- name: catch
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
4 changes: 0 additions & 4 deletions examples/wsproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
- kind: listener
name: listener
protocol: http
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
22 changes: 10 additions & 12 deletions ext/pkg/control/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// BlockNodeSpec defines the specification for creating a BlockNode.
type BlockNodeSpec struct {
spec.Meta `map:",inline"`
Specs []*spec.Unstructured `map:"specs"`
Specs []spec.Spec `map:"specs"`
}

// BlockNode systematically manages complex data processing flows and executes multiple sub-nodes sequentially.
Expand All @@ -32,25 +32,23 @@ var _ node.Node = (*BlockNode)(nil)

// NewBlockNodeCodec creates a new codec for BlockNodeSpec.
func NewBlockNodeCodec(s *scheme.Scheme) scheme.Codec {
return scheme.CodecWithType(func(spec *BlockNodeSpec) (node.Node, error) {
symbols := make([]*symbol.Symbol, 0, len(spec.Specs))
for _, sp := range spec.Specs {
decoded, err := s.Decode(sp)
if err != nil {
for _, n := range symbols {
n.Close()
}
return nil, err
return scheme.CodecWithType(func(sp *BlockNodeSpec) (node.Node, error) {
symbols := make([]*symbol.Symbol, 0, len(sp.Specs))
for _, sp := range sp.Specs {
sp, err := s.Build(sp)
for _, n := range symbols {
n.Close()
}
n, err := s.Compile(decoded)

n, err := s.Compile(sp)
if err != nil {
for _, n := range symbols {
n.Close()
}
return nil, err
}
symbols = append(symbols, &symbol.Symbol{
Spec: decoded,
Spec: sp,
Node: n,
})
}
Expand Down
4 changes: 2 additions & 2 deletions ext/pkg/control/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestBlockNodeCodec_Decode(t *testing.T) {
codec := NewBlockNodeCodec(s)

spec := &BlockNodeSpec{
Specs: []*spec.Unstructured{
{
Specs: []spec.Spec{
&spec.Unstructured{
Meta: spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: kind,
Expand Down
75 changes: 34 additions & 41 deletions ext/pkg/control/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/siyul-park/uniflow/ext/pkg/language"
"github.com/siyul-park/uniflow/pkg/hook"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/spec"
"github.com/siyul-park/uniflow/pkg/symbol"
)

Expand Down Expand Up @@ -33,54 +34,46 @@ func AddToHook() hook.Register {
}

// AddToScheme returns a function that adds node types and codecs to the provided spec.
func AddToScheme(module *language.Module, lang string) scheme.Register {
func AddToScheme(module *language.Module, lang string, specs ...map[string]spec.Spec) scheme.Register {
value := map[string]spec.Spec{}
for _, val := range specs {
for v, k := range val {
value[v] = k
}
}

return scheme.RegisterFunc(func(s *scheme.Scheme) error {
expr, err := module.Load(lang)
if err != nil {
return err
}

s.AddKnownType(KindBlock, &BlockNodeSpec{})
s.AddCodec(KindBlock, NewBlockNodeCodec(s))

s.AddKnownType(KindPipe, &PipeNodeSpec{})
s.AddCodec(KindPipe, NewPipeNodeCodec())

s.AddKnownType(KindFork, &ForkNodeSpec{})
s.AddCodec(KindFork, NewForkNodeCodec())

s.AddKnownType(KindIf, &IfNodeSpec{})
s.AddCodec(KindIf, NewIfNodeCodec(expr))

s.AddKnownType(KindLoop, &LoopNodeSpec{})
s.AddCodec(KindLoop, NewLoopNodeCodec())

s.AddKnownType(KindMerge, &MergeNodeSpec{})
s.AddCodec(KindMerge, NewMergeNodeCodec())

s.AddKnownType(KindNOP, &NOPNodeSpec{})
s.AddCodec(KindNOP, NewNOPNodeCodec())

s.AddKnownType(KindReduce, &ReduceNodeSpec{})
s.AddCodec(KindReduce, NewReduceNodeCodec(expr))

s.AddKnownType(KindRetry, &RetryNodeSpec{})
s.AddCodec(KindRetry, NewRetryNodeCodec())

s.AddKnownType(KindSession, &SessionNodeSpec{})
s.AddCodec(KindSession, NewSessionNodeCodec())

s.AddKnownType(KindSnippet, &SnippetNodeSpec{})
s.AddCodec(KindSnippet, NewSnippetNodeCodec(module))

s.AddKnownType(KindSplit, &SplitNodeSpec{})
s.AddCodec(KindSplit, NewSplitNodeCodec())

s.AddKnownType(KindSwitch, &SwitchNodeSpec{})
s.AddCodec(KindSwitch, NewSwitchNodeCodec(expr))
definitions := []struct {
kind string
codec scheme.Codec
spec spec.Spec
}{
{KindBlock, NewBlockNodeCodec(s), &BlockNodeSpec{}},
{KindPipe, NewPipeNodeCodec(), &PipeNodeSpec{}},
{KindFork, NewForkNodeCodec(), &ForkNodeSpec{}},
{KindIf, NewIfNodeCodec(expr), &IfNodeSpec{}},
{KindLoop, NewLoopNodeCodec(), &LoopNodeSpec{}},
{KindMerge, NewMergeNodeCodec(), &MergeNodeSpec{}},
{KindNOP, NewNOPNodeCodec(), &NOPNodeSpec{}},
{KindReduce, NewReduceNodeCodec(expr), &ReduceNodeSpec{}},
{KindRetry, NewRetryNodeCodec(), &RetryNodeSpec{}},
{KindSession, NewSessionNodeCodec(), &SessionNodeSpec{}},
{KindSnippet, NewSnippetNodeCodec(module), &SnippetNodeSpec{}},
{KindSplit, NewSplitNodeCodec(), &SplitNodeSpec{}},
{KindSwitch, NewSwitchNodeCodec(expr), &SwitchNodeSpec{}},
{KindWait, NewWaitNodeCodec(), &WaitNodeSpec{}},
}

s.AddKnownType(KindWait, &WaitNodeSpec{})
s.AddCodec(KindWait, NewWaitNodeCodec())
for _, def := range definitions {
s.AddKnownType(def.kind, def.spec)
s.AddKnownValue(def.kind, value[def.kind])
s.AddCodec(def.kind, def.codec)
}

return nil
})
Expand Down
36 changes: 27 additions & 9 deletions ext/pkg/io/builder.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
package io

import "github.com/siyul-park/uniflow/pkg/scheme"
import (
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/spec"
)

// AddToScheme returns a function that adds node types and codecs to the provided spec.
func AddToScheme(fs FileSystem) scheme.Register {
return scheme.RegisterFunc(func(s *scheme.Scheme) error {
s.AddKnownType(KindSQL, &SQLNodeSpec{})
s.AddCodec(KindSQL, NewSQLNodeCodec())
func AddToScheme(fs FileSystem, specs ...map[string]spec.Spec) scheme.Register {
value := map[string]spec.Spec{}
for _, val := range specs {
for v, k := range val {
value[v] = k
}
}

s.AddKnownType(KindPrint, &PrintNodeSpec{})
s.AddCodec(KindPrint, NewPrintNodeCodec(fs))
return scheme.RegisterFunc(func(s *scheme.Scheme) error {
definitions := []struct {
kind string
codec scheme.Codec
spec spec.Spec
}{
{KindSQL, NewSQLNodeCodec(), &SQLNodeSpec{}},
{KindPrint, NewPrintNodeCodec(fs), &PrintNodeSpec{}},
{KindScan, NewScanNodeCodec(fs), &ScanNodeSpec{}},
}

s.AddKnownType(KindScan, &ScanNodeSpec{})
s.AddCodec(KindScan, NewScanNodeCodec(fs))
for _, def := range definitions {
s.AddKnownType(def.kind, def.spec)
s.AddKnownValue(def.kind, value[def.kind])
s.AddCodec(def.kind, def.codec)
}

return nil
})

}
Loading

0 comments on commit ec1b260

Please sign in to comment.