Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-996] Support includeZero option in IF Helper #1150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -179,6 +179,21 @@ public static class Utils {
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(final Object value) {
return isEmpty(value, false);
}

/**
* Evaluate the given object and return true is the object is considered empty. Nulls, empty
* list or array and false values are considered empty. If includeZero is false, zero values are
* considered empty.
*
* @param value The object value.
* @param includeZero If true, zero values are considered empty.
* @return Return true is the object is considered empty. Nulls, empty list or array and false
* values are considered empty.
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(final Object value, Boolean includeZero) {
if (value == null) {
return true;
}
Expand All @@ -197,7 +212,7 @@ public static boolean isEmpty(final Object value) {
if (value.getClass().isArray()) {
return Array.getLength(value) == 0;
}
if (value instanceof Number) {
if (!includeZero && value instanceof Number) {
return ((Number) value).doubleValue() == 0;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public <T> T hash(final String name, final Object defaultValue) {
* @return False if its argument is false, null or empty list/array (a "falsy" value).
*/
public boolean isFalsy(final Object value) {
return Handlebars.Utils.isEmpty(value);
return Handlebars.Utils.isEmpty(value, (Boolean) this.hash.getOrDefault("includeZero", false));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Handlebars.java: https://github.com/jknack/handlebars.java
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars.jackson;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.context.MapValueResolver;

public class Issue996 extends AbstractTest {

@Override
protected Object configureContext(final Object model) {
return Context.newBuilder(model)
.resolver(MapValueResolver.INSTANCE, JsonNodeValueResolver.INSTANCE)
.build();
}

@Test
public void shouldSupportIncludeZeroOptionIfHelper() throws IOException {
JsonNode tree = new ObjectMapper().readTree("[1, 2, 3]");
shouldCompileTo(
"{{#if 0}}true condition{{else}}false condition{{/if}}", tree, "false condition");
shouldCompileTo(
"{{#if 0 includeZero=true}}true condition{{else}}false condition{{/if}}",
tree,
"true condition");
shouldCompileTo(
"{{#if 0 includeZero=false}}true condition{{else}}false condition{{/if}}",
tree,
"false condition");
}
}