Java Code Examples for com.hubspot.jinjava.interpret.JinjavaInterpreter#resolveProperty()

The following examples show how to use com.hubspot.jinjava.interpret.JinjavaInterpreter#resolveProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortFilter.java    From jinjava with Apache License 2.0 6 votes vote down vote up
private Object mapObject(
  JinjavaInterpreter interpreter,
  Object o,
  List<String> propertyChain
) {
  if (o == null) {
    throw new InvalidInputException(interpreter, this, InvalidReason.NULL_IN_LIST);
  }

  if (propertyChain.isEmpty()) {
    return o;
  }

  Object result = interpreter.resolveProperty(o, propertyChain);
  if (result == null) {
    throw new InvalidArgumentException(
      interpreter,
      this,
      InvalidReason.NULL_ATTRIBUTE_IN_LIST,
      2,
      DOT_JOINER.join(propertyChain)
    );
  }
  return result;
}
 
Example 2
Source File: UniqueFilter.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
  Map<Object, Object> result = new LinkedHashMap<>();
  String attr = null;

  if (args.length > 0) {
    attr = args[0];
  }

  ForLoop loop = ObjectIterator.getLoop(var);
  while (loop.hasNext()) {
    Object val = loop.next();
    Object key = val;

    if (attr != null) {
      key = interpreter.resolveProperty(val, attr);
    }

    if (!result.containsKey(key)) {
      result.put(key, val);
    }
  }

  return result.values();
}
 
Example 3
Source File: AttrFilter.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
  if (args.length < 1) {
    throw new TemplateSyntaxException(
      interpreter,
      getName(),
      "requires 1 argument (attribute name to use)"
    );
  }

  return interpreter.resolveProperty(var, args[0]);
}
 
Example 4
Source File: JoinFilter.java    From jinjava with Apache License 2.0 4 votes vote down vote up
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
  LengthLimitingStringBuilder stringBuilder = new LengthLimitingStringBuilder(
    interpreter.getConfig().getMaxStringLength()
  );

  String separator = "";
  if (args.length > 0) {
    separator = args[0];
  }

  String attr = null;
  if (args.length > 1) {
    attr = args[1];
  }

  ForLoop loop = ObjectIterator.getLoop(var);
  boolean first = true;
  while (loop.hasNext()) {
    Object val = loop.next();

    if (attr != null) {
      val = interpreter.resolveProperty(val, attr);
    }

    try {
      if (!first) {
        stringBuilder.append(separator);
      } else {
        first = false;
      }
      stringBuilder.append(Objects.toString(val, ""));
    } catch (OutputTooBigException ex) {
      interpreter.addError(
        new TemplateError(
          ErrorType.WARNING,
          ErrorReason.OTHER,
          ErrorItem.FILTER,
          String.format(
            "Result of %s filter has been truncated to the max String length of %d",
            getName(),
            interpreter.getConfig().getMaxStringLength()
          ),
          null,
          interpreter.getLineNumber(),
          interpreter.getPosition(),
          ex
        )
      );

      return stringBuilder.toString();
    }
  }

  return stringBuilder.toString();
}