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

Programmatically build workflow #190

Merged

Conversation

ribeiromiranda
Copy link
Collaborator

@ribeiromiranda ribeiromiranda commented Sep 27, 2023

Proposal for programmatically build workflow definitions.

Example:

workflowBuilder, builder := NewWorkflowBuilder("test", "1.0")
workflowBuilder.
  Function("function0", "http://...").
  Parent().
  Event("event0").
  Parent().
  OperationState("state0").Action("action0").FunctionRef("function0").
  Parent().
  Parent().
  Parent().
  OperationState("state1").Action("action0").FunctionRef("function0")

workflowBuilder.OperationState("state2").Action("action0").FunctionRef("function1")
workflowBuilder.Function("function1", "http://...")
workflow := builder.Build()

Signed-off-by: André R. de Miranda <andre@galgo.tech>
Copy link
Member

@ricardozanini ricardozanini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvement to the SDK! A few suggestions, if you don't mind!

model/builder/action.go Outdated Show resolved Hide resolved
return workflowBuilder, builder
}

type WorkflowBuilder struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about an interface instead? Hence, we can model different builders if needed and have a clear API.

Also, can we have a WorkflowBuilder.BuildAsYaml(path)? This way we can persist into a file programmatically. wdyt?

Copy link
Collaborator Author

@ribeiromiranda ribeiromiranda Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The builder interface has been decoupled from the WorkflowBuilder, so it is possible to have an ActionBuilder

It is possible to do this as follows:

type Builder[T, K any] struct {
	builder Build[K]
}

func (b *Builder[T, K]) Build() K {
	return b.builder.Build()
}

func (b *Builder[T, K]) BuildAsYaml() string {
	return b.builder.BuildAsYaml()
}

type Build[T any] interface {
	Build() T
	BuildAsYaml() string
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you implement it in this PR? We may use our functions to generate the file.

return workflowBuilder, builder
}

type WorkflowBuilder struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you implement it in this PR? We may use our functions to generate the file.

Copy link
Member

@spolti spolti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, just one thing, maybe improve the tests.

Added a small comment about it.

Other than that, just address the open Zanini's questions. :)

Thanks very much!

"github.com/stretchr/testify/assert"
)

func TestNewWorkflowBuilder(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the test, can you please improve it?

E.g., use a complete workflow that we have in the examples, build it using the Builder, then parse it to yaml or json and compare the final result with the example we have used as base.

does it make sense?

@spolti
Copy link
Member

spolti commented Oct 31, 2023

@ribeiromiranda hi, do you still have plans to continue working on this PR?

Thanks.

@ribeiromiranda
Copy link
Collaborator Author

@spolti I'm looking at creating a "builder-gen" as the "deepcopy-gen". But, I'm short on time.

@spolti
Copy link
Member

spolti commented Oct 31, 2023

Nice, no problem, take your time and many thanks!!!

Signed-off-by: André R. de Miranda <andre@galgo.tech>
Signed-off-by: André R. de Miranda <andre@galgo.tech>
@ribeiromiranda
Copy link
Collaborator Author

ribeiromiranda commented Nov 15, 2023

@ricardozanini @spolti Hi,

Generated the file model/zz_buildergen_generated.go with generator builder-gen.
In the file model/builder/builder_test.go there is an example of use.
Do you agree with this builder format?

Some pending issues detected:

  • Support to type floatstr.Float32OrString
  • Support to type Object
  • Support to type Map
  • Execute validation

Generator used:
https://github.com/galgotech/gengo/blob/builder-gen/examples/builder-gen/generators/builder.go

Signed-off-by: André R. de Miranda <andre@galgo.tech>
Copy link
Member

@ricardozanini ricardozanini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool!

Signed-off-by: André R. de Miranda <andre@galgo.tech>
Signed-off-by: André R. de Miranda <andre@galgo.tech>
@ribeiromiranda ribeiromiranda force-pushed the programmatically_build branch 2 times, most recently from fc09bbb to 2f0f3ef Compare November 21, 2023 19:43
Signed-off-by: André R. de Miranda <andre@galgo.tech>
@ricardozanini
Copy link
Member

@ribeiromiranda when you finish, can you let us know so we can do a final review/

@ribeiromiranda
Copy link
Collaborator Author

@ribeiromiranda when you finish, can you let us know so we can do a final review/

@ricardozanini I think for a first version is ready for review.

Copy link
Member

@ricardozanini ricardozanini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ricardozanini
Copy link
Member

Closes #154

I missed the CI from my notifications, merging it.

@ricardozanini ricardozanini linked an issue Dec 18, 2023 that may be closed by this pull request
@ricardozanini ricardozanini merged commit 48c5d8e into serverlessworkflow:main Dec 18, 2023
6 checks passed
@ribeiromiranda ribeiromiranda deleted the programmatically_build branch December 27, 2023 02:17
@ricardozanini ricardozanini linked an issue Dec 28, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants