Is it possible to throw a JexlException from an JxltTemplate evaluation
when a property exists, but is null? For example, if our context contains this class (using pseudo-code,with accessors omitted for brevity): public class User { private String firstName; private String lastName; } context.put("user1", new User("firstname", "lastname")); context.put("user3", new User(null, "lastname")); and evaulate a JxltTemplate such as: hello ${user1.firstName}, ${user2.firstName} ${user3.firstName} ${user1.firstName} will return "firstname" ${user2.firstName} will throw a Variable exception, as there is no user2 in the context ${user3.firstName} will output an empty string, as introspection will find getLastName() on the class, though invoking it provides a null value. I have been unable to hit on the proper combination of strict/silent/strategy to throw any type of JexlException in the third case, as well, as any strategy comes up with an executor that finds the property accessor, but the value is not handled until later, inInterpreter#visit(ASTReference, Object). A custom resolver that gets the value and throws an exception does work, but doubles the potential gets and seems like a brittle approach. I understand that built-in template functions (empty) or custom functors could also work, but those writing templates would prefer nothing more complicated than dot syntax. Is there a way to accomplish getting an error in this case, or perhaps another tactic I have missed? Thanks, Greg |
I don't see any current way to throw an exception on null evaluation through
configuration. A case can be made to add this as a feature (a hook checking eval result before doPrint in TemplateInterpreter). And about making this use case, you can't workaround using a conditional (${user3.firstName?:error()}) and you definitely must throw an error in the case of null evaluation? Cheers Henrib -- Sent from: http://apache-commons.680414.n4.nabble.com/Commons-User-f735979.html --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
Thank you for the response. A conditional definitely works, as does a
specific namespaced function, e.g. ${val:orError(my.field)} -- if I cannot persuade our template authors of either of these approaches, I will look at working towards building a case and a PR around TemplateInterpreter. On Fri, Aug 30, 2019 at 11:25 AM henrib <[hidden email]> wrote: > I don't see any current way to throw an exception on null evaluation > through > configuration. > A case can be made to add this as a feature (a hook checking eval result > before doPrint in TemplateInterpreter). > And about making this use case, you can't workaround using a conditional > (${user3.firstName?:error()}) and you definitely must throw an error in the > case of null evaluation? > Cheers > Henrib > > > > -- > Sent from: > http://apache-commons.680414.n4.nabble.com/Commons-User-f735979.html > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [hidden email] > For additional commands, e-mail: [hidden email] > > |
Free forum by Nabble | Edit this page |