Skip to content

Commit f84a079

Browse files
committed
Address feedback2
1 parent 3b4ee6a commit f84a079

File tree

3 files changed

+48
-57
lines changed

3 files changed

+48
-57
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/AggregateField.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,28 @@ private AggregateField(@Nullable FieldPath fieldPath, @NonNull String operator)
3535

3636
// Use $operator_$field format if it's an aggregation of a specific field. For example: sum_foo.
3737
// Use $operator format if there's no field. For example: count.
38-
//
39-
// Note: If the fieldPath contains characters that need to be escaped, we need to do further
40-
// processing. Example:
41-
// field: contains`invalid
42-
// field.getEncodedPath(): `contains\`invalid`
43-
// alias: `sum_contains\`invalid`
44-
if (fieldPath == null) {
45-
this.alias = operator;
46-
} else {
47-
String encodedFieldPath = fieldPath.toString();
48-
boolean hasEscapedChars =
49-
encodedFieldPath.startsWith("`")
50-
&& encodedFieldPath.endsWith("`")
51-
&& encodedFieldPath.length() > 1;
52-
String maybeBacktick = hasEscapedChars ? "`" : "";
53-
// Strip away the leading and trailing backticks.
54-
encodedFieldPath =
55-
hasEscapedChars
56-
? encodedFieldPath.substring(1, encodedFieldPath.length() - 1)
57-
: encodedFieldPath;
58-
this.alias = maybeBacktick + operator + "_" + encodedFieldPath + maybeBacktick;
59-
}
38+
this.alias = operator + (fieldPath == null ? "" : "_" + fieldPath);
6039
}
6140

6241
/**
6342
* Returns the field on which the aggregation takes place. Returns an empty string if there's no
6443
* field (e.g. for count).
6544
*/
45+
@RestrictTo(RestrictTo.Scope.LIBRARY)
6646
@NonNull
6747
public String getFieldPath() {
6848
return fieldPath == null ? "" : fieldPath.toString();
6949
}
7050

7151
/** Returns the alias used internally for this aggregate field. */
52+
@RestrictTo(RestrictTo.Scope.LIBRARY)
7253
@NonNull
7354
public String getAlias() {
7455
return alias;
7556
}
7657

7758
/** Returns a string representation of this aggregation's operator. For example: "sum" */
59+
@RestrictTo(RestrictTo.Scope.LIBRARY)
7860
@NonNull
7961
public String getOperator() {
8062
return operator;

firebase-firestore/src/main/java/com/google/firebase/firestore/AggregateQuerySnapshot.java

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,7 @@ public long getCount() {
6767
@RestrictTo(RestrictTo.Scope.LIBRARY)
6868
@Nullable
6969
public Object get(@Nonnull AggregateField aggregateField) {
70-
if (!data.containsKey(aggregateField.getAlias())) {
71-
throw new IllegalArgumentException(
72-
"'"
73-
+ aggregateField.getOperator()
74-
+ "("
75-
+ aggregateField.getFieldPath()
76-
+ ")"
77-
+ "' was not requested in the aggregation query.");
78-
}
79-
Value value = data.get(aggregateField.getAlias());
80-
if (value.hasNullValue()) {
81-
return null;
82-
} else if (value.hasDoubleValue()) {
83-
return value.getDoubleValue();
84-
} else if (value.hasIntegerValue()) {
85-
return value.getIntegerValue();
86-
} else {
87-
throw new IllegalStateException("Found aggregation result that is not an integer nor double");
88-
}
70+
return getInternal(aggregateField);
8971
}
9072

9173
/**
@@ -98,18 +80,12 @@ public Object get(@Nonnull AggregateField aggregateField) {
9880
/** @hide */
9981
@RestrictTo(RestrictTo.Scope.LIBRARY)
10082
public long get(@Nonnull AggregateField.CountAggregateField countAggregateField) {
101-
Object value = get((AggregateField) countAggregateField);
83+
Long value = getLong(countAggregateField);
10284
if (value == null) {
10385
throw new IllegalArgumentException(
10486
"RunAggregationQueryResponse alias " + countAggregateField.getAlias() + " is null");
105-
} else if (!(value instanceof Long)) {
106-
throw new IllegalArgumentException(
107-
"RunAggregationQueryResponse alias "
108-
+ countAggregateField.getAlias()
109-
+ " has incorrect type: "
110-
+ value.getClass().getName());
11187
}
112-
return (long) value;
88+
return value;
11389
}
11490

11591
/**
@@ -126,7 +102,7 @@ public long get(@Nonnull AggregateField.CountAggregateField countAggregateField)
126102
@RestrictTo(RestrictTo.Scope.LIBRARY)
127103
@Nullable
128104
public Double get(@Nonnull AggregateField.AverageAggregateField averageAggregateField) {
129-
return (Double) get((AggregateField) averageAggregateField);
105+
return getDouble(averageAggregateField);
130106
}
131107

132108
/**
@@ -143,8 +119,8 @@ public Double get(@Nonnull AggregateField.AverageAggregateField averageAggregate
143119
@RestrictTo(RestrictTo.Scope.LIBRARY)
144120
@Nullable
145121
public Double getDouble(@Nonnull AggregateField aggregateField) {
146-
Number result = (Number) get(aggregateField);
147-
return result == null ? null : result.doubleValue();
122+
Number val = getTypedValue(aggregateField, Number.class);
123+
return val != null ? val.doubleValue() : null;
148124
}
149125

150126
/**
@@ -160,8 +136,44 @@ public Double getDouble(@Nonnull AggregateField aggregateField) {
160136
@RestrictTo(RestrictTo.Scope.LIBRARY)
161137
@Nullable
162138
public Long getLong(@Nonnull AggregateField aggregateField) {
163-
Number result = (Number) get(aggregateField);
164-
return result == null ? null : result.longValue();
139+
Number val = getTypedValue(aggregateField, Number.class);
140+
return val != null ? val.longValue() : null;
141+
}
142+
143+
@Nullable
144+
private Object getInternal(@Nonnull AggregateField aggregateField) {
145+
if (!data.containsKey(aggregateField.getAlias())) {
146+
throw new IllegalArgumentException(
147+
"'"
148+
+ aggregateField.getOperator()
149+
+ "("
150+
+ aggregateField.getFieldPath()
151+
+ ")"
152+
+ "' was not requested in the aggregation query.");
153+
}
154+
Value value = data.get(aggregateField.getAlias());
155+
UserDataWriter userDataWriter =
156+
new UserDataWriter(
157+
query.getQuery().firestore, DocumentSnapshot.ServerTimestampBehavior.DEFAULT);
158+
return userDataWriter.convertValue(value);
159+
}
160+
161+
@Nullable
162+
private <T> T getTypedValue(@Nonnull AggregateField aggregateField, Class<T> clazz) {
163+
Object value = getInternal(aggregateField);
164+
return castTypedValue(value, aggregateField, clazz);
165+
}
166+
167+
@Nullable
168+
private <T> T castTypedValue(
169+
Object value, @Nonnull AggregateField aggregateField, Class<T> clazz) {
170+
if (value == null) {
171+
return null;
172+
} else if (!clazz.isInstance(value)) {
173+
throw new RuntimeException(
174+
"AggregateField '" + aggregateField.getAlias() + "' is not a " + clazz.getName());
175+
}
176+
return clazz.cast(value);
165177
}
166178

167179
/**

firebase-firestore/src/main/java/com/google/firebase/firestore/UserDataWriter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static com.google.firebase.firestore.util.Assert.fail;
3232

3333
import androidx.annotation.RestrictTo;
34-
import androidx.annotation.VisibleForTesting;
3534
import com.google.firebase.Timestamp;
3635
import com.google.firebase.firestore.model.DatabaseId;
3736
import com.google.firebase.firestore.model.DocumentKey;
@@ -53,15 +52,13 @@ public class UserDataWriter {
5352
private final FirebaseFirestore firestore;
5453
private final DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior;
5554

56-
@VisibleForTesting
5755
public UserDataWriter(
5856
FirebaseFirestore firestore,
5957
DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior) {
6058
this.firestore = firestore;
6159
this.serverTimestampBehavior = serverTimestampBehavior;
6260
}
6361

64-
@VisibleForTesting
6562
public Object convertValue(Value value) {
6663
switch (typeOrder(value)) {
6764
case TYPE_ORDER_MAP:

0 commit comments

Comments
 (0)