Skip to content

Support -0.0 in Firestore #2751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 17, 2020
Merged

Support -0.0 in Firestore #2751

merged 6 commits into from
Mar 17, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

We have a couple of unit tests that verify that we treat DoubleValue -0.0 distinct from 0.0, but we were never storing -0.0 as a DoubleValue.

We have a couple of unit tests that verify that we treat DoubleValue -0.0 distinct from 0.0, but we were never storing -0.0 as a DoubleValue.
/**
* Safely checks if the number is NaN.
*/
export function safeIsNaN(value: unknown): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this function was never used.

@schmidt-sebastian
Copy link
Contributor Author

This doesn't seem to work with persistence. I will take a look.

@schmidt-sebastian
Copy link
Contributor Author

So it turns out that JSON serialization does not round-trip -0:

const obj = { d: -0 }
JSON.parse(JSON.stringify(obj)) => {d: 0}

I instead opted to use a string constant for -0, which seems to work (even with the backend).

Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -46,6 +46,12 @@ function customDeepEqual(left: unknown, right: unknown): boolean {
/**
* END: Custom compare logic
*/
if (left === 0.0 && right === 0.0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably move this inside the left === right case.

if (left === right) {
  if (left === 0) {
    // ...
    return 1 / left === 1 / right;
  } else {
    return true;
  }
}

Also, the comment above about "END: Custom compare logic" I can't say I really understand what it means. It seems like there's custom logic both above and below the comment. Could you strike it or make it clear what it's actually talking about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I was debating doing this earlier anyways.

@@ -58,25 +58,22 @@ export function isNullOrUndefined(value: unknown): boolean {
return value === null || value === undefined;
}

/** Returns whether the value represents -0.. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra period.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@@ -47,6 +47,12 @@ apiDescribe('Firestore', (persistence: boolean) => {
});
});

it('can read and write number fields', () => {
return withTestDb(persistence, db => {
return expectRoundtrip(db, { a: 1, b: NaN, c: Infinity, d: -0.0 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually make any assertion about the format of the d value. Is it worth separately asserting that in the forward direction -0.0 renders as a string '-0'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customDeepEqual actually does assert that d is -0 with my changes below. If I just make those changes, this test fails.

@var-const var-const changed the title Support -0.0 in Firstore Support -0.0 in Firestore Mar 17, 2020
@schmidt-sebastian schmidt-sebastian merged commit 4f1f303 into master Mar 17, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/minus0 branch March 17, 2020 18:52
@firebase firebase locked and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants