Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk-go proposal for DSL v1.0.0-alpha2 #204

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ lint:
.PHONY: test
coverage="false"

test: deepcopy buildergen
#test: deepcopy buildergen
test:
make lint
@go test ./...

Expand Down
44 changes: 18 additions & 26 deletions builder/builder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The Serverless Workflow Specification Authors
// Copyright 2024 The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,45 +17,37 @@ package builder
import (
"encoding/json"

"github.com/serverlessworkflow/sdk-go/v4/validate"
"sigs.k8s.io/yaml"

"github.com/serverlessworkflow/sdk-go/v2/model"
val "github.com/serverlessworkflow/sdk-go/v2/validator"
)

func New() *model.WorkflowBuilder {
return model.NewWorkflowBuilder()
}

func Yaml(builder *model.WorkflowBuilder) ([]byte, error) {
func Validate(builder *WorkflowBuilder) error {
data, err := Json(builder)
if err != nil {
return nil, err
return err
}
return yaml.JSONToYAML(data)
}

func Json(builder *model.WorkflowBuilder) ([]byte, error) {
workflow, err := Object(builder)
err = validate.FromJSONSource(data)
if err != nil {
return nil, err
return err
}
return json.Marshal(workflow)

return nil
}

func Object(builder *model.WorkflowBuilder) (*model.Workflow, error) {
workflow := builder.Build()
ctx := model.NewValidatorContext(&workflow)
if err := val.GetValidator().StructCtx(ctx, workflow); err != nil {
func Json(builder *WorkflowBuilder) ([]byte, error) {
data, err := json.MarshalIndent(builder.Node(), "", " ")
if err != nil {
return nil, err
}
return &workflow, nil

return data, nil
}

func Validate(object interface{}) error {
ctx := model.NewValidatorContext(object)
if err := val.GetValidator().StructCtx(ctx, object); err != nil {
return val.WorkflowError(err)
func Yaml(builder *WorkflowBuilder) ([]byte, error) {
data, err := Json(builder)
if err != nil {
return nil, err
}
return nil
return yaml.JSONToYAML(data)
}
120 changes: 0 additions & 120 deletions builder/builder_test.go

This file was deleted.

51 changes: 51 additions & 0 deletions builder/call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import "github.com/serverlessworkflow/sdk-go/v4/graph"

type CallKind string

const (
CallKindHttp CallKind = "http"
CallKindGrpc CallKind = "grpc"
)

type CallBuilder struct {
root *graph.Node
with *MapBuilder
}

func (b *CallBuilder) SetCall(call CallKind) *CallBuilder {
b.root.Edge("call").SetString(string(call))
return b
}

func (b *CallBuilder) GetCall() string {
return b.root.Edge("call").GetString()
}

func (b *CallBuilder) With() *MapBuilder {
if b.with == nil {
b.with = NewMapBuilder(b.root.Edge("with"))
}
return b.with
}

func NewCallBuilder(root *graph.Node) *CallBuilder {
return &CallBuilder{
root: root,
}
}
59 changes: 59 additions & 0 deletions builder/do.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import (
"fmt"

"github.com/serverlessworkflow/sdk-go/v4/graph"
)

type DoBuilder struct {
root *graph.Node
tasks []any
}

func (b *DoBuilder) AddCall(name string) (*CallBuilder, int) {
index := len(b.tasks)
nodeIndex := b.root.Edge(fmt.Sprintf("%d", index))
nodeName := nodeIndex.Edge(name)

callBuilder := NewCallBuilder(nodeName)
b.tasks = append(b.tasks, callBuilder)
return callBuilder, index
}

func (b *DoBuilder) AddWait(name string) (*WaitBuilder, int) {
index := len(b.tasks)
nodeIndex := b.root.Edge(fmt.Sprintf("%d", index))
nodeName := nodeIndex.Edge(name)

waitBuilder := NewWaitBuilder(nodeName)
b.tasks = append(b.tasks, waitBuilder)
return waitBuilder, index
}

func (b *DoBuilder) RemoveTask(index int) *DoBuilder {
b.tasks = append(b.tasks[:index], b.tasks[index+1:]...)
return b
}

func NewDoBuilder(root *graph.Node) *DoBuilder {
root.List(true)
return &DoBuilder{
root: root,
tasks: []any{},
}
}
76 changes: 76 additions & 0 deletions builder/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2024 The Serverless Workflow Specification Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import (
"github.com/serverlessworkflow/sdk-go/v4/graph"
"github.com/serverlessworkflow/sdk-go/v4/internal/dsl"
)

type DocumentBuilder struct {
root *graph.Node
}

func (b *DocumentBuilder) SetDSL(dsl string) *DocumentBuilder {
node := b.root.Edge("dsl")
node.SetString(dsl)
return b
}

func (b *DocumentBuilder) GetDSL() string {
node := b.root.Edge("dsl")
return node.GetString()
}

func (b *DocumentBuilder) SetNamespace(dsl string) *DocumentBuilder {
node := b.root.Edge("namespace")
node.SetString(dsl)
return b
}

func (b *DocumentBuilder) GetNamespace() string {
node := b.root.Edge("namespace")
return node.GetString()
}

func (b *DocumentBuilder) SetName(dsl string) *DocumentBuilder {
node := b.root.Edge("name")
node.SetString(dsl)
return b
}

func (b *DocumentBuilder) GetName() string {
node := b.root.Edge("name")
return node.GetString()
}

func (b *DocumentBuilder) SetVersion(dsl string) *DocumentBuilder {
node := b.root.Edge("version")
node.SetString(dsl)
return b
}

func (b *DocumentBuilder) GetVersion() string {
node := b.root.Edge("version")
return node.GetString()
}

func NewDocumentBuilder(root *graph.Node) *DocumentBuilder {
documentBuilder := &DocumentBuilder{
root: root,
}
documentBuilder.SetDSL(dsl.DSLVersion)
return documentBuilder
}
Loading
Loading