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));
}
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));
}
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));
}
Aggregations