/** * Convenience method to build a field path from a path string. This is a simple proxy for {@link * DataType#buildFieldPath(String)}. * * @param fieldType The data type of the value to build a path for. * @param fieldPath The path string to parse. * @return The corresponding field path object. */ public static FieldPath newInstance(DataType fieldType, String fieldPath) { return fieldType.buildFieldPath(fieldPath); }
/** * Using the given field path, digs through the document and returns the matching field value. * If the field path resolves to multiple values, returns an ArrayFieldValue containing the * values. */ public FieldValue getRecursiveValue(String path) { return getRecursiveValue(getDataType().buildFieldPath(path)); }
@Override public FieldPath buildFieldPath(String remainFieldName) { if (remainFieldName.length() > 0 && remainFieldName.charAt(0) == '[') { int endPos = remainFieldName.indexOf(']'); if (endPos == -1) { throw new IllegalArgumentException("Array subscript must be closed with ]"); } else { FieldPath path = getNestedType().buildFieldPath(skipDotInString(remainFieldName, endPos)); List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(path.getList()); if (remainFieldName.charAt(1) == '$') { tmpPath.add(0, FieldPathEntry.newVariableLookupEntry(remainFieldName.substring(2, endPos), getNestedType())); } else { tmpPath.add(0, FieldPathEntry.newArrayLookupEntry(Integer.parseInt(remainFieldName.substring(1, endPos)), getNestedType())); } return new FieldPath(tmpPath); } } return getNestedType().buildFieldPath(remainFieldName); }
@Override public FieldPath buildFieldPath(String remainFieldName) { if (remainFieldName.length() == 0) { return new FieldPath(); } String currFieldName = remainFieldName; String subFieldName = ""; for (int i = 0; i < remainFieldName.length(); i++) { if (remainFieldName.charAt(i) == '.') { currFieldName = remainFieldName.substring(0, i); subFieldName = remainFieldName.substring(i + 1); break; } else if (remainFieldName.charAt(i) == '{' || remainFieldName.charAt(i) == '[') { currFieldName = remainFieldName.substring(0, i); subFieldName = remainFieldName.substring(i); break; } } Field f = getField(currFieldName); if (f != null) { FieldPath fieldPath = f.getDataType().buildFieldPath(subFieldName); List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(fieldPath.getList()); tmpPath.add(0, FieldPathEntry.newStructFieldEntry(f)); return new FieldPath(tmpPath); } else { throw new IllegalArgumentException("Field '" + currFieldName + "' not found in type " + this); } }
public static FieldPath buildFieldPath(String remainFieldName, DataType keyType, DataType valueType) { if (remainFieldName.length() > 0 && remainFieldName.charAt(0) == '{') { FieldPathEntry.KeyParseResult result = FieldPathEntry.parseKey(remainFieldName); String keyValue = result.parsed; FieldPath path = valueType.buildFieldPath(skipDotInString(remainFieldName, result.consumedChars - 1)); List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(path.getList()); if (remainFieldName.charAt(1) == '$') { tmpPath.add(0, FieldPathEntry.newVariableLookupEntry(keyValue.substring(1), valueType)); } else { FieldValue fv = keyType.createFieldValue(); fv.assign(keyValue); tmpPath.add(0, FieldPathEntry.newMapLookupEntry(fv, valueType)); } return new FieldPath(tmpPath); } else if (remainFieldName.startsWith("key")) { FieldPath path = keyType.buildFieldPath(skipDotInString(remainFieldName, 2)); List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(path.getList()); tmpPath.add(0, FieldPathEntry.newAllKeysLookupEntry(keyType)); return new FieldPath(tmpPath); } else if (remainFieldName.startsWith("value")) { FieldPath path = valueType.buildFieldPath(skipDotInString(remainFieldName, 4)); List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(path.getList()); tmpPath.add(0, FieldPathEntry.newAllValuesLookupEntry(valueType)); return new FieldPath(tmpPath); } return keyType.buildFieldPath(remainFieldName); }