Skip to content

Commit

Permalink
Merge branch 'main' into feature/table_printer
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
2 parents b96770d + 9e8b547 commit c10c7ce
Show file tree
Hide file tree
Showing 70 changed files with 903 additions and 1,544 deletions.
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@
Uniflow is a low-code engine that enables fast and efficient construction and execution of backend workflows.

## Getting Started
### Installation
1. [Download Go][go_download_url]: Install **Go** (version `1.21` or higher is required).
2. Clone the repository and initialize:
```shell
git clone https://github.com/siyul-park/uniflow
cd uniflow
make init
```

### Build
1. Build the project:
```shell
make build
```
2. Check the build result:
```shell
ls /dist
uniflow
```
3. Run tests:
```shell
make test
```
### Install & Build
[Download Go][go_download_url] and install (version `1.21` or higher is required).

Clone the repository and initialize.

```shell
git clone https://github.com/siyul-park/uniflow
cd uniflow
make init
```

Build the project and check the result.

```shell
make build
```

```shell
ls /dist
uniflow
```

Run a test to see if it's working properly.

```shell
make test
```

### Start
Uniflow is now ready to be used. Let's start the [ping](/examples/ping.yaml) example.
Expand Down
83 changes: 83 additions & 0 deletions cmd/resource/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package resource

import (
"io"
"io/fs"

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

type (
Builder struct {
scheme *scheme.Scheme
namespace string
fsys fs.FS
filename string
}
)

func NewBuilder() *Builder {
return &Builder{}
}

func (b *Builder) Scheme(scheme *scheme.Scheme) *Builder {
b.scheme = scheme
return b
}

func (b *Builder) Namespace(namespace string) *Builder {
b.namespace = namespace
return b
}

func (b *Builder) FS(fsys fs.FS) *Builder {
b.fsys = fsys
return b
}

func (b *Builder) Filename(filename string) *Builder {
b.filename = filename
return b
}

func (b *Builder) Build() ([]scheme.Spec, error) {
if b.fsys == nil || b.filename == "" {
return nil, nil
}
file, err := b.fsys.Open(b.filename)
if err != nil {
return nil, err
}
defer file.Close()

data, err := io.ReadAll(file)
if err != nil {
return nil, err
}

var raws []map[string]any
if err := UnmarshalYAMLOrJSON(data, &raws); err != nil {
var e map[string]any
if err := UnmarshalYAMLOrJSON(data, &e); err != nil {
return nil, err
} else {
raws = []map[string]any{e}
}
}

codec := NewSpecCodec(SpecCodecOptions{
Scheme: b.scheme,
Namespace: b.namespace,
})

var specs []scheme.Spec
for _, raw := range raws {
if spec, err := codec.Decode(raw); err != nil {
return nil, err
} else {
specs = append(specs, spec)
}
}

return specs, nil
}
50 changes: 50 additions & 0 deletions cmd/resource/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package resource

import (
"encoding/json"
"testing"
"testing/fstest"

"github.com/go-faker/faker/v4"
"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/stretchr/testify/assert"
)

func TestBuilder_Build(t *testing.T) {
s := scheme.New()
fsys := make(fstest.MapFS)

filename := "spec.json"
kind := faker.Word()

spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
s.AddCodec(kind, codec)

data, _ := json.Marshal(spec)

fsys[filename] = &fstest.MapFile{
Data: data,
}

builder := NewBuilder().
Scheme(s).
Namespace(scheme.NamespaceDefault).
FS(fsys).
Filename(filename)

specs, err := builder.Build()
assert.NoError(t, err)
assert.Len(t, specs, 1)
}
2 changes: 1 addition & 1 deletion cmd/resource/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package resource

import (
"github.com/pkg/errors"
"github.com/siyul-park/uniflow/internal/encoding"
"github.com/siyul-park/uniflow/pkg/encoding"
"github.com/siyul-park/uniflow/pkg/primitive"
"github.com/siyul-park/uniflow/pkg/scheme"
)
Expand Down
43 changes: 9 additions & 34 deletions cmd/uniflow/apply/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package apply

import (
"fmt"
"io"
"io/fs"

"github.com/oklog/ulid/v2"
"github.com/samber/lo"
"github.com/siyul-park/uniflow/cmd/flag"
"github.com/siyul-park/uniflow/cmd/printer"
"github.com/siyul-park/uniflow/cmd/resource"
"github.com/siyul-park/uniflow/internal/util"
"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/storage"
Expand Down Expand Up @@ -77,43 +76,19 @@ func NewCmd(config Config) *cobra.Command {
return err
}

file, err := fsys.Open(fl)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
b := resource.NewBuilder().
Scheme(sc).
Namespace(ns).
FS(fsys).
Filename(fl)

data, err := io.ReadAll(file)
specs, err := b.Build()
if err != nil {
return err
}

var raws []map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &raws); err != nil {
var e map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &e); err != nil {
return err
} else {
raws = []map[string]any{e}
}
}

