@Override public int getItemCnt() { return length(); }
@Override public int getItemCnt() { return length(); }
@Override public boolean hasItem(SoyValue key) { int index = getIntegerIndex(key); return 0 <= index && index < length(); }
@Override public boolean hasItem(SoyValue key) { int index = getIntegerIndex(key); return 0 <= index && index < length(); }
@Override @Nonnull public Iterable<? extends SoyValue> getItemKeys() { ImmutableList.Builder<IntegerData> indexesBuilder = ImmutableList.builder(); for (int i = 0, n = length(); i < n; i++) { indexesBuilder.add(IntegerData.forValue(i)); } return indexesBuilder.build(); }
@Override public SoyData compute(List<SoyData> args) { SoyData arg = args.get(0); if (!(arg instanceof SoyListData)) { throw new IllegalArgumentException("Argument to length() function is not SoyListData."); } return toSoyData(((SoyListData) arg).length()); }
@Override @Nonnull public Iterable<? extends SoyValue> getItemKeys() { ImmutableList.Builder<IntegerData> indexesBuilder = ImmutableList.builder(); for (int i = 0, n = length(); i < n; i++) { indexesBuilder.add(IntegerData.forValue(i)); } return indexesBuilder.build(); }
@Override protected void visitForeachNode(ForeachNode node) { SoyData dataRefValue = eval(node.getExpr()); if (!(dataRefValue instanceof SoyListData)) { throw new RenderException( "In 'foreach' command " + node.toSourceString() + ", the data reference does not resolve to a SoyListData."); } SoyListData foreachList = (SoyListData) dataRefValue; if (foreachList.length() > 0) { // Case 1: Nonempty list. String varName = node.getVarName(); Map<String, SoyData> newEnvFrame = Maps.newHashMap(); // Note: No need to save firstIndex as it's always 0. newEnvFrame.put(varName + "__lastIndex", IntegerData.forValue(foreachList.length() - 1)); env.push(newEnvFrame); for (int i = 0; i < foreachList.length(); ++i) { newEnvFrame.put(varName + "__index", IntegerData.forValue(i)); newEnvFrame.put(varName, foreachList.get(i)); visitChildren((ForeachNonemptyNode) node.getChild(0)); } env.pop(); } else { // Case 2: Empty list. If the 'ifempty' node exists, visit it. if (node.numChildren() == 2) { visit(node.getChild(1)); } } }