Package com.google.template.soy.data

Examples of com.google.template.soy.data.SoyData


  // Basics.


  public static SoyData $$getData(SoyData collectionData, String keyStr) {

    SoyData value = ((CollectionData) collectionData).get(keyStr);
    return (value != null) ? value : UndefinedData.INSTANCE;
  }
View Full Code Here



  // TODO: Use this in generated Java code instead of $$getData(), whenever possible.
  public static SoyData $$getDataSingle(SoyData collectionData, String key) {

    SoyData value = ((CollectionData) collectionData).getSingle(key);
    return (value != null) ? value : UndefinedData.INSTANCE;
  }
View Full Code Here

      Object valueObj = entry.getValue();
      PrimitiveData value;
      boolean isValidValue = true;
      try {
        SoyData value0 = SoyData.createFromExistingData(valueObj);
        if (!(value0 instanceof PrimitiveData)) {
          isValidValue = false;
        }
        value = (PrimitiveData) value0;
      } catch (SoyDataException sde) {
View Full Code Here

  private static void addMapDataHelper(
      SoyMapData combinedMapData, String keyPrefix, Map<String, SoyData> map) {

    for (Map.Entry<String, SoyData> entry : map.entrySet()) {
      String key = entry.getKey();
      SoyData value = entry.getValue();
      if (value instanceof SoyMapData) {
        addMapDataHelper(combinedMapData, keyPrefix + key + ".", ((SoyMapData) value).asMap());
      } else if (value instanceof SoyListData) {
        addListDataHelper(combinedMapData, keyPrefix + key + ".", ((SoyListData) value).asList());
      } else {
View Full Code Here

   */
  private static void addListDataHelper(
      SoyMapData combinedMapData, String keyPrefix, List<SoyData> list) {

    for (int i = 0; i < list.size(); ++i) {
      SoyData el = list.get(i);
      if (el instanceof SoyMapData) {
        addMapDataHelper(combinedMapData, keyPrefix + i + ".", ((SoyMapData) el).asMap());
      } else if (el instanceof SoyListData) {
        addListDataHelper(combinedMapData, keyPrefix + i + ".", ((SoyListData) el).asList());
      } else {
View Full Code Here

  }


  @Override public SoyData getSingle(String key) {

    SoyData value = super.getSingle(key);
    if (value != null) {
      return value;
    }
    return baseData.getSingle(key);
  }
View Full Code Here

    // Recurse.
    visitSoyNode(node);

    // If the SwitchNode's expr is not constant, we can't simplify.
    SoyData switchExprValue = getConstantOrNull(node.getExpr());
    if (switchExprValue == null) {
      return// cannot simplify this node
    }

    // For each SwitchCaseNode child:
    // (a) If the case has a constant expr that matches: Replace the child with a SwitchDefaultNode
    //     and remove all children after it, if any. Can stop processing after doing this, because
    //     the new SwitchDefaultNode is now the last child.
    // (b) If the case has all constant exprs and none match: Remove the child.
    for (SoyNode child : Lists.newArrayList(node.getChildren()) /*copy*/) {
      if (child instanceof SwitchCaseNode) {
        SwitchCaseNode caseNode = (SwitchCaseNode) child;

        boolean hasMatchingConstant = false;
        boolean hasAllNonmatchingConstants = true;
        for (ExprRootNode<?> caseExpr : caseNode.getExprList()) {
          SoyData caseExprValue = getConstantOrNull(caseExpr);
          if (caseExprValue == null) {
            hasAllNonmatchingConstants = false;
          } else if (caseExprValue.equals(switchExprValue)) {
            hasMatchingConstant = true;
            hasAllNonmatchingConstants = false;
            break;
          }
        }
View Full Code Here

  @Override protected SoyData visitMapLiteralNode(MapLiteralNode node) {

    Map<String, SoyData> map = Maps.newHashMap();

    for (int i = 0, n = node.numChildren(); i < n; i += 2) {
      SoyData key = visit(node.getChild(i));
      if (! (key instanceof StringData)) {
        throw new RenderException(
            "Maps must have string keys (key \"" + node.getChild(i).toSourceString() + "\"" +
            " in map " + node.toSourceString() + " does not evaluate to a string).");
      }
      SoyData value = visit(node.getChild(i + 1));
      map.put(key.stringValue(), value);
    }

    return new SoyMapData(map);
  }
View Full Code Here


  @Override protected SoyData visitDataRefNode(DataRefNode node) {

    // First resolve the first key, which may reference a variable, data, or injected data.
    SoyData value0 = resolveDataRefFirstKey(node);

    // Case 1: There is only one key. We already have the final value of the data reference.
    if (node.numChildren() == 0) {
      return value0;
    }

    // Case 2: There are more keys.
    SoyData value = value0;
    for (ExprNode child : node.getChildren()) {
      DataRefAccessNode accessNode = (DataRefAccessNode) child;

      // We expect 'value' to be a CollectionData during every iteration.
      if (! (value instanceof CollectionData)) {
        if (accessNode.isNullSafe()) {
          if (value == null || value instanceof UndefinedData || value instanceof NullData) {
            return NullData.INSTANCE;
          } else {
            throw new RenderException(
                "While evaluating \"" + node.toSourceString() + "\", encountered non-collection" +
                " just before accessing \"" + accessNode.toSourceString() + "\".");
          }
        } else {
          // This behavior is not ideal, but needed for compatibility with existing code.
          return UndefinedData.INSTANCE;
          // TODO: If feasible, find and fix existing instances, then enable this exception.
          //if (value == null || value instanceof UndefinedData) {
          //  throw new RenderException(
          //      "While evaluating \"" + node.toSourceString() + "\", encountered undefined LHS" +
          //      " just before accessing \"" + accessNode.toSourceString() + "\".");
          //}
          //value = UndefinedData.INSTANCE;
          //continue;
        }
      }

      // Extract either a string key or integer index from the child access node.
      String key = null;
      int index = -1;
      switch (accessNode.getKind()) {
        case DATA_REF_ACCESS_KEY_NODE:
          key = ((DataRefAccessKeyNode) accessNode).getKey();
          break;
        case DATA_REF_ACCESS_INDEX_NODE:
          index = ((DataRefAccessIndexNode) accessNode).getIndex();
          break;
        case DATA_REF_ACCESS_EXPR_NODE:
          SoyData keyData = visit(accessNode.getChild(0));
          if (keyData instanceof IntegerData) {
            index = ((IntegerData) keyData).getValue();
          } else {
            key = keyData.toString();
          }
          break;
        default:
          throw new AssertionError();
      }
View Full Code Here

  // Implementations for operators.


  @Override protected SoyData visitNegativeOpNode(NegativeOpNode node) {

    SoyData operand = visit(node.getChild(0));
    if (operand instanceof IntegerData) {
      return convertResult( - operand.integerValue() );
    } else {
      return convertResult( - operand.floatValue() );
    }
  }
View Full Code Here

TOP

Related Classes of com.google.template.soy.data.SoyData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.