Skip to content
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

Add method to get field ids for a proto message class #1938

Merged
merged 17 commits into from
Aug 2, 2024
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
17 changes: 9 additions & 8 deletions functions/src/common/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
import * as functions from 'firebase-functions';
import {firestore} from 'firebase-admin';
import {DocumentData, GeoPoint, QuerySnapshot} from 'firebase-admin/firestore';
import {FieldNumbers} from '@ground/lib/dist/proto-field-numbers';
import {registry} from '@ground/lib';
import {GroundProtos} from '@ground/proto';

import Pb = GroundProtos.ground.v1beta1;
const l = registry.getFieldIds(Pb.LocationOfInterest);
const sb = registry.getFieldIds(Pb.Submission);

/**
*
*/
type pseudoGeoJsonGeometry = {
type: string;
coordinates: any;

Check warning on line 32 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
};

/**
Expand Down Expand Up @@ -120,7 +125,7 @@
fetchSubmissionsByJobId(surveyId: string, jobId: string) {
return this.db_
.collection(submissions(surveyId))
.where(FieldNumbers.Submission.job_id, '==', jobId)
.where(sb.jobId, '==', jobId)
.get();
}

Expand All @@ -134,7 +139,7 @@
): Promise<QuerySnapshot<DocumentData, DocumentData>> {
return this.db_
.collection(lois(surveyId))
.where(FieldNumbers.LocationOfInterest.job_id, '==', jobId)
.where(l.jobId, '==', jobId)
.get();
}

Expand All @@ -151,11 +156,7 @@
loiId: string
): Promise<number> {
const submissionsRef = this.db_.collection(submissions(surveyId));
const submissionsForLoiQuery = submissionsRef.where(
FieldNumbers.Submission.loi_id,
'==',
loiId
);
const submissionsForLoiQuery = submissionsRef.where(sb.loiId, '==', loiId);
const snapshot = await submissionsForLoiQuery.count().get();
return snapshot.data().count;
}
Expand All @@ -174,7 +175,7 @@
await loiRef.update({properties});
}

static toFirestoreMap(geometry: any) {

Check warning on line 178 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
return Object.fromEntries(
Object.entries(geometry).map(([key, value]) => [
key,
Expand All @@ -183,7 +184,7 @@
);
}

static toFirestoreValue(value: any): any {

Check warning on line 187 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type

Check warning on line 187 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
if (value === null) {
return null;
}
Expand Down Expand Up @@ -211,7 +212,7 @@
*
* @returns GeoJSON geometry object (with geometry as list of lists)
*/
static fromFirestoreMap(geoJsonGeometry: any): any {

Check warning on line 215 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type

Check warning on line 215 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
const geometryObject = geoJsonGeometry as pseudoGeoJsonGeometry;
if (!geometryObject) {
throw new Error(
Expand All @@ -226,7 +227,7 @@
return geometryObject;
}

static fromFirestoreValue(coordinates: any) {

Check warning on line 230 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type
if (coordinates instanceof GeoPoint) {
// Note: GeoJSON coordinates are in lng-lat order.
return [coordinates.longitude, coordinates.latitude];
Expand All @@ -235,7 +236,7 @@
if (typeof coordinates !== 'object') {
return coordinates;
}
const result = new Array<any>(coordinates.length);

Check warning on line 239 in functions/src/common/datastore.ts

View workflow job for this annotation

GitHub Actions / Check

Unexpected any. Specify a different type

Object.entries(coordinates).map(([i, nestedValue]) => {
const index = Number.parseInt(i);
Expand Down
96 changes: 46 additions & 50 deletions functions/src/import-geojson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ import {
createPostRequestSpy,
createResponseSpy,
} from './testing/http-test-helpers';
import {
$job_id,
$geometry,
$submission_count,
$source,
$properties,
$point,
$polygon,
$multi_polygon,
$shell,
$coordinates,
$polygons,
$latitude,
$longitude,
} from '@ground/lib/dist/testing/proto-field-aliases';
import {importGeoJsonCallback} from './import-geojson';
import {DecodedIdToken} from 'firebase-admin/auth';
import {Blob, FormData} from 'formdata-node';
Expand All @@ -46,6 +31,17 @@ import {invokeCallbackAsync} from './handlers';
import {OWNER_ROLE} from './common/auth';
import {resetDatastore} from './common/context';
import {Firestore} from 'firebase-admin/firestore';
import {registry} from '@ground/lib';
import {GroundProtos} from '@ground/proto';

import Pb = GroundProtos.ground.v1beta1;
const l = registry.getFieldIds(Pb.LocationOfInterest);
const g = registry.getFieldIds(Pb.Geometry);
const p = registry.getFieldIds(Pb.Point);
const c = registry.getFieldIds(Pb.Coordinates);
const pg = registry.getFieldIds(Pb.Polygon);
const lr = registry.getFieldIds(Pb.LinearRing);
const mp = registry.getFieldIds(Pb.MultiPolygon);

describe('importGeoJson()', () => {
let mockFirestore: Firestore;
Expand Down Expand Up @@ -76,13 +72,13 @@ describe('importGeoJson()', () => {
],
};
const pointLoi = {
[$job_id]: 'job123',
[$geometry]: {
[$point]: {[$coordinates]: {[$latitude]: 10.1, [$longitude]: 125.6}},
[l.jobId]: 'job123',
[l.geometry]: {
[g.point]: {[p.coordinates]: {[c.latitude]: 10.1, [c.longitude]: 125.6}},
},
[$submission_count]: 0,
[$source]: 1, // IMPORTED
[$properties]: {name: 'Dinagat Islands', area: 3.08},
[l.submissionCount]: 0,
[l.source]: 1, // IMPORTED
[l.properties]: {name: 'Dinagat Islands', area: 3.08},
jobId: 'job123',
predefined: true,
geometry: {type: 'Point', coordinates: TestGeoPoint(10.1, 125.6)},
Expand All @@ -108,21 +104,21 @@ describe('importGeoJson()', () => {
],
};
const polygonLoi = {
[$job_id]: 'job123',
[$geometry]: {
[$polygon]: {
[$shell]: {
[$coordinates]: [
{[$latitude]: 0, [$longitude]: 100},
{[$latitude]: 0, [$longitude]: 101},
{[$latitude]: 1, [$longitude]: 101},
{[$latitude]: 0, [$longitude]: 100},
[l.jobId]: 'job123',
[l.geometry]: {
[g.polygon]: {
[pg.shell]: {
[lr.coordinates]: [
{[c.latitude]: 0, [c.longitude]: 100},
{[c.latitude]: 0, [c.longitude]: 101},
{[c.latitude]: 1, [c.longitude]: 101},
{[c.latitude]: 0, [c.longitude]: 100},
],
},
},
},
[$submission_count]: 0,
[$source]: 1, // IMPORTED
[l.submissionCount]: 0,
[l.source]: 1, // IMPORTED
jobId: 'job123',
predefined: true,
geometry: {
Expand Down Expand Up @@ -167,37 +163,37 @@ describe('importGeoJson()', () => {
],
};
const multiPolygonLoi = {
[$job_id]: 'job123',
[$geometry]: {
[$multi_polygon]: {
[$polygons]: [
[l.jobId]: 'job123',
[l.geometry]: {
[g.multiPolygon]: {
[mp.polygons]: [
// polygons[0]
{
[$shell]: {
[$coordinates]: [
{[$latitude]: 0, [$longitude]: 100},
{[$latitude]: 0, [$longitude]: 101},
{[$latitude]: 1, [$longitude]: 101},
{[$latitude]: 0, [$longitude]: 100},
[pg.shell]: {
[lr.coordinates]: [
{[c.latitude]: 0, [c.longitude]: 100},
{[c.latitude]: 0, [c.longitude]: 101},
{[c.latitude]: 1, [c.longitude]: 101},
{[c.latitude]: 0, [c.longitude]: 100},
],
},
},
// polygons[1]
{
[$shell]: {
[$coordinates]: [
{[$latitude]: 1, [$longitude]: 120},
{[$latitude]: 1, [$longitude]: 121},
{[$latitude]: 2, [$longitude]: 121},
{[$latitude]: 1, [$longitude]: 120},
[pg.shell]: {
[lr.coordinates]: [
{[c.latitude]: 1, [c.longitude]: 120},
{[c.latitude]: 1, [c.longitude]: 121},
{[c.latitude]: 2, [c.longitude]: 121},
{[c.latitude]: 1, [c.longitude]: 120},
],
},
},
],
},
},
[$submission_count]: 0,
[$source]: 1, // IMPORTED
[l.submissionCount]: 0,
[l.source]: 1, // IMPORTED
jobId: 'job123',
predefined: true,
geometry: {
Expand Down
2 changes: 1 addition & 1 deletion functions/src/import-geojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {DocumentData} from 'firebase-admin/firestore';
import {toDocumentData, deleteEmpty} from '@ground/lib';
import {Feature, Geometry, Position} from 'geojson';

import Pb = GroundProtos.google.ground.v1beta1;
import Pb = GroundProtos.ground.v1beta1;
import {ErrorHandler} from './handlers';

/**
Expand Down
8 changes: 6 additions & 2 deletions functions/src/on-write-submission.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ import * as functions from './index';
import {loi} from './common/datastore';
import {Firestore} from 'firebase-admin/firestore';
import {resetDatastore} from './common/context';
import {FieldNumbers} from '@ground/lib';
import {registry} from '@ground/lib';
import {GroundProtos} from '@ground/proto';

const test = require('firebase-functions-test')();

import Pb = GroundProtos.ground.v1beta1;
const sb = registry.getFieldIds(Pb.Submission);

describe('onWriteSubmission()', () => {
let mockFirestore: Firestore;
const SURVEY_ID = 'survey1';
Expand Down Expand Up @@ -63,7 +67,7 @@ describe('onWriteSubmission()', () => {
.and.returnValue({
where: jasmine
.createSpy('where')
.withArgs(FieldNumbers.Submission.loi_id, '==', loiId)
.withArgs(sb.loiId, '==', loiId)
.and.returnValue(newCountQuery(count)),
} as any);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/firestore-to-proto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {toMessage} from './firestore-to-proto';
import {Constructor} from 'protobufjs';

const {Coordinates, Job, LinearRing, Role, Style, Survey, Task} =
GroundProtos.google.ground.v1beta1;
GroundProtos.ground.v1beta1;

describe('toMessage()', () => {
[
Expand Down
2 changes: 1 addition & 1 deletion lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
export {toDocumentData} from './proto-to-firestore';
export {toMessage} from './firestore-to-proto';
export {deleteEmpty, isEmpty} from './obj-util';
export {FieldNumbers} from './proto-field-numbers';
export {registry} from './message-registry';
14 changes: 14 additions & 0 deletions lib/src/message-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ export class MessageRegistry {
return typePath ? this.getDescriptorByPath(typePath) : null;
}

/**
* Returns a dictionary containing the numbers of all fields in a
* message definition, keyed by field name. Throws an error if not found.
*/
getFieldIds(constructor: any): {[key: string]: string} {
const desc = this.getMessageDescriptor(constructor);
if (!desc) throw new Error(`Unknown constructor ${constructor.name}`);
const map: {[key: string]: string} = {};
if (desc.fields) {
Object.keys(desc.fields).forEach(name => map[name] = desc.fields![name].id?.toString());
}
return map;
}

/**
* Searches the descriptor hierarchy starting for the specified `typeName`,
* starting from the specified containing `messageTypePath`. If the type
Expand Down
32 changes: 0 additions & 32 deletions lib/src/proto-field-numbers.ts

This file was deleted.

Loading
Loading