Skip to content
Merged
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
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ lint:

.PHONY: test
coverage="false"
test: deepcopy
test: deepcopy buildergen
make lint
@go test ./...

.PHONY: deepcopy
.PHONY: deepcopy buildergen
deepcopy: $(DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
./hack/deepcopy-gen.sh deepcopy

buildergen: $(BUILDER_GEN) ## Download builder-gen locally if necessary.
./hack/builder-gen.sh buildergen

.PHONY: kube-integration
kube-integration: controller-gen
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:allowDangerousTypes=true webhook paths="./..." output:crd:artifacts:config=config/crd/bases
Expand Down
53 changes: 53 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2023 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 (
"encoding/json"

"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 Object(builder *model.WorkflowBuilder) (*model.Workflow, error) {
workflow := builder.Build()
ctx := model.NewValidatorContext(&workflow)
if err := val.GetValidator().StructCtx(ctx, workflow); err != nil {
return nil, err
}
return &workflow, nil
}

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

func Yaml(builder *model.WorkflowBuilder) ([]byte, error) {
data, err := Json(builder)
if err != nil {
return nil, err
}
return yaml.JSONToYAML(data)
}
94 changes: 94 additions & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2023 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 (
"testing"

"github.com/serverlessworkflow/sdk-go/v2/model"
"github.com/stretchr/testify/assert"
)

func prepareBuilder() *model.WorkflowBuilder {
builder := New().Key("key test").ID("id test")

builder.AddFunctions().Name("function name").Operation("http://test")
builder.AddFunctions().Name("function name2").Operation("http://test")

function3 := builder.AddFunctions().Name("function name2").Operation("http://test")
builder.RemoveFunctions(function3)

state1 := builder.AddStates().
Name("state").
Type(model.StateTypeInject)
state1.End().Terminate(true)

inject := state1.InjectState()
inject.Data(map[string]model.Object{
"test": model.FromMap(map[string]any{}),
})

return builder
}

func TestObject(t *testing.T) {
workflow, err := Object(prepareBuilder())
if assert.NoError(t, err) {
assert.Equal(t, "key test", workflow.Key)
assert.Equal(t, "id test", workflow.ID)
assert.Equal(t, "0.8", workflow.SpecVersion)
assert.Equal(t, "jq", workflow.ExpressionLang.String())
assert.Equal(t, 2, len(workflow.Functions))

assert.Equal(t, "function name", workflow.Functions[0].Name)
assert.Equal(t, "function name2", workflow.Functions[1].Name)
}
}

func TestJson(t *testing.T) {
data, err := Json(prepareBuilder())
if assert.NoError(t, err) {
d := `{"id":"id test","key":"key test","version":"","specVersion":"0.8","expressionLang":"jq","states":[{"name":"state","type":"inject","end":{"terminate":true},"data":{"test":{}}}],"functions":[{"name":"function name","operation":"http://test","type":"rest"},{"name":"function name2","operation":"http://test","type":"rest"}]}`
assert.Equal(t, d, string(data))
}
}

func TestYaml(t *testing.T) {
data, err := Yaml(prepareBuilder())
if assert.NoError(t, err) {
d := `expressionLang: jq
functions:
- name: function name
operation: http://test
type: rest
- name: function name2
operation: http://test
type: rest
id: id test
key: key test
specVersion: "0.8"
states:
- data:
test: {}
end:
terminate: true
name: state
type: inject
version: ""
`

assert.Equal(t, d, string(data))
}
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.80.2-0.20221028030830-9ae4992afb54 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -81,8 +81,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -96,8 +96,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
Expand Down
48 changes: 48 additions & 0 deletions hack/builder-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
# Copyright 2022 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.

# retrieved from https://github.com/kubernetes/code-generator/blob/master/generate-internal-groups.sh
# and adapted to only install and run the deepcopy-gen

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
echo "Script root is $SCRIPT_ROOT"

GENS="$1"
shift 1

(
# To support running this script from anywhere, first cd into this directory,
# and then install with forced module mode on and fully qualified name.
# make sure your GOPATH env is properly set.
# it will go under $GOPATH/bin
cd "$(dirname "${0}")"
GO111MODULE=on go install github.com/galgotech/builder-gen@latest
)

function codegen::join() { local IFS="$1"; shift; echo "$*"; }

if [ "${GENS}" = "all" ] || grep -qw "buildergen" <<<"${GENS}"; then
echo "Generating buildergen funcs"
export GO111MODULE=on
# for debug purposes, increase the log level by updating the -v flag to higher numbers, e.g. -v 4
"${GOPATH}/bin/builder-gen" -v 1 \
--input-dirs ./model -O zz_generated.buildergen \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.txt"
"$@"
fi
2 changes: 2 additions & 0 deletions model/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package model
import "github.com/serverlessworkflow/sdk-go/v2/util"

// Action specify invocations of services or other workflows during workflow execution.
// +builder-gen:new-call=ApplyDefault
type Action struct {
// Defines Unique action identifier.
// +optional
Expand Down Expand Up @@ -72,6 +73,7 @@ func (a *Action) ApplyDefault() {
}

// FunctionRef defines the reference to a reusable function definition
// +builder-gen:new-call=ApplyDefault
type FunctionRef struct {
// Name of the referenced function.
// +kubebuilder:validation:Required
Expand Down
3 changes: 1 addition & 2 deletions model/action_data_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import "github.com/serverlessworkflow/sdk-go/v2/util"

// ActionDataFilter used to filter action data results.
// +optional
// +optional
// +builder-gen:new-call=ApplyDefault
type ActionDataFilter struct {
// Workflow expression that filters state data that can be used by the action.
// +optional
// +optional
FromStateData string `json:"fromStateData,omitempty"`
// If set to false, action data results are not added/merged to state data. In this case 'results'
// and 'toStateData' should be ignored. Default is true.
Expand Down
3 changes: 3 additions & 0 deletions model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
)

// Event used to define events and their correlations
// +builder-gen:new-call=ApplyDefault
type Event struct {
Common `json:",inline"`
// Unique event name.
Expand All @@ -56,6 +57,7 @@ type Event struct {
Kind EventKind `json:"kind,omitempty" validate:"required,oneofkind"`
// If `true`, only the Event payload is accessible to consuming Workflow states. If `false`, both event payload
// and context attributes should be accessible. Defaults to true.
// +kubebuilder:default=true
// +optional
DataOnly bool `json:"dataOnly,omitempty"`
// Define event correlation rules for this event. Only used for consumed events.
Expand Down Expand Up @@ -88,6 +90,7 @@ type Correlation struct {
}

// EventRef defining invocation of a function via event
// +builder-gen:new-call=ApplyDefault
type EventRef struct {
// Reference to the unique name of a 'produced' event definition,
// +kubebuilder:validation:Required
Expand Down
1 change: 1 addition & 0 deletions model/event_data_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package model
import "github.com/serverlessworkflow/sdk-go/v2/util"

// EventDataFilter used to filter consumed event payloads.
// +builder-gen:new-call=ApplyDefault
type EventDataFilter struct {
// If set to false, event payload is not added/merged to state data. In this case 'data' and 'toStateData'
// should be ignored. Default is true.
Expand Down
2 changes: 2 additions & 0 deletions model/event_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

// EventState await one or more events and perform actions when they are received. If defined as the
// workflow starting state, the event state definition controls when the workflow instances should be created.
// +builder-gen:new-call=ApplyDefault
type EventState struct {
// TODO: EventState doesn't have usedForCompensation field.

Expand Down Expand Up @@ -64,6 +65,7 @@ func (e *EventState) ApplyDefault() {
}

// OnEvents define which actions are be performed for the one or more events.
// +builder-gen:new-call=ApplyDefault
type OnEvents struct {
// References one or more unique event names in the defined workflow events.
// +kubebuilder:validation:MinItems=1
Expand Down
1 change: 1 addition & 0 deletions model/foreach_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
)

// ForEachState used to execute actions for each element of a data set.
// +builder-gen:new-call=ApplyDefault
type ForEachState struct {
// Workflow expression selecting an array element of the states' data.
// +kubebuilder:validation:Required
Expand Down
1 change: 1 addition & 0 deletions model/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (i FunctionType) String() string {
}

// Function ...
// +builder-gen:new-call=ApplyDefault
type Function struct {
Common `json:",inline"`
// Unique function name
Expand Down
1 change: 1 addition & 0 deletions model/operation_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

// OperationState defines a set of actions to be performed in sequence or in parallel.
// +builder-gen:new-call=ApplyDefault
type OperationState struct {
// Specifies whether actions are performed in sequence or in parallel, defaults to sequential.
// +kubebuilder:validation:Enum=sequential;parallel
Expand Down
1 change: 1 addition & 0 deletions model/parallel_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
)

// ParallelState Consists of a number of states that are executed in parallel
// +builder-gen:new-call=ApplyDefault
type ParallelState struct {
// List of branches for this parallel state.
// +kubebuilder:validation:MinItems=1
Expand Down
1 change: 1 addition & 0 deletions model/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

// Retry ...
// +builder-gen:new-call=ApplyDefault
type Retry struct {
// Unique retry strategy name
// +kubebuilder:validation:Required
Expand Down
1 change: 1 addition & 0 deletions model/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (b *BaseState) MarshalJSON() ([]byte, error) {
return cus, err
}

// +builder-gen:embedded-ignore-method=BaseState
type State struct {
BaseState `json:",inline"`
// delayState Causes the workflow execution to delay for a specified duration.
Expand Down
Loading