spullara / spullara/mustache.java

Null handling when iterating String arrays/lists

Open
#202 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
2k
Forks
281
PR merge metrics
No merged PRs in 30d

Description

Given this example:

import java.io.StringReader;
import java.io.StringWriter;

import org.testng.annotations.Test;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;


public class SimpleMustacheTest
{
private static class MyObject
{
	public String[] getNames()
	{
		return new String[] { "Fred", null };
	}
}


@Test
public void simpleTest()
{
	final String template = "{{#names}}{{.}}\n\n{{/names}}";

	final MustacheFactory mustacheFactory = new DefaultMustacheFactory();
	final Mustache mustache = mustacheFactory.compile(new StringReader(template), "test");

	final StringWriter writer = new StringWriter();
	mustache.execute(writer, new MyObject());

	System.out.println(writer.toString());
}
}

Why do I get this?

Fred
SimpleMustacheTest$MyObject@4891a775

Effectively it looks like it's saying that if a value cannot be extracted for an element then I'll take the parent scope. If I'm doing a {{.}} then that parent scope seeking shouldn't happen should it? It's not like anyone would be saying that if I don't have a value then get my parent instead would they?

If this is a design decision then does that mean we have to wrap all objects in true/false checks?

Currently the scenario in the real code I'm forcing the data to always return empty string if the value is a null to overcome this issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example as SimpleMustacheTest using DefaultMustacheFactory, the {{#names}}{{.}} template, and a String[] containing "Fred" and null. Start by tracing compilation and iteration from DefaultMustacheFactory, then verify the completed behavior in the test: the null element should not render the parent object or produce its toString output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.