Macro for processing collections

0 votes
asked Jan 25, 2019 in Wanted features by MR

Dear plantuml,

Do you reckon it would be possible to implement support for macros that can handle collection on the input?

My use case for this is the ability to enter a collection into the macro without knowing how many elements will be input, in order to build up some shared macros that can be used by multiple people and potentially updated to make global changes in formatting/styling that would affect our whole repository of specifications.

An example of what's possible:

@startuml

!define descHeader(element) element : <size:10>**Some formatting headline...**</size>
!define descRow(element,text) element : <size:10> text</size>

state A
state B

descHeader(A)
descHeader(B)

descRow(A,some quality of A)
descRow(A,another quality of A)
descRow(B,some quality of B)
descRow(B,some flaw of B)

@enduml

Now here I know that I have two states, A and B, so I could do this:

@startuml

!definelong descHeaders(e1,e2)
descHeader(e1)
descHeader(e2)
!enddefinelong
!define descHeader(element) element : <size:10>**Some formatting headline...**</size>
!define descRow(element,text) element : <size:10> text</size>

state A
state B

descHeaders(A,B)

descRow(A,some quality of A)
descRow(A,another quality of A)
descRow(B,some quality of B)
descRow(B,some flaw of B)

@enduml

But what I would like to be able to achieve is something like this:

@startuml

!defineloop descHeaders(elements) element : <size:10>**Some formatting headline...**</size>
!define descRow(element,text) element : <size:10> text</size>

state A
state B

descHeaders(A,B)

descRow(A,some quality of A)
descRow(A,another quality of A)
descRow(B,some quality of B)
descRow(B,some flaw of B)

@enduml

Without having to worry how many elements I enter, so if the situation demanded it, I could do for instance descHeaders(A,B,C,D,E,F,G,H,I,J,K,L,M,N), which would totally beat the readability out of having to write descHeader(A), descHeader(B), ... that would be spread over many lines (14 in this particular example) which would only compromise readability and maintainability.

Obviously the syntax above is just first thing that came to mind and it might need to be a bit more complicated to enable more elaborate loops, but in fact just this would be quite a leap ahead for me.

1 Answer

0 votes
answered May 25, 2020 by The-Lu (64,340 points)

Hello M.,

According to the current version (with ‘procedure’ [from V1.2020.7], and json management).

See:

We can now use the json data as:

!$data={
    "elements": ["A", "B", "C", "D", "..."]
}

And use a foreach loop:

  !foreach $element in $elements
    $element : <size:10>**Some formatting headline...**</size>
  !endfor

And create a procedure:

!procedure descHeaders($elements)
  !foreach $element in $elements
    state $element
    $element : <size:10>**Some formatting headline...**</size>
  !endfor
!endprocedure

With that, we obtain:

PlantUML diagram
[Click to see code on PlantUML online server]

If that can help,
Regards,
Th.

...