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

Wraps params bind exception to allow keep going on. #928

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rogelio-o
Copy link
Contributor

Fixes #920. This is the easier way to solve the problem. The context and all its data is tried to be binded and if it cannot be done the exception is catched and logged with info level (maybe it should be debug level).

Another way to do it is creating a RockerModel, creating a BindableRockerModel from it and then checking if the RockerModel has the "set" method related with the key of the param that is going to be binded.

@pmlopes
Copy link
Member

pmlopes commented May 29, 2018

Alternatively to reduce the amount of exceptions we could make the assumption that keys starting with __ are not expected to be binded?

context.data().forEach((name, value) -> {
  if (name.length() < 3 || (name.charAt(0) != '_' && name.charAt(1) != '_')) {
   bindParam(model, name, value));
  }
});

Something in these lines?! @rogelio-o WDYT?

@rogelio-o
Copy link
Contributor Author

It's a clean approach, but I think that then the implementation of whole system would depend on the template engine.

Another approach could be to use the RockerModel instead of BindableRockerModel like in the code below. The result should be the same as using BindableRockerModel.

RockerModel model = RockerRuntime.getInstance().getBootstrap().model(templatePath);
bindParam(templatePath, model, "context", context);
context.data().forEach((name, value) -> bindParam(templatePath, model, name, value));
private void bindParam(String templatePath, RockerModel model, String name, Object value) {
  Method setter = findModelMethod(model, name);
  if(setter != null) {
    try {
      setter.invoke(model, value);
    } catch (Exception e) {
      throw new TemplateBindException(templatePath, model.getClass().getCanonicalName(), "Unable to set property '" + name + "'", e);
    }
  }
}
  
private Method findModelMethod(RockerModel model, String name) {
  Method result = null;
  Method[] methods = model.getClass().getMethods();
  for (Method method : methods) {
    if (method.getName().equals(name)) {
      Class<?>[] parameterTypes = method.getParameterTypes();
      if (parameterTypes != null && parameterTypes.length == 1) {
        result = method;
      }
    }
  }
    
  return result;
}

@rogelio-o
Copy link
Contributor Author

Refloating this! I've switched the solution by the one proposed above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants