Skip to content

Add toJSON methods to GeoPoint and Timestamp #3615

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 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/firestore/src/api/geo_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class GeoPoint {
return this._lat === other._lat && this._long === other._long;
}

toJSON(): { latitude: number; longitude: number } {
return { latitude: this._lat, longitude: this._long };
}

/**
* Actually private to JS consumers of our API, so this function is prefixed
* with an underscore.
Expand Down
4 changes: 4 additions & 0 deletions packages/firestore/src/api/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export class Timestamp {
);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Strictly speaking, this isn't necessary -- Timestamp would have the same JSON representation without this function. I mostly added it to make it explicit that Timestamp has a well-defined JSON representation -- let me know if you prefer to remove it.

By the way, why is it that GeoPoint uses different names for internal variables and getters while Timestamp doesn't?

Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation in GeoPoint is a little safer: the readonly attribute is Typescript-only, so nothing saves a JavaScript user from directly modifying Timestamp.seconds or Timestamp.nanoseconds. To do the same with a GeoPoint, they would have to use an underscore prefixed variable (which is likely mangled), which would obviously be something we don't support.

Why Timestamp doesn't use this pattern is a question I can't answer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the explanation!

toJSON(): { seconds: number; nanoseconds: number } {
return { seconds: this.seconds, nanoseconds: this.nanoseconds };
}

valueOf(): string {
// This method returns a string of the form <seconds>.<nanoseconds> where <seconds> is
// translated to have a non-negative value and both <seconds> and <nanoseconds> are left-padded
Expand Down
15 changes: 15 additions & 0 deletions packages/firestore/test/unit/api/geo_point.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,19 @@ describe('GeoPoint', () => {
expectNotEqual(new GeoPoint(1, 2), new GeoPoint(1, 1));
expectNotEqual(new GeoPoint(1, 2), new GeoPoint(2, 1));
});

it('serializes to JSON', () => {
expect(new GeoPoint(1, 2).toJSON()).to.deep.equal({
latitude: 1,
longitude: 2
});
expect(new GeoPoint(0, 0).toJSON()).to.deep.equal({
latitude: 0,
longitude: 0
});
expect(new GeoPoint(90, 180).toJSON()).to.deep.equal({
latitude: 90,
longitude: 180
});
});
});
15 changes: 15 additions & 0 deletions packages/firestore/test/unit/api/timestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ describe('Timestamp', () => {
expect(t1 > t2).to.be.false;
expect(t1 >= t2).to.be.false;
});

it('serializes to JSON', () => {
expect(new Timestamp(123, 456).toJSON()).to.deep.equal({
seconds: 123,
nanoseconds: 456
});
expect(new Timestamp(0, 0).toJSON()).to.deep.equal({
seconds: 0,
nanoseconds: 0
});
expect(new Timestamp(-123, 456).toJSON()).to.deep.equal({
seconds: -123,
nanoseconds: 456
});
});
});