Skip to content

Commit fbacf5c

Browse files
gino-mrfontanarosa
andauthored
Add method to get field ids for a proto message class (#1938)
Co-authored-by: Roberto Fontanarosa <[email protected]>
1 parent 57175ed commit fbacf5c

23 files changed

+138
-203
lines changed

functions/src/common/datastore.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import * as functions from 'firebase-functions';
1818
import {firestore} from 'firebase-admin';
1919
import {DocumentData, GeoPoint, QuerySnapshot} from 'firebase-admin/firestore';
20-
import {FieldNumbers} from '@ground/lib/dist/proto-field-numbers';
20+
import {registry} from '@ground/lib';
21+
import {GroundProtos} from '@ground/proto';
22+
23+
import Pb = GroundProtos.ground.v1beta1;
24+
const l = registry.getFieldIds(Pb.LocationOfInterest);
25+
const sb = registry.getFieldIds(Pb.Submission);
2126

2227
/**
2328
*
@@ -120,7 +125,7 @@ export class Datastore {
120125
fetchSubmissionsByJobId(surveyId: string, jobId: string) {
121126
return this.db_
122127
.collection(submissions(surveyId))
123-
.where(FieldNumbers.Submission.job_id, '==', jobId)
128+
.where(sb.jobId, '==', jobId)
124129
.get();
125130
}
126131

@@ -134,7 +139,7 @@ export class Datastore {
134139
): Promise<QuerySnapshot<DocumentData, DocumentData>> {
135140
return this.db_
136141
.collection(lois(surveyId))
137-
.where(FieldNumbers.LocationOfInterest.job_id, '==', jobId)
142+
.where(l.jobId, '==', jobId)
138143
.get();
139144
}
140145

@@ -151,11 +156,7 @@ export class Datastore {
151156
loiId: string
152157
): Promise<number> {
153158
const submissionsRef = this.db_.collection(submissions(surveyId));
154-
const submissionsForLoiQuery = submissionsRef.where(
155-
FieldNumbers.Submission.loi_id,
156-
'==',
157-
loiId
158-
);
159+
const submissionsForLoiQuery = submissionsRef.where(sb.loiId, '==', loiId);
159160
const snapshot = await submissionsForLoiQuery.count().get();
160161
return snapshot.data().count;
161162
}

functions/src/import-geojson.spec.ts

+46-50
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ import {
2323
createPostRequestSpy,
2424
createResponseSpy,
2525
} from './testing/http-test-helpers';
26-
import {
27-
$job_id,
28-
$geometry,
29-
$submission_count,
30-
$source,
31-
$properties,
32-
$point,
33-
$polygon,
34-
$multi_polygon,
35-
$shell,
36-
$coordinates,
37-
$polygons,
38-
$latitude,
39-
$longitude,
40-
} from '@ground/lib/dist/testing/proto-field-aliases';
4126
import {importGeoJsonCallback} from './import-geojson';
4227
import {DecodedIdToken} from 'firebase-admin/auth';
4328
import {Blob, FormData} from 'formdata-node';
@@ -46,6 +31,17 @@ import {invokeCallbackAsync} from './handlers';
4631
import {OWNER_ROLE} from './common/auth';
4732
import {resetDatastore} from './common/context';
4833
import {Firestore} from 'firebase-admin/firestore';
34+
import {registry} from '@ground/lib';
35+
import {GroundProtos} from '@ground/proto';
36+
37+
import Pb = GroundProtos.ground.v1beta1;
38+
const l = registry.getFieldIds(Pb.LocationOfInterest);
39+
const g = registry.getFieldIds(Pb.Geometry);
40+
const p = registry.getFieldIds(Pb.Point);
41+
const c = registry.getFieldIds(Pb.Coordinates);
42+
const pg = registry.getFieldIds(Pb.Polygon);
43+
const lr = registry.getFieldIds(Pb.LinearRing);
44+
const mp = registry.getFieldIds(Pb.MultiPolygon);
4945

5046
describe('importGeoJson()', () => {
5147
let mockFirestore: Firestore;
@@ -76,13 +72,13 @@ describe('importGeoJson()', () => {
7672
],
7773
};
7874
const pointLoi = {
79-
[$job_id]: 'job123',
80-
[$geometry]: {
81-
[$point]: {[$coordinates]: {[$latitude]: 10.1, [$longitude]: 125.6}},
75+
[l.jobId]: 'job123',
76+
[l.geometry]: {
77+
[g.point]: {[p.coordinates]: {[c.latitude]: 10.1, [c.longitude]: 125.6}},
8278
},
83-
[$submission_count]: 0,
84-
[$source]: 1, // IMPORTED
85-
[$properties]: {name: 'Dinagat Islands', area: 3.08},
79+
[l.submissionCount]: 0,
80+
[l.source]: 1, // IMPORTED
81+
[l.properties]: {name: 'Dinagat Islands', area: 3.08},
8682
jobId: 'job123',
8783
predefined: true,
8884
geometry: {type: 'Point', coordinates: TestGeoPoint(10.1, 125.6)},
@@ -108,21 +104,21 @@ describe('importGeoJson()', () => {
108104
],
109105
};
110106
const polygonLoi = {
111-
[$job_id]: 'job123',
112-
[$geometry]: {
113-
[$polygon]: {
114-
[$shell]: {
115-
[$coordinates]: [
116-
{[$latitude]: 0, [$longitude]: 100},
117-
{[$latitude]: 0, [$longitude]: 101},
118-
{[$latitude]: 1, [$longitude]: 101},
119-
{[$latitude]: 0, [$longitude]: 100},
107+
[l.jobId]: 'job123',
108+
[l.geometry]: {
109+
[g.polygon]: {
110+
[pg.shell]: {
111+
[lr.coordinates]: [
112+
{[c.latitude]: 0, [c.longitude]: 100},
113+
{[c.latitude]: 0, [c.longitude]: 101},
114+
{[c.latitude]: 1, [c.longitude]: 101},
115+
{[c.latitude]: 0, [c.longitude]: 100},
120116
],
121117
},
122118
},
123119
},
124-
[$submission_count]: 0,
125-
[$source]: 1, // IMPORTED
120+
[l.submissionCount]: 0,
121+
[l.source]: 1, // IMPORTED
126122
jobId: 'job123',
127123
predefined: true,
128124
geometry: {
@@ -167,37 +163,37 @@ describe('importGeoJson()', () => {
167163
],
168164
};
169165
const multiPolygonLoi = {
170-
[$job_id]: 'job123',
171-
[$geometry]: {
172-
[$multi_polygon]: {
173-
[$polygons]: [
166+
[l.jobId]: 'job123',
167+
[l.geometry]: {
168+
[g.multiPolygon]: {
169+
[mp.polygons]: [
174170
// polygons[0]
175171
{
176-
[$shell]: {
177-
[$coordinates]: [
178-
{[$latitude]: 0, [$longitude]: 100},
179-
{[$latitude]: 0, [$longitude]: 101},
180-
{[$latitude]: 1, [$longitude]: 101},
181-
{[$latitude]: 0, [$longitude]: 100},
172+
[pg.shell]: {
173+
[lr.coordinates]: [
174+
{[c.latitude]: 0, [c.longitude]: 100},
175+
{[c.latitude]: 0, [c.longitude]: 101},
176+
{[c.latitude]: 1, [c.longitude]: 101},
177+
{[c.latitude]: 0, [c.longitude]: 100},
182178
],
183179
},
184180
},
185181
// polygons[1]
186182
{
187-
[$shell]: {
188-
[$coordinates]: [
189-
{[$latitude]: 1, [$longitude]: 120},
190-
{[$latitude]: 1, [$longitude]: 121},
191-
{[$latitude]: 2, [$longitude]: 121},
192-
{[$latitude]: 1, [$longitude]: 120},
183+
[pg.shell]: {
184+
[lr.coordinates]: [
185+
{[c.latitude]: 1, [c.longitude]: 120},
186+
{[c.latitude]: 1, [c.longitude]: 121},
187+
{[c.latitude]: 2, [c.longitude]: 121},
188+
{[c.latitude]: 1, [c.longitude]: 120},
193189
],
194190
},
195191
},
196192
],
197193
},
198194
},
199-
[$submission_count]: 0,
200-
[$source]: 1, // IMPORTED
195+
[l.submissionCount]: 0,
196+
[l.source]: 1, // IMPORTED
201197
jobId: 'job123',
202198
predefined: true,
203199
geometry: {

functions/src/import-geojson.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {DocumentData} from 'firebase-admin/firestore';
2727
import {toDocumentData, deleteEmpty} from '@ground/lib';
2828
import {Feature, Geometry, Position} from 'geojson';
2929

30-
import Pb = GroundProtos.google.ground.v1beta1;
30+
import Pb = GroundProtos.ground.v1beta1;
3131
import {ErrorHandler} from './handlers';
3232

3333
/**

functions/src/on-write-submission.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ import * as functions from './index';
2525
import {loi} from './common/datastore';
2626
import {Firestore} from 'firebase-admin/firestore';
2727
import {resetDatastore} from './common/context';
28-
import {FieldNumbers} from '@ground/lib';
28+
import {registry} from '@ground/lib';
29+
import {GroundProtos} from '@ground/proto';
2930

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

33+
import Pb = GroundProtos.ground.v1beta1;
34+
const sb = registry.getFieldIds(Pb.Submission);
35+
3236
describe('onWriteSubmission()', () => {
3337
let mockFirestore: Firestore;
3438
const SURVEY_ID = 'survey1';
@@ -63,7 +67,7 @@ describe('onWriteSubmission()', () => {
6367
.and.returnValue({
6468
where: jasmine
6569
.createSpy('where')
66-
.withArgs(FieldNumbers.Submission.loi_id, '==', loiId)
70+
.withArgs(sb.loiId, '==', loiId)
6771
.and.returnValue(newCountQuery(count)),
6872
} as any);
6973
}

lib/src/firestore-to-proto.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {toMessage} from './firestore-to-proto';
1919
import {Constructor} from 'protobufjs';
2020

2121
const {Coordinates, Job, LinearRing, Role, Style, Survey, Task} =
22-
GroundProtos.google.ground.v1beta1;
22+
GroundProtos.ground.v1beta1;
2323

2424
describe('toMessage()', () => {
2525
[

lib/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
export {toDocumentData} from './proto-to-firestore';
1818
export {toMessage} from './firestore-to-proto';
1919
export {deleteEmpty, isEmpty} from './obj-util';
20-
export {FieldNumbers} from './proto-field-numbers';
20+
export {registry} from './message-registry';

lib/src/message-registry.ts

+14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ export class MessageRegistry {
7272
return typePath ? this.getDescriptorByPath(typePath) : null;
7373
}
7474

75+
/**
76+
* Returns a dictionary containing the numbers of all fields in a
77+
* message definition, keyed by field name. Throws an error if not found.
78+
*/
79+
getFieldIds(constructor: any): {[key: string]: string} {
80+
const desc = this.getMessageDescriptor(constructor);
81+
if (!desc) throw new Error(`Unknown constructor ${constructor.name}`);
82+
const map: {[key: string]: string} = {};
83+
if (desc.fields) {
84+
Object.keys(desc.fields).forEach(name => map[name] = desc.fields![name].id?.toString());
85+
}
86+
return map;
87+
}
88+
7589
/**
7690
* Searches the descriptor hierarchy starting for the specified `typeName`,
7791
* starting from the specified containing `messageTypePath`. If the type

lib/src/proto-field-numbers.ts

-32
This file was deleted.

0 commit comments

Comments
 (0)