json structures inside a function

0 votes
asked Apr 13, 2020 in Question / help by gobravedave (200 points)

2 Answers

+1 vote
answered Apr 16, 2020 by plantuml (257,940 points)
selected Apr 18, 2020 by gobravedave
commented Apr 16, 2020 by Serge Wenger Work (14,160 points)

I agree with procedure for  void function (like old Pascal yes)

commented Apr 17, 2020 by plantuml (257,940 points)

We've just published a new beta http://beta.plantuml.net/plantuml.jar which uses !procedure now.

This will be offically released in next version. It has a slight impact on some existing diagrams, because some !function definitions have to be changed into !procedure definition. But I think it's more clear this way, and that it's not too late to do this important move.

See some info here https://forum.plantuml.net/11219/multiple-function-calls-in-text-only-one-processed

commented Apr 18, 2020 by gobravedave (200 points)
thank you thank you
0 votes
answered Apr 14, 2020 by The-Lu (31,620 points)

Hello David,

1/ First: We will test without redirection on a variable ($d)

and if you want to use 'unquoted', it is necessary to suppress the double-quote on the test :

  !if $part.id == $id

Here is the first full example (and that is OK):

@startuml
!unquoted function $lookup($id)
!$data={"participants":[
{"id": "cm", "label": "label cm", "desc": "desc cm"},
{"id": "tm", "label": "label tm", "desc": "desc tm"},
{"id": "am", "label": "label am", "desc": "desc am"}
]}
!foreach $part in $data.participants
  !if $part.id == $id
  :id=$part.id \nlabel=$part.label \ndesc=$part.desc;
  !endif;
!endfor
!endfunction

!$a="cm"
:$a;
$lookup($a)
$lookup(cm)
$lookup("cm")

'!$d=$lookup(cm)
@enduml

http://www.plantuml.com/plantuml/umla/RP1HReCm38RVVGedYhGXLJq0A5BliUt1YuDc3G8hoLF5tOS1iKxjatzz-CEnBrEcQovz1wQadpBaoDYM5B8C2Lqt3DzbV7N25HZ7bCdVxBW84cIab2TRly7D2jiQRUZj2Mr7dx6pzHvmWnode4ohelbqMFcPbH-j_C-YPnOzM8fc-9Z1jCCrKlX2fwz7IRXkSxxV1H2Dj5l7MHYzHoUyq5hO7_GzhSDsiEQ5wR0TQMpqNp6nj0reNQPlTRyiW77azM9GEshWE3HLHmxzNzRE2k35EFRttoynSUcxNm00


2/ Then: if you want a redirection on a variable ($d), perhaps it is necessary to use a return-function.

And if we add this :

!return ":id=$part.id \nlabel=$part.label \ndesc=$part.desc;"

We observe this defect: 500 - Internal Server Error

Here is the second full example, please unquote the line '!$d=$lookup(cm) :

@startuml
!unquoted function $lookup($id)
!$data={"participants":[
{"id": "cm", "label": "label cm", "desc": "desc cm"},
{"id": "tm", "label": "label tm", "desc": "desc tm"},
{"id": "am", "label": "label am", "desc": "desc am"}
]}
!foreach $part in $data.participants
  !if $part.id == $id
  !return ":id=$part.id \nlabel=$part.label \ndesc=$part.desc;"
  !endif;
!endfor
!endfunction

:begin;

'!$d=$lookup(cm)
'$d

:end;
@enduml

Here is the link:
http://www.plantuml.com/plantuml/umla/RP1TReCm383VVGeJHUegLJq0A5BliUqXJS9cBIGCdAUgTvy3hDfKNh3vua_-kSni9ov3XAQaxv8vUEnBSamveOuvVvNnHPC_GgEzPMjkQXI178qsyQpQLxWfygf5vGPrGXNjDKJvNHDSeG-pgwp6YkwdXyLx5ZzR_C-oUvPzjgnOy7w7fizJiEuJTPqUAU6opVdlBe3OKBzMdCcZCIYBLpe5BbD2rP8tZ-Utj7JTm98BgrqtLDDELJyaJtq7DSeSQznk3D1UmmUb3k0W5pQ_9tV34GxQoxCKTt2HRndY3m00

If it can help,
Regards,
Th.

commented Apr 14, 2020 by gobravedave (200 points)

thank you TL.. it has given me some pointers, however all the options come to a halt when you need to return a value.

the following works..

@startuml dump json table function with no return

!unquoted function $dump ()

