Skip to content

Add geopoint support #105

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 1 commit into from
Jul 14, 2021
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '8'
- '10'
- '12'
- '8'
- '10'
- '12'
1 change: 0 additions & 1 deletion scripts/publish-container/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/fft-package-builder', '.']
images: ['gcr.io/$PROJECT_ID/fft-package-builder']

4 changes: 1 addition & 3 deletions scripts/publish/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ steps:
# Set up the Twitter credentials.
- name: 'gcr.io/$PROJECT_ID/fft-package-builder'
entrypoint: 'cp'
args:
['-v', 'twitter.json', '${_REPOSITORY_NAME}/scripts/twitter.json']
args: ['-v', 'twitter.json', '${_REPOSITORY_NAME}/scripts/twitter.json']

# Set up the npm credentials.
- name: 'gcr.io/$PROJECT_ID/fft-package-builder'
Expand Down Expand Up @@ -112,4 +111,3 @@ substitutions:
_KEY_NAME: 'publish'
_REPOSITORY_ORG: 'firebase'
_REPOSITORY_NAME: 'firebase-functions-test'

44 changes: 44 additions & 0 deletions spec/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import * as firebase from 'firebase-admin';
import fft = require('../../src/index');

describe('providers/firestore', () => {
Expand Down Expand Up @@ -29,4 +30,47 @@ describe('providers/firestore', () => {
expect(snapshot.data()).to.deep.equal(undefined);
expect(snapshot.id).to.equal('doc-id');
});

it('should allow geopoints with makeDocumentSnapshot', () => {
const test = fft();
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional - could get rid of this line thats repeated in every test cases and use a before/beforeEach instead


const hq = new firebase.firestore.GeoPoint(47.6703, 122.1971);
const snapshot = test.firestore.makeDocumentSnapshot(
{ geopoint: hq },
'collection/doc-id'
);

expect(snapshot.data()).to.deep.equal({ geopoint: hq });
});

it('should allow timestmaps with makeDocumentSnapshot', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

typo in timestamps

const test = fft();

const time = new Date();
const snapshot = test.firestore.makeDocumentSnapshot(
{ time },
'collection/doc-id'
);

expect(snapshot.data().time).to.be.instanceof(firebase.firestore.Timestamp);
expect(snapshot.data().time.toDate()).to.deep.equal(time);
});

it('should allow references with makeDocumentSnapshot', () => {
const test = fft();
firebase.initializeApp({
projectId: 'not-a-project',
});

const ref = firebase.firestore().doc('collection/doc-id');
const snapshot = test.firestore.makeDocumentSnapshot(
{ ref },
'collection/doc-id'
);

expect(snapshot.data().ref).to.be.instanceOf(
firebase.firestore.DocumentReference
);
expect(snapshot.data().ref.toString()).to.equal(ref.toString());
});
});
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@

import { has, merge, random, get } from 'lodash';

import { CloudFunction, EventContext, Change, https, config } from 'firebase-functions';
import {
CloudFunction,
EventContext,
Change,
https,
config,
} from 'firebase-functions';

/** Fields of the event context that can be overridden/customized. */
export type EventContextOptions = {
Expand Down
12 changes: 12 additions & 0 deletions src/providers/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ export function objectToValueProto(data: object) {
};
}
if (val === null) {
// TODO: Look this up. This is a google.protobuf.NulLValue,
// and everything in google.protobuf has a customized JSON encoder.
// OTOH, Firestore's generated .d.ts files don't take this into
// account and have the default proto layout.
return {
nullValue: 'NULL_VALUE',
};
Expand All @@ -199,6 +203,14 @@ export function objectToValueProto(data: object) {
timestampValue: val.toDate().toISOString(),
};
}
if (val instanceof firestore.GeoPoint) {
return {
geoPointValue: {
latitude: val.latitude,
longitude: val.longitude,
},
};
}
if (isPlainObject(val)) {
return {
mapValue: {
Expand Down