Skip to content

Conversation

ribeiromiranda
Copy link
Contributor

@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 <[email protected]>
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!

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
Contributor 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
Contributor 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!!!

André R. de Miranda added 2 commits November 15, 2023 14:35
@ribeiromiranda
Copy link
Contributor 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 <[email protected]>
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!

André R. de Miranda added 3 commits November 18, 2023 00:19
@ricardozanini
Copy link
Member

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

@ribeiromiranda
Copy link
Contributor 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
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal for programmatically build workflow definitions Programmatically build workflow definitions with fluent api
3 participants