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

Pipeline viz #4

Merged
merged 7 commits into from
Feb 8, 2024
Merged

Pipeline viz #4

merged 7 commits into from
Feb 8, 2024

Conversation

fabalex7
Copy link
Owner

introduced the builder pattern


blockType(block: BlockDefinition): MermaidBuilder {
this.mermaidLines.push(
`${block.name}[${block.name}<br><i>${block.type.ref?.name}</i>]`,
Copy link

Choose a reason for hiding this comment

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

Invalid type "string | undefined" of template literal expression

-block.type.ref?.name
+block.type.ref?.name ?? ''

Copy link

Choose a reason for hiding this comment

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

Better: Assert non-nullish

Copy link
Owner Author

Choose a reason for hiding this comment

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

move assert into builder


blockTypeWithProperties(
block: BlockDefinition,
propertyString: string,
Copy link

Choose a reason for hiding this comment

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

You did right with differentiating between blockType and blockTypeWithProperties, however, the propertyString needs to be valid mermaid syntax and thus leaks implementation details of the builder.

Ideally, the parameter is some kind of key-value object.
The proper mermaid formatting of these properties, including making sure of the proper formatting (escapeHtml), is part of your concrete builder, not the caller of the builder (mermaid_utils.ts).

Copy link
Owner Author

Choose a reason for hiding this comment

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

combine into only one blockType
processes dictionary with properties (depending on mermaid options)

export class MermaidBuilder {
protected mermaidLines: string[] = [];

pipelineHead(pipelineName: string): MermaidBuilder {
Copy link

Choose a reason for hiding this comment

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

Minor thing: Consistency is king/queen.
You used BlockDefinition as a parameter in other functions and just a name here. But as I've said this is a very minor thing that you'll probably automatically do when you have a bit more experience.

Using BlockDefinition makes more sense in this case IMO.

Copy link
Owner Author

Choose a reason for hiding this comment

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

do you mean a PipelineDefinition?

return this;
}

compositeBody(subblocks: string[], depth: number): MermaidBuilder {
Copy link

Choose a reason for hiding this comment

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

depth can be an internal variable. It's the builder's job to make the output nice.
Opening a composite then increases the depth, closing decreases it.

A more common name for depth in this context is indent btw :)

return this;
}

classAssign(block: BlockDefinition, className: string): MermaidBuilder {
Copy link

Choose a reason for hiding this comment

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

The Builder should do that itself when build is called.

Copy link

Choose a reason for hiding this comment

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

You can ignore that

return this;
}

newLine(): MermaidBuilder {
Copy link

Choose a reason for hiding this comment

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

This is not the job of a caller of a mermaid builder.

When deciding if a method should be available to a caller, try asking yourself this question:

Does it make sense to do X when building a diagram?

E.g. "Does it make sense to insert a new line when building a diagram?" - Not really, right?
However, it does make sense for a mermaid diagram Builder to produce a pretty output with a new line. Therefore, this method is useful for internal use only.

Copy link
Owner Author

Choose a reason for hiding this comment

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

ok, I could make it a protected method
But how do I achieve the same behavior? Currently, the caller knows in which cases I want a new line after a section.

Copy link
Owner Author

Choose a reason for hiding this comment

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

split into different parts of members and connect them with new-line

return this;
}

section(section: string): MermaidBuilder {
Copy link

Choose a reason for hiding this comment

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

What is a section?

Copy link

Choose a reason for hiding this comment

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

Ah, I got it when reading mermaid_utils.ts. Don't build too early, just continue with the same builder (i.e. use just one instance of a builder = just one new MermaidBuilder()).

Copy link
Owner Author

Choose a reason for hiding this comment

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

but that would require passing the builder around, instead of adding the output of a (sub)-builder as a section

Copy link
Owner Author

Choose a reason for hiding this comment

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

remove sections and write lines directly

Copy link

@joluj joluj left a comment

Choose a reason for hiding this comment

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

LGTM.

I've left two comments. Both are rather FYI and don't necessarily need to be changed.


constructor(protected options: MermaidOptions) {}

escapeHtml(unsafe: string) {
Copy link

Choose a reason for hiding this comment

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

(Improvement, not required) Make methods that a caller doesn't need or shouldn't use protected.

.replace(/'/g, '&#039;');
}

diagram(diagramType: string, diagramDirection: string) {
Copy link

Choose a reason for hiding this comment

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

(Optional improvement / if you want to flex with your TypeScript Skills) Use literal types.

E.g. diagram(diagramType: string, diagramDirection: 'TD' | 'LR') { (Use the values mermaid understands).

Also, be aware that implementation details are leaked here. However, in this case, I'm fine with that (since these are rather generic instructors that just happen to be mermaid syntax).

@fabalex7 fabalex7 merged commit dc3fb28 into main Feb 8, 2024
2 of 3 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Feb 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants