8000 Fix/mustache recursive templates by klag · Pull Request #537 · jolie/jolie · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,16 @@ public class JolieMustacheObjectHandler implements ObjectHandler {
@Override
public Wrapper find( String name, List< Object > scopes0 ) {
return scopes -> {
for( int i = scopes.size() - 1; i >= 0; i-- ) {
Object scope = scopes.get( i );
if( scope != null ) {
Value value = (Value) scope;
if( value.hasChildren( name ) ) {
return value.getChildren( name );
// ValueVector vec = value.getChildren( name );
// return vec.size() > 1 ? vec : vec.first().valueObject();
}
Object scope = scopes.get( scopes.size() - 1 );
if( scope != null ) {
Value value = (Value) scope;
if( value.hasChildren( name ) ) {
return value.getChildren( name );
// ValueVector vec = value.getChildren( name );
// return vec.size() > 1 ? vec : vec.first().valueObject();
}
}

return null;
};
}
Expand Down Expand Up @@ -87,7 +86,9 @@ public Writer iterate( Iteration iteration, Writer writer, Object object, List<
} else {
Writer w = writer;
for( Value value : vec ) {
scopes.add( value );
w = iteration.next( w, value, scopes );
scopes.remove( scopes.size() - 1 );
}
return w;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
Expand All @@ -33,7 +34,18 @@
@AndJarDeps( "compiler.jar" )
public class MustacheService extends JavaService {
public String render( Value request ) {
final Map< String, Integer > compiledPartialsDepth = new HashMap<>();
final DefaultMustacheFactory mustacheFactory;

final int partialsRecursionLimit;


if( request.hasChildren( "partialsRecursionLimit" ) ) {
partialsRecursionLimit = request.getFirstChild( "partialsRecursionLimit" ).intValue();
} else {
partialsRecursionLimit = 10;
}

if( request.hasChildren( "dir" ) ) {
mustacheFactory = new DefaultMustacheFactory( new File( request.getFirstChild( "dir" ).strValue() ) );
} else if( request.hasChildren( "partials" ) ) {
Expand All @@ -45,22 +57,35 @@ public String render( Value request ) {
partialValue.getFirstChild( "template" ).strValue() ) )
.toArray( Map.Entry[]::new ) );
mustacheFactory = new DefaultMustacheFactory() {
int globalDepthCounter = 0;

@Override
public Mustache compilePartial( String name ) {
String partialTemplate = templateMap.get( name );
if( partialTemplate != null ) {
StringReader reader = new StringReader( partialTemplate );
return compile( reader, name );
} else {
if( !templateMap.containsKey( name ) ) {
throw new IllegalArgumentException( "Partial template '" + name + "' not found in memory" );
}
globalDepthCounter++;
compiledPartialsDepth.computeIfAbsent( name, k -> 0 );
StringReader reader = new StringReader( "" );
if( compiledPartialsDepth.get( name ) <= partialsRecursionLimit
&& globalDepthCounter <= getRecursionLimit() ) {
reader = new StringReader( templateMap.get( name ) );
}
compiledPartialsDepth.put( name, compiledPartialsDepth.get( name ) + 1 );
Mustache mustache = compile( reader, name );
compiledPartialsDepth.put( name, compiledPartialsDepth.get( name ) - 1 );
globalDepthCounter--;
return mustache;
}
};
} else {
mustacheFactory = new DefaultMustacheFactory();
}

mustacheFactory.setObjectHandler( new JolieMustacheObjectHandler() );
if( request.hasChildren( "recursionLimit" ) ) {
mustacheFactory.setRecursionLimit( request.getFirstChild( "recursionLimit" ).intValue() );
}
Mustache mustache = mustacheFactory.compile(
new StringReader( request.getFirstChild( "template" ).strValue() ),
"Jolie" );
Expand Down
4 changes: 4 additions & 0 deletions packages/mustache.ol
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ type RenderRequest {
template:string ///< The mustache template
data:undefined ///< The data for the template
dir?:string ///< The directory in which to look for other templates (for partials)
recursionLimit?: int ///< Maximum limit for recursive calls in partials. Default is 100
partialsRecursionLimit?: int ///< Maximum limit for recursive calls specific for partials. Dafult is 10
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is exactly the difference between recursionLimit and partialsRecursionLimit? Is there another way to do recursion than using partials?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

recursionLimit is the general count of recursive calls. partialsRecursionLimit is the limit in recurisve calls specifically for each partials. In case you have more recursive dependency among partials, you need both for determine the depth of the tree

} | {
template:string ///< The mustache template
data:undefined ///< The data for the template
partials* { ///< List of other templates to be used
name: string
template: string
}
recursionLimit?: int ///< Maximum limit for recursive calls in partials
partialsRecursionLimit?: int ///< Maximum limit for recursive calls specific for partials
}

interface MustacheInterface {
Expand Down
40 changes: 35 additions & 5 deletions test/library/mustache.ol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from ..test-unit import TestUnitInterface
from mustache import Mustache
from console import Console

service Main {
inputPort TestUnitInput {
Expand All @@ -27,6 +28,7 @@ service Main {
}

embed Mustache as mustache
embed Console as Console
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You import and embed console but it's not used. Remove?


main {
test()() {
Expand Down Expand Up @@ -87,8 +89,7 @@ service Main {
) throw( TestFailed, "structure" )

// usage of partials online
if(
render@mustache( {
render = render@mustache( {
template = "<h1>{{title}}</h1>{{> hello }}"
partials << {
name = "hello"
Expand All @@ -98,9 +99,8 @@ service Main {
title = "MyTitle"
name = "Homer"
}
} )
!= "<h1>MyTitle</h1><p>Hello Homer</p>"
) throw( TestFailed, "online partials 1" )
})
if( render != "<h1>MyTitle</h1><p>Hello Homer</p>" ) throw( TestFailed, "online partials 1. found " + render )

if(
render@mustache( {
Expand All @@ -123,6 +123,36 @@ service Main {
} )
!= "<h1>MyTitle</h1><p>Hello Homer</p><hr><p>Item 1</p><p>Item 2</p><p>Item 3</p>"
) throw( TestFailed, "online partials 2" )

render = render@mustache( {
template = "start {{#b}}{{> recursive}}{{/b}}"
partials[0] << {
name = "recursive"
template = "1{{#a}}{{> recursive}}{{/a}}{{a}}"
}
data << {
b.a.a.a.a = 0
}
} )
if( render != "start 111110" ) throw( TestFailed, "online partials 3, found " + render )

render = render@mustache( {
template = "start {{#b}}{{> recursive}}{{> recursive2}}{{/b}}"
partials[0] << {
name = "recursive"
template = "1{{#a}}{{> recursive}}{{/a}}{{a}}"
}
partials[1] << {
name = "recursive2"
template = "{{#c}}2{{> recursive}}{{/c}}"
}
data << {
b.c.a.a.a.a = 0
}
} )
if( render != "start 12111110" ) throw( TestFailed, "online partials 4, found " + render )



}
}
Expand Down
0