#1 2019-12-19 19:38:35

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Issue with passing a JSON string to a DMustache helper

Hello ab,
The helper being used in this form:

{{{helperA {src: "folder1/file1.jpeg", alt: "some text"}}}}

If I understand the document correctly, this part, the JSON string will be passed to helperA by the engine:

  {src: "folder1/file1.jpeg", alt: "some text"}

is supposed to be passed into the TMyClass.HelperA method below:

The helper function registered with TSynMustache.HelperAdd is defined as something like:
procedure TMyClass.HelperA(const Value: variant; out result: variant);
begin
   // here I expect the "Value" parameter contains the JSON string passed to the helper, 
  // but the Delphi debugger shows that the "Value" parameter is null 
end;

However, the debugger shows that the "Value" parameter is null.

Any hint? Thanks.

Last edited by edwinsn (2019-12-20 03:44:15)


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#2 2019-12-20 09:12:15

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Issue with passing a JSON string to a DMustache helper

I fixed the issue, a github PR is coming


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#3 2019-12-20 09:36:36

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Issue with passing a JSON string to a DMustache helper

@ab,

the gihtub PR is here: https://github.com/synopse/mORMot/pull/260
Please review and make changes as needed. Thanks.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2019-12-20 12:47:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,263
Website

Re: Issue with passing a JSON string to a DMustache helper

It could have been better with some associated regression tests...
I will write them.

Offline

#5 2019-12-20 17:28:19

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Issue with passing a JSON string to a DMustache helper

Thank you ab! I think you can write the tests better, and I'll check them once you are done, so that maybe next time my PR will also include some tests.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#6 2019-12-23 12:59:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,263
Website

Re: Issue with passing a JSON string to a DMustache helper

Your implementation didn't properly handle nested objects and such.
I also added some regression tests.

Please check https://synopse.info/fossil/info/f6cd3f162d

Offline

#7 2019-12-24 11:47:32

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Issue with passing a JSON string to a DMustache helper

Thanks for the new implementation, ab, I actually didn't take into account nested JSON objects.

I tested the new version and found an issue that's also exist with my implementation (which I didn't notice until now).

The issue is with tags with 3 brackets, in that case the result will include an extra unwanted '}' at the end of the rendering result.
I guess the following test can reveal the issue.

  helpers := mustache.HelpersGetStandardList(['jsonhelper'], [MustacheHelper]);
  mustache := TSynMustache.Parse('{{{jsonhelper {a:"a",b:10}}}}');
  html := mustache.RenderJSON('', nil, helpers);
  Check(html='a=a,b=10');

Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#8 2019-12-24 18:01:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,263
Website

Re: Issue with passing a JSON string to a DMustache helper

You are right.

Please check https://synopse.info/fossil/info/96a87d13b0

Offline

#9 2019-12-25 03:59:41

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Issue with passing a JSON string to a DMustache helper

I confirm it's fixed now! Happy holidays!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB