Skip to content

Commit

Permalink
fixed expressions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hui committed Dec 20, 2024
1 parent 34b3e71 commit c5678fe
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 149 deletions.
12 changes: 9 additions & 3 deletions packages/firestore/src/core/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ abstract class BigIntOrDoubleArithmetics<
right: {
integerValue: number | string;
}
): bigint | undefined;
): bigint | number | undefined;
abstract doubleArith(
left:
| { doubleValue: number | string }
Expand Down Expand Up @@ -389,8 +389,11 @@ abstract class BigIntOrDoubleArithmetics<
return undefined;
}

if (typeof result === 'number') {
return { doubleValue: result };
}
// Check for overflow
if (result < LongMinValue || result > LongMaxValue) {
else if (result < LongMinValue || result > LongMaxValue) {
return undefined; // Simulate overflow error
} else {
return { integerValue: `${result}` };
Expand Down Expand Up @@ -540,10 +543,13 @@ export class CoreDivide extends BigIntOrDoubleArithmetics<Divide> {
right: {
integerValue: number | string;
}
): bigint | undefined {
): bigint | number | undefined {
const rightValue = asBigInt(right);
if (rightValue === BigInt(0)) {
return undefined;
// return isNegativeZero(asDouble(right))
// ? Number.NEGATIVE_INFINITY
// : Number.POSITIVE_INFINITY;
}
return asBigInt(left) / rightValue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/lite-api/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ export class Constant extends Expr {
// TODO how should we treat the value of `undefined`?
this._protoValue = parseData(null, context)!;
} else {
this._protoValue = parseData(this.value, context)!;
this._protoValue = parseData(this.value, context, this.options)!;
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/firestore/src/lite-api/user_data_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,8 @@ export function parseQueryValue(
*/
export function parseData(
input: unknown,
context: ParseContextImpl
context: ParseContextImpl,
options?: { preferIntegers: boolean }
): ProtoValue | null {
// Unwrap the API type from the Compat SDK. This will return the API type
// from firestore-exp.
Expand Down Expand Up @@ -779,7 +780,7 @@ export function parseData(
}
return parseArray(input as unknown[], context);
} else {
return parseScalarValue(input, context);
return parseScalarValue(input, context, options);
}
}
}
Expand Down Expand Up @@ -860,14 +861,15 @@ function parseSentinelFieldValue(
*/
export function parseScalarValue(
value: unknown,
context: ParseContextImpl
context: ParseContextImpl,
options?: { preferIntegers: boolean }
): ProtoValue | null {
value = getModularInstance(value);

if (value === null) {
return { nullValue: 'NULL_VALUE' };
} else if (typeof value === 'number') {
return toNumber(context.serializer, value);
return toNumber(context.serializer, value, options);
} else if (typeof value === 'boolean') {
return { booleanValue: value };
} else if (typeof value === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/model/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function valueEquals(
);
case TypeOrder.VectorValue:
case TypeOrder.ObjectValue:
return objectEquals(left, right);
return objectEquals(left, right, options);
case TypeOrder.MaxValue:
return true;
default:
Expand Down
9 changes: 8 additions & 1 deletion packages/firestore/src/remote/number_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export function toInteger(value: number): ProtoValue {
* The return value is an IntegerValue if it can safely represent the value,
* otherwise a DoubleValue is returned.
*/
export function toNumber(serializer: Serializer, value: number): ProtoValue {
export function toNumber(
serializer: Serializer,
value: number,
options?: { preferIntegers: boolean }
): ProtoValue {
if (Number.isInteger(value) && options?.preferIntegers) {
return toInteger(value);
}
return isSafeInteger(value) ? toInteger(value) : toDouble(serializer, value);
}
Loading

0 comments on commit c5678fe

Please sign in to comment.