Merge pull request #140 from ralfs-gutmanis/master
Add ability to pass CharSequence instead of forcing String for contex…
This commit is contained in:
@@ -9,4 +9,6 @@ package java.io;
|
|||||||
public abstract class Writer
|
public abstract class Writer
|
||||||
{
|
{
|
||||||
public abstract void write (String text) throws IOException;
|
public abstract void write (String text) throws IOException;
|
||||||
|
|
||||||
|
public abstract Writer append (CharSequence text) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,10 +207,25 @@ public class Mustache {
|
|||||||
* then empty strings are considered falsey. If {@link #zeroIsFalse} is true, then zero
|
* then empty strings are considered falsey. If {@link #zeroIsFalse} is true, then zero
|
||||||
* values are considered falsey. */
|
* values are considered falsey. */
|
||||||
public boolean isFalsey (Object value) {
|
public boolean isFalsey (Object value) {
|
||||||
return ((emptyStringIsFalse && "".equals(formatter.format(value))) ||
|
return ((emptyStringIsFalse && isEmptyCharSequence(formatter.format(value))) ||
|
||||||
(zeroIsFalse && (value instanceof Number) && ((Number)value).longValue() == 0));
|
(zeroIsFalse && (value instanceof Number) && ((Number)value).longValue() == 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces "".equals(value). E.g. only not null values with length 0
|
||||||
|
*/
|
||||||
|
private boolean isEmptyCharSequence(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(value instanceof CharSequence) {
|
||||||
|
return ((CharSequence) value).length() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/** Loads and compiles the template {@code name} using this compiler's configured template
|
/** Loads and compiles the template {@code name} using this compiler's configured template
|
||||||
* loader. Note that this does no caching: the caller should cache the loaded template if
|
* loader. Note that this does no caching: the caller should cache the loaded template if
|
||||||
* they expect to use it multiple times.
|
* they expect to use it multiple times.
|
||||||
@@ -259,8 +274,8 @@ public class Mustache {
|
|||||||
/** Handles converting objects to strings when rendering templates. */
|
/** Handles converting objects to strings when rendering templates. */
|
||||||
public interface Formatter {
|
public interface Formatter {
|
||||||
|
|
||||||
/** Converts {@code value} to a string for inclusion in a template. */
|
/** Converts {@code value} to a CharSequence for inclusion in a template. */
|
||||||
String format (Object value);
|
CharSequence format (Object value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handles lambdas. */
|
/** Handles lambdas. */
|
||||||
@@ -299,6 +314,11 @@ public class Mustache {
|
|||||||
|
|
||||||
/** Returns {@code raw} with the appropriate characters replaced with escape sequences. */
|
/** Returns {@code raw} with the appropriate characters replaced with escape sequences. */
|
||||||
String escape (String raw);
|
String escape (String raw);
|
||||||
|
|
||||||
|
/** Returns {@code raw} with the appropriate characters replaced with escape sequences. **/
|
||||||
|
default CharSequence escape (CharSequence raw) {
|
||||||
|
return escape(raw.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handles loading partial templates. */
|
/** Handles loading partial templates. */
|
||||||
|
|||||||
@@ -384,9 +384,9 @@ public class Template {
|
|||||||
|
|
||||||
abstract void visit (Mustache.Visitor visitor);
|
abstract void visit (Mustache.Visitor visitor);
|
||||||
|
|
||||||
protected static void write (Writer out, String data) {
|
protected static void write (Writer out, CharSequence data) {
|
||||||
try {
|
try {
|
||||||
out.write(data);
|
out.append(data);
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
throw new MustacheException(ioe);
|
throw new MustacheException(ioe);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ import java.util.Optional;
|
|||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mustache tests that can only be run on the JVM. Most tests should go in BaseMustacheTest so
|
* Mustache tests that can only be run on the JVM. Most tests should go in BaseMustacheTest so
|
||||||
@@ -54,6 +51,14 @@ public class MustacheTest extends SharedTests
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testCharSequenceVariable() {
|
||||||
|
Map<String, CharSequence> ctx = new HashMap<>();
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
stringBuffer.append("bar");
|
||||||
|
ctx.put("foo", stringBuffer);
|
||||||
|
test("bar", "{{foo}}", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
public interface HasDefault {
|
public interface HasDefault {
|
||||||
default String getFoo () { return "bar"; }
|
default String getFoo () { return "bar"; }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user