Skip to content

Commit

Permalink
feat(pipelinetemplates): Adding withMapKey handlebars helper (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert committed Mar 8, 2017
1 parent d6f3737 commit 0635afd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.helper.JsonHelper;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.helper.ModuleHelper;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.helper.UnknownIdentifierHelper;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.helper.WithMapKeyHelper;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -55,6 +56,7 @@ public HandlebarsRenderer(ObjectMapper pipelineTemplateObjectMapper) {
.registerHelperMissing(new UnknownIdentifierHelper())
.registerHelper("json", new JsonHelper(pipelineTemplateObjectMapper))
.registerHelper("module", new ModuleHelper(this, pipelineTemplateObjectMapper))
.registerHelper("withMapKey", new WithMapKeyHelper())
;
ConditionHelper.register(handlebars);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.helper;

import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.Options.Buffer;

import java.io.IOException;
import java.util.Map;

import static org.apache.commons.lang3.Validate.notNull;

public class WithMapKeyHelper implements Helper<Map<String, Object>> {

@Override
public Object apply(Map<String, Object> context, Options options) throws IOException {
notNull(context, "Map value must not be null");

String key = options.param(0);
notNull(key, "A key is required");

if (!context.containsKey(key)) {
throw new IllegalArgumentException("withObjectKey helper given key that does not exist (key: " + key + ")");
}

Object val = context.get(key);

Buffer buffer = options.buffer();
if (options.isFalsy(val)) {
buffer.append(options.inverse(val));
} else {
buffer.append(options.fn(val));
}
return buffer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.helper

import com.github.jknack.handlebars.EscapingStrategy
import com.github.jknack.handlebars.Handlebars
import com.github.jknack.handlebars.Template
import spock.lang.Specification

class WithMapKeyHelperSpec extends Specification {

Handlebars handlebars = new Handlebars()
.with(EscapingStrategy.NOOP)

def setup() {
handlebars.registerHelper("withMapKey", new WithMapKeyHelper())
}

def 'should render nested map'() {
given:
Map context = [
'stringVar': 'key1',
'mapVar': [key1: 'value1', key2: 'value2']
]

and:
def template = '{{#withMapKey mapVar stringVar}}{{this}}{{/withMapKey}}'

expect:
compile(template, context) == 'value1'
}

String compile(String template, Object context) {
Template t = handlebars.compileInline(template)
return t.apply(context)
}

}

0 comments on commit 0635afd

Please sign in to comment.