2020package io .wcm .devops .conga .plugins .aem .handlebars .helper ;
2121
2222import java .io .IOException ;
23- import java .util .HashMap ;
2423import java .util .List ;
2524import java .util .Map ;
2625import java .util .stream .Collectors ;
3029import com .github .jknack .handlebars .Context ;
3130import com .github .jknack .handlebars .Options ;
3231import com .github .jknack .handlebars .Options .Buffer ;
32+ import com .google .common .collect .ImmutableList ;
3333import com .google .common .collect .ImmutableMap ;
3434
3535import io .wcm .devops .conga .generator .spi .handlebars .HelperPlugin ;
@@ -45,7 +45,7 @@ abstract class AbstractCloudManagerConditionalHelper implements HelperPlugin<Obj
4545
4646 static final String HTTPD_KEY = "httpd" ;
4747 static final String CLOUD_MANAGER_CONDITIONAL_KEY = "cloudManagerConditional" ;
48- static final String TARGET_ENVIRONMENT_KEY = "targetEnvironment" ;
48+ static final List < String > ENVIRONMENTS = ImmutableList . of ( "dev" , "stage" , "prod" ) ;
4949
5050 @ Override
5151 @ SuppressWarnings ("unchecked" )
@@ -60,13 +60,13 @@ public final Object apply(Object context, Options options, HelperContext pluginC
6060 // get tenants from context
6161 Object cloudManagerConditional = currentContext .get (HTTPD_KEY + "." + CLOUD_MANAGER_CONDITIONAL_KEY );
6262
63- if (!(cloudManagerConditional instanceof List )) {
63+ if (!(cloudManagerConditional instanceof Map )) {
6464 // no conditional statement - just render the body
6565 buffer .append (options .fn (currentContext ));
6666 }
6767 else {
6868 // render body for each environment in conditional block with a merged context
69- List <CloudManagerConditional > items = getCloudManagerConditional ((List < Map <String , Object > >)cloudManagerConditional );
69+ List <CloudManagerConditional > items = getCloudManagerConditional ((Map <String , Object >)cloudManagerConditional );
7070 for (CloudManagerConditional item : items ) {
7171
7272 // config inside httpd.cloudManagerConditional is considered to be prefixed with "httpd.", so wrap it around here for merging
@@ -94,10 +94,9 @@ public final Object apply(Object context, Options options, HelperContext pluginC
9494 protected abstract void renderBodyContent (Buffer buffer , CharSequence bodyContent ,
9595 String targetEnvironment ) throws IOException ;
9696
97- private List <CloudManagerConditional > getCloudManagerConditional (List <Map <String , Object >> configs ) {
98- return configs .stream ()
99- .map (config -> new CloudManagerConditional (config ))
100- .filter (item -> item .getTargetEnvironment () != null )
97+ private List <CloudManagerConditional > getCloudManagerConditional (Map <String , Object > cloudManagerConditional ) {
98+ return ENVIRONMENTS .stream ()
99+ .map (env -> new CloudManagerConditional (env , cloudManagerConditional .getOrDefault (env , ImmutableMap .of ())))
101100 .collect (Collectors .toList ());
102101 }
103102
@@ -109,16 +108,15 @@ private static class CloudManagerConditional {
109108 private final Map <String , Object > config ;
110109 private final String targetEnvironment ;
111110
112- CloudManagerConditional ( Map < String , Object > config ) {
113- this . config = new HashMap <>( config );
114- Object value = this . config . get ( AbstractCloudManagerConditionalHelper . TARGET_ENVIRONMENT_KEY ) ;
115- if (value instanceof String ) {
116- this .targetEnvironment = (String )value ;
111+ @ SuppressWarnings ( "unchecked" )
112+ CloudManagerConditional ( String targetEnvironment , Object value ) {
113+ this . targetEnvironment = targetEnvironment ;
114+ if (value instanceof Map ) {
115+ this .config = (Map < String , Object > )value ;
117116 }
118117 else {
119- this .targetEnvironment = null ;
118+ this .config = ImmutableMap . of () ;
120119 }
121- this .config .remove (AbstractCloudManagerConditionalHelper .TARGET_ENVIRONMENT_KEY );
122120 }
123121
124122 public String getTargetEnvironment () {
0 commit comments