!$data={"participants" :

{"id""cm" ,"label""customer master","desc""manages customers"},

{"id""tm" ,"label""transaction manager","desc""manages transactions"},

{"id""am" ,"label""account master","desc""manages accounts"}

]}

class dump {

!foreach $part in $data.participants

   $part.desc;

!endfor

}

!endfunction

$dump()

@enduml

results with..

http://www.plantuml.com/plantuml/uml/TP3HJkim38Rl_HHkLEaSHBK7w8Iqzq1SU4d6WXAd98wugFhkfEc0LO8xM_dznVvEMIX92HvD2HE-vSWeTFOMBuMrkDf-EBaYHqnMIcBe2h-NADRy9Fes-_y1kjwGqDEifWfrsat4aXMEpmYpSaQDg7HGE2XFP-jRMxB4O1C6ocAJ6fInMQylWPXURSQlH5RBy4sHFKKISQPjcJRtA-akjOFH7aPQnyBozqQtG6N0omBmRspdhvSVGNlAUNCvGtU9oPA-Oh_wGCVO_1pkxG0YFcw1m_hH4JhBfWx2ibKtoG3DSfKC9ykrz9y0

 the following now works on the beta 2020.7 but not on .6.

@startuml
!unquoted function $dump ($id)
!$data={"participants" :[ 
{"id": "cm" ,"label": "customer master","desc": "manages customers"},
{"id": "tm" ,"label": "transaction manager","desc": "manages transactions"},
{"id": "am" ,"label": "account master","desc": "manages accounts"}
]}

!foreach $part in $data.participants
  !if $part.id == $id
    class dump {
      - $part.id
      + $part.label ()
      + $part.desc ()
      }
  !endif
!endfor
!endfunction

'!$am="am"
'$dump($am")
$dump("am")
@enduml

http://www.plantuml.com/plantuml/uml/TL5RRi8m4FpVKqoiI81AEG1I9EvHzMEndU8gTgYzLZyGTw_Zy82g_LhFU7OqkxjBmb6o7s2oF-8pZG72-y6Ypq6BA_3RoG6kHnWHhUGOgCdXAuzYpLtKrlPbwyoAcjQmS7TInsBjj3joaAImVGETb3Dg2wMzmbeDlBT3XJd9w6s4voGsghKoDkdfntFW3vjmLIHrNjzSvDb58eV4SvxQzwVJW-h9Z9_DMEin1_a_qKLGFEZzJDJqOxIi3sYdgU42wXOsZpiWe77zhDWuWwv3MLXX0JrmIlCDJfK0Nc_22_5o8Mf6B5U_w2dWdJsNsjXWN4zJAUbglHwCQD6qxBjfR5hKwoqBLYkQtwg-QMT3WSCF

But even trying the quoting the return string with and without trailing spaces still doesn't work.

@startuml
!unquoted function $lookup($id)
!$data={"participants":[
{"id": "cm", "label": "label cm", "desc": "desc cm"},
{"id": "tm", "label": "label tm", "desc": "desc tm"},
{"id": "am", "label": "label am", "desc": "desc am"}
]}
!foreach $part in $data.participants
  !if $part.id == $id
    '!return "id=$part.id \nlabel=$part.label \ndesc=$part.desc";
    '!return id=$part.id \nlabel=$part.label \ndesc=$part.desc;
    !return "id=$part.id \nlabel=$part.label \ndesc=$part.desc"
  !endif
!endfor
!endfunction

!$a="cm"
$lookup($a)
$lookup(am)
$lookup("tm")

'!$note = $lookup("cm")
component dump [
$lookup("cm")
]  

'!$d=$lookup(cm)
@enduml

http://www.plantuml.com/plantuml/uml/fP71Ri8m38RlVWetgiIG40_058dtO1oy9DMYjKvNd1FYtHS7w5R1PTh9pcz_Tgn_VnAQ90yzD9c_SfBWiSli92R6ja_fCuylRVHhQ5fFGlPinW945qTYEPdT0SucUhD3umQpGTFJU-ZrMHEyYZwSd6eQLRfiPagUKV98oOAYPnGzKbGeE5wWwT8Ko7rWgx_7o5YlsVw-1H2Rs5qxjj6ZjLWEBohYgfc2v8cnhBTpmnlNlJUXvaNJlJT9K_EwdF3d0LV-7n_GkmBxs866Baqrtas6OYnPDGzcksazvpJyvEhL6c3Lj9maeCMvuBJWqZ0c3Ypeyp3Y0PRb8s9blRthhipU1oxT_JS0

...