Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit f61c61f

Browse files
authored
feat: add support for remaining map pipeline expressions (#2321)
1 parent f9439c9 commit f61c61f

3 files changed

Lines changed: 504 additions & 0 deletions

File tree

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expression.java

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,184 @@ public static Expression mapRemove(String mapField, String key) {
19941994
return mapRemove(field(mapField), key);
19951995
}
19961996

1997+
/**
1998+
* Creates an expression that returns a new map with the specified entries added or updated.
1999+
*
2000+
* <ul>
2001+
* <li>Only performs shallow updates to the map.
2002+
* <li>Setting a value to {@code null} will retain the key with a {@code null} value. To remove
2003+
* a key entirely, use {@code mapRemove}.
2004+
* </ul>
2005+
*
2006+
* @param mapExpr The expression representing the map.
2007+
* @param key The key to set. Must be an expression representing a string.
2008+
* @param value The value to set.
2009+
* @param moreKeyValues Additional key-value pairs to set.
2010+
* @return A new {@link Expression} representing the map with the entries set.
2011+
*/
2012+
@BetaApi
2013+
public static Expression mapSet(
2014+
Expression mapExpr, Expression key, Expression value, Expression... moreKeyValues) {
2015+
ImmutableList.Builder<Expression> builder = ImmutableList.builder();
2016+
builder.add(mapExpr);
2017+
builder.add(key);
2018+
builder.add(value);
2019+
builder.add(moreKeyValues);
2020+
return new FunctionExpression("map_set", builder.build());
2021+
}
2022+
2023+
/**
2024+
* Creates an expression that returns a new map with the specified entries added or updated.
2025+
*
2026+
* <ul>
2027+
* <li>Only performs shallow updates to the map.
2028+
* <li>Setting a value to {@code null} will retain the key with a {@code null} value. To remove
2029+
* a key entirely, use {@code mapRemove}.
2030+
* </ul>
2031+
*
2032+
* @param mapExpr The map field to set entries in.
2033+
* @param key The key to set.
2034+
* @param value The value to set.
2035+
* @param moreKeyValues Additional key-value pairs to set.
2036+
* @return A new {@link Expression} representing the map with the entries set.
2037+
*/
2038+
@BetaApi
2039+
public static Expression mapSet(
2040+
Expression mapExpr, String key, Object value, Object... moreKeyValues) {
2041+
return mapSet(
2042+
mapExpr,
2043+
constant(key),
2044+
toExprOrConstant(value),
2045+
toArrayOfExprOrConstant(moreKeyValues).toArray(new Expression[0]));
2046+
}
2047+
2048+
/**
2049+
* Creates an expression that returns a new map with the specified entries added or updated.
2050+
*
2051+
* <ul>
2052+
* <li>Only performs shallow updates to the map.
2053+
* <li>Setting a value to {@code null} will retain the key with a {@code null} value. To remove
2054+
* a key entirely, use {@code mapRemove}.
2055+
* </ul>
2056+
*
2057+
* @param mapField The map field to set entries in.
2058+
* @param key The key to set. Must be an expression representing a string.
2059+
* @param value The value to set.
2060+
* @param moreKeyValues Additional key-value pairs to set.
2061+
* @return A new {@link Expression} representing the map with the entries set.
2062+
*/
2063+
@BetaApi
2064+
public static Expression mapSet(
2065+
String mapField, Expression key, Expression value, Expression... moreKeyValues) {
2066+
return mapSet(field(mapField), key, value, moreKeyValues);
2067+
}
2068+
2069+
/**
2070+
* Creates an expression that returns a new map with the specified entries added or updated.
2071+
*
2072+
* <ul>
2073+
* <li>Only performs shallow updates to the map.
2074+
* <li>Setting a value to {@code null} will retain the key with a {@code null} value. To remove
2075+
* a key entirely, use {@code mapRemove}.
2076+
* </ul>
2077+
*
2078+
* @param mapField The map field to set entries in.
2079+
* @param key The key to set. Must be an expression representing a string.
2080+
* @param value The value to set.
2081+
* @param moreKeyValues Additional key-value pairs to set.
2082+
* @return A new {@link Expression} representing the map with the entries set.
2083+
*/
2084+
@BetaApi
2085+
public static Expression mapSet(
2086+
String mapField, String key, Object value, Object... moreKeyValues) {
2087+
return mapSet(field(mapField), key, value, moreKeyValues);
2088+
}
2089+
2090+
/**
2091+
* Creates an expression that returns the keys of a map.
2092+
*
2093+
* <p>While the backend generally preserves insertion order, relying on the order of the output
2094+
* array is not guaranteed and should be avoided.
2095+
*
2096+
* @param mapExpr The expression representing the map to get the keys of.
2097+
* @return A new {@link Expression} representing the keys of the map.
2098+
*/
2099+
@BetaApi
2100+
public static Expression mapKeys(Expression mapExpr) {
2101+
return new FunctionExpression("map_keys", ImmutableList.of(mapExpr));
2102+
}
2103+
2104+
/**
2105+
* Creates an expression that returns the keys of a map.
2106+
*
2107+
* <p>While the backend generally preserves insertion order, relying on the order of the output
2108+
* array is not guaranteed and should be avoided.
2109+
*
2110+
* @param mapField The map field to get the keys of.
2111+
* @return A new {@link Expression} representing the keys of the map.
2112+
*/
2113+
@BetaApi
2114+
public static Expression mapKeys(String mapField) {
2115+
return mapKeys(field(mapField));
2116+
}
2117+
2118+
/**
2119+
* Creates an expression that returns the values of a map.
2120+
*
2121+
* <p>While the backend generally preserves insertion order, relying on the order of the output
2122+
* array is not guaranteed and should be avoided.
2123+
*
2124+
* @param mapExpr The expression representing the map to get the values of.
2125+
* @return A new {@link Expression} representing the values of the map.
2126+
*/
2127+
@BetaApi
2128+
public static Expression mapValues(Expression mapExpr) {
2129+
return new FunctionExpression("map_values", ImmutableList.of(mapExpr));
2130+
}
2131+
2132+
/**
2133+
* Creates an expression that returns the values of a map.
2134+
*
2135+
* <p>While the backend generally preserves insertion order, relying on the order of the output
2136+
* array is not guaranteed and should be avoided.
2137+
*
2138+
* @param mapField The map field to get the values of.
2139+
* @return A new {@link Expression} representing the values of the map.
2140+
*/
2141+
@BetaApi
2142+
public static Expression mapValues(String mapField) {
2143+
return mapValues(field(mapField));
2144+
}
2145+
2146+
/**
2147+
* Creates an expression that returns the entries of a map as an array of maps, where each map
2148+
* contains a "k" property for the key and a "v" property for the value.
2149+
*
2150+
* <p>While the backend generally preserves insertion order, relying on the order of the output
2151+
* array is not guaranteed and should be avoided.
2152+
*
2153+
* @param mapExpr The expression representing the map to get the entries of.
2154+
* @return A new {@link Expression} representing the entries of the map.
2155+
*/
2156+
@BetaApi
2157+
public static Expression mapEntries(Expression mapExpr) {
2158+
return new FunctionExpression("map_entries", ImmutableList.of(mapExpr));
2159+
}
2160+
2161+
/**
2162+
* Creates an expression that returns the entries of a map as an array of maps.
2163+
*
2164+
* <p>While the backend generally preserves insertion order, relying on the order of the output
2165+
* array is not guaranteed and should be avoided.
2166+
*
2167+
* @param mapField The map field to get the entries of.
2168+
* @return A new {@link Expression} representing the entries of the map.
2169+
*/
2170+
@BetaApi
2171+
public static Expression mapEntries(String mapField) {
2172+
return mapEntries(field(mapField));
2173+
}
2174+
19972175
/**
19982176
* Creates an expression that reverses a string, blob, or array.
19992177
*
@@ -5077,6 +5255,80 @@ public final Expression mapRemove(String key) {
50775255
return mapRemove(this, key);
50785256
}
50795257

5258+
/**
5259+
* Creates an expression that returns a new map with the specified entries added or updated.
5260+
*
5261+
* <p>Note that {@code mapSet} only performs shallow updates to the map. Setting a value to {@code
5262+
* null} will retain the key with a {@code null} value. To remove a key entirely, use {@code
5263+
* mapRemove}.
5264+
*
5265+
* @param key The key to set.
5266+
* @param value The value to set.
5267+
* @param moreKeyValues Additional key-value pairs to set.
5268+
* @return A new {@link Expression} representing the map with the entries set.
5269+
*/
5270+
@BetaApi
5271+
public final Expression mapSet(Expression key, Expression value, Expression... moreKeyValues) {
5272+
return mapSet(this, key, value, moreKeyValues);
5273+
}
5274+
5275+
/**
5276+
* Creates an expression that returns a new map with the specified entries added or updated.
5277+
*
5278+
* @param key The key to set.
5279+
* @param value The value to set.
5280+
* @param moreKeyValues Additional key-value pairs to set.
5281+
* @return A new {@link Expression} representing the map with the entries set.
5282+
*/
5283+
@BetaApi
5284+
public final Expression mapSet(String key, Object value, Object... moreKeyValues) {
5285+
return mapSet(
5286+
this,
5287+
constant(key),
5288+
toExprOrConstant(value),
5289+
toArrayOfExprOrConstant(moreKeyValues).toArray(new Expression[0]));
5290+
}
5291+
5292+
/**
5293+
* Creates an expression that returns the keys of this map expression.
5294+
*
5295+
* <p>While the backend generally preserves insertion order, relying on the order of the output
5296+
* array is not guaranteed and should be avoided.
5297+
*
5298+
* @return A new {@link Expression} representing the keys of the map.
5299+
*/
5300+
@BetaApi
5301+
public final Expression mapKeys() {
5302+
return mapKeys(this);
5303+
}
5304+
5305+
/**
5306+
* Creates an expression that returns the values of this map expression.
5307+
*
5308+
* <p>While the backend generally preserves insertion order, relying on the order of the output
5309+
* array is not guaranteed and should be avoided.
5310+
*
5311+
* @return A new {@link Expression} representing the values of the map.
5312+
*/
5313+
@BetaApi
5314+
public final Expression mapValues() {
5315+
return mapValues(this);
5316+
}
5317+
5318+
/**
5319+
* Creates an expression that returns the entries of this map expression as an array of maps,
5320+
* where each map contains a "k" property for the key and a "v" property for the value.
5321+
*
5322+
* <p>While the backend generally preserves insertion order, relying on the order of the output
5323+
* array is not guaranteed and should be avoided.
5324+
*
5325+
* @return A new {@link Expression} representing the entries of the map.
5326+
*/
5327+
@BetaApi
5328+
public final Expression mapEntries() {
5329+
return mapEntries(this);
5330+
}
5331+
50805332
/**
50815333
* Creates an expression that reverses this expression, which must be a string, blob, or array.
50825334
*

0 commit comments

Comments
 (0)