You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Dmitri Blinov (JIRA)" <ji...@apache.org> on 2018/02/06 14:30:00 UTC
[jira] [Comment Edited] (JEXL-250) Safe navigation operator
[ https://issues.apache.org/jira/browse/JEXL-250?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16353749#comment-16353749 ]
Dmitri Blinov edited comment on JEXL-250 at 2/6/18 2:29 PM:
------------------------------------------------------------
The following test does not pass for me
{code:java}
@Test
public void test250() throws Exception {
MapContext ctx = new MapContext();
HashMap<Oject,Object> x = new HashMap<Object, Object> ();
x.put(2, "123456789");
ctx.set("x", x);
JexlEngine engine = new JexlBuilder().strict(true).silent(false).create();
String stmt = "x.2.class.name";
JexlScript script = engine.createScript(stmt);
Object result = script.execute(ctx);
Assert.assertEquals("java.lang.String", result);
try {
stmt = "x.3?.class.name";
script = engine.createScript(stmt);
result = script.execute(ctx);
Assert.assertNull(result);
} catch(JexlException xany) {
Assert.fail("Should have evaluated to null");
}
}
{code}
The test breaks with {{Should have evaluated to null}}
was (Author: dmitri_blinov):
The following test does not pass for me
{code}
@Test
public void test250() throws Exception {
MapContext ctx = new MapContext();
HashMap<Object,Object> x = new HashMap<Object, Object> ();
x.put(2, "123456789");
ctx.set("x", x);
JexlEngine engine = new JexlBuilder().strict(true).silent(false).create();
String stmt = "x.2.class.name";
JexlScript script = engine.createScript(stmt);
Object result = script.execute(ctx);
Assert.assertEquals("java.lang.String", result);
try {
stmt = "x?.3.class.name";
script = engine.createScript(stmt);
result = script.execute(ctx);
Assert.assertNull(result);
} catch(JexlException xany) {
Assert.fail("Should have evaluated to null");
}
}
{code}
The test breaks with {{Should have evaluated to null}}
> Safe navigation operator
> ------------------------
>
> Key: JEXL-250
> URL: https://issues.apache.org/jira/browse/JEXL-250
> Project: Commons JEXL
> Issue Type: New Feature
> Affects Versions: 3.1
> Reporter: Dmitri Blinov
> Assignee: Henri Biestro
> Priority: Minor
> Fix For: 3.2
>
>
> It would be more convenient for a script writer to have a kind of safe navigation operator, in the form of, for example, ({{object?.property),}} for null pointer dereferences. I think its quite a common feature of many scripting (and compiled languages) now. The safe operator should work like basic navigation operator for non null values and should short-circuit the entire expression evaluation after encountering null value.
> I see the difference of new operator from the existing lenient mode evaluation in ability to explicitly define places in an expression where {{null}} deference is allowed, for example the expression {code}Order.Customer?.Name{code} defines that each {{Order}} should have a {{Customer}} and only the {{Name}} of the {{Customer}} is optional.
> The difference of new operator from the existing null coalescing {{??}} operator is in its ability to short-circuit evaluation.
> So, safe navigation operator would be a helpful syntaxic sugar which in my opinion does not overlap with an existing functionality.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)