Search in sources :

Example 1 with SoyData

use of com.google.template.soy.data.SoyData in project closure-templates by google.

the class CollectionData method get.

// ------------ get*() ------------
/**
 * Gets the data at the specified key string.
 *
 * @param keyStr One or more map keys and/or list indices (separated by '.' if multiple parts).
 *     Indicates the path to the location within this data tree.
 * @return The data at the specified key string, or null if there's no data at the location.
 */
public SoyData get(String keyStr) {
    List<String> keys = split(keyStr, '.');
    int numKeys = keys.size();
    CollectionData collectionData = this;
    for (int i = 0; i <= numKeys - 2; ++i) {
        SoyData soyData = collectionData.getSingle(keys.get(i));
        if (!(soyData instanceof CollectionData)) {
            return null;
        }
        collectionData = (CollectionData) soyData;
    }
    return collectionData.getSingle(keys.get(numKeys - 1));
}
Also used : SoyData(com.google.template.soy.data.SoyData)

Example 2 with SoyData

use of com.google.template.soy.data.SoyData in project closure-templates by google.

the class CollectionData method remove.

// ------------ remove() ------------
/**
 * Removes the data at the specified key string.
 *
 * @param keyStr One or more map keys and/or list indices (separated by '.' if multiple parts).
 *     Indicates the path to the location within this data tree.
 */
public void remove(String keyStr) {
    List<String> keys = split(keyStr, '.');
    int numKeys = keys.size();
    CollectionData collectionData = this;
    for (int i = 0; i <= numKeys - 2; ++i) {
        SoyData soyData = collectionData.getSingle(keys.get(i));
        if (!(soyData instanceof CollectionData)) {
            return;
        }
        collectionData = (CollectionData) soyData;
    }
    collectionData.removeSingle(keys.get(numKeys - 1));
}
Also used : SoyData(com.google.template.soy.data.SoyData)

Example 3 with SoyData

use of com.google.template.soy.data.SoyData in project closure-templates by google.

the class CollectionData method put.

/**
 * Puts data into this data tree at the specified key string.
 *
 * @param keyStr One or more map keys and/or list indices (separated by '.' if multiple parts).
 *     Indicates the path to the location within this data tree.
 * @param value The data to put at the specified location.
 */
public void put(String keyStr, SoyData value) {
    List<String> keys = split(keyStr, '.');
    int numKeys = keys.size();
    CollectionData collectionData = this;
    for (int i = 0; i <= numKeys - 2; ++i) {
        SoyData nextSoyData = collectionData.getSingle(keys.get(i));
        if (nextSoyData != null && !(nextSoyData instanceof CollectionData)) {
            throw new SoyDataException("Failed to evaluate key string \"" + keyStr + "\" for put().");
        }
        CollectionData nextCollectionData = (CollectionData) nextSoyData;
        if (nextCollectionData == null) {
            // Create the SoyData object that will be bound to keys.get(i). We need to check the first
            // part of keys[i+1] to know whether to create a SoyMapData or SoyListData (checking the
            // first char is sufficient).
            nextCollectionData = (Character.isDigit(keys.get(i + 1).charAt(0))) ? new SoyListData() : new SoyMapData();
            collectionData.putSingle(keys.get(i), nextCollectionData);
        }
        collectionData = nextCollectionData;
    }
    collectionData.putSingle(keys.get(numKeys - 1), ensureValidValue(value));
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) SoyListData(com.google.template.soy.data.SoyListData) SoyDataException(com.google.template.soy.data.SoyDataException) SoyData(com.google.template.soy.data.SoyData)

Aggregations

SoyData (com.google.template.soy.data.SoyData)3 SoyDataException (com.google.template.soy.data.SoyDataException)1 SoyListData (com.google.template.soy.data.SoyListData)1 SoyMapData (com.google.template.soy.data.SoyMapData)1