codec := resource.NewSpecCodec(resource.SpecCodecOptions{
Scheme: sc,
Namespace: ns,
})

var specs []scheme.Spec
for _, raw := range raws {
if spec, err := codec.Decode(raw); err != nil {
return err
} else {
specs = append(specs, spec)
}
}

for _, spec := range specs {
if util.IsZero(spec.GetID()) {
if spec.GetID() == (ulid.ULID{}) {
if spec.GetName() != "" {
filter := storage.Where[string](scheme.KeyName).EQ(spec.GetName()).And(storage.Where[string](scheme.KeyNamespace).EQ(spec.GetNamespace()))
if exist, err := st.FindOne(ctx, filter); err != nil {
Expand All @@ -133,7 +108,7 @@ func NewCmd(config Config) *cobra.Command {
}

exists, err := st.FindMany(ctx, storage.Where[ulid.ULID](scheme.KeyID).IN(ids...), &database.FindOptions{
Limit: util.Ptr[int](len(ids)),
Limit: lo.ToPtr[int](len(ids)),
})
if err != nil {
return err
Expand Down
41 changes: 8 additions & 33 deletions cmd/uniflow/start/cmd.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package start

import (
"io"
"io/fs"
"os"
"os/signal"
"syscall"

"github.com/samber/lo"
"github.com/siyul-park/uniflow/cmd/flag"
"github.com/siyul-park/uniflow/cmd/resource"
"github.com/siyul-park/uniflow/internal/util"
"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/hook"
"github.com/siyul-park/uniflow/pkg/runtime"
Expand Down Expand Up @@ -63,45 +62,21 @@ func NewCmd(config Config) *cobra.Command {
}

if specs, err := st.FindMany(ctx, filter, &database.FindOptions{
Limit: util.Ptr[int](1),
Limit: lo.ToPtr[int](1),
}); err != nil {
return err
} else if len(specs) == 0 {
file, err := fsys.Open(boot)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
b := resource.NewBuilder().
Scheme(sc).
Namespace(ns).
FS(fsys).
Filename(boot)

data, err := io.ReadAll(file)
specs, err := b.Build()
if err != nil {
return err
}

var raws []map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &raws); err != nil {
var e map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &e); err != nil {
return err
} else {
raws = []map[string]any{e}
}
}

codec := resource.NewSpecCodec(resource.SpecCodecOptions{
Scheme: sc,
Namespace: ns,
})

var specs []scheme.Spec
for _, raw := range raws {
if spec, err := codec.Decode(raw); err != nil {
return err
} else {
specs = append(specs, spec)
}
}

if _, err := st.InsertMany(ctx, specs); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions examples/echo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: http
address: :8000
links:
io:
out:
- name: router
port: in

Expand All @@ -15,4 +15,4 @@
links:
out[0]:
- name: http
port: io
port: in
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ go 1.21.0
require (
github.com/benbjohnson/immutable v0.4.3
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d
github.com/evanw/esbuild v0.19.5
github.com/emirpasic/gods v1.18.1
github.com/evanw/esbuild v0.19.7
github.com/go-faker/faker/v4 v4.2.0
github.com/iancoleman/strcase v0.3.0
github.com/jedib0t/go-pretty/v6 v6.4.9
github.com/lithammer/dedent v1.1.0
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/oklog/ulid/v2 v2.1.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.38.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/tryvium-travels/memongo v0.10.0
github.com/xiatechs/jsonata-go v1.7.0
github.com/xiatechs/jsonata-go v1.7.1
go.mongodb.org/mongo-driver v1.12.1
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
Loading

0 comments on commit c10c7ce

Please sign in to comment.