Skip to content

Commit f303049

Browse files
committed
Properly annotate map fields in pbjs, fixes #624
1 parent 4b78628 commit f303049

File tree

5 files changed

+80
-79
lines changed

5 files changed

+80
-79
lines changed

Diff for: cli/targets/static.js

+42-41
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,37 @@ function buildFunction(type, functionName, gen, scope) {
199199
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
200200
}
201201

202+
function toJsType(field) {
203+
switch (field.type) {
204+
case "double":
205+
case "float":
206+
case "int32":
207+
case "uint32":
208+
case "sint32":
209+
case "fixed32":
210+
case "sfixed32":
211+
return "number";
212+
case "int64":
213+
case "uint64":
214+
case "sint64":
215+
case "fixed64":
216+
case "sfixed64":
217+
return "number|$protobuf.Long";
218+
case "bool":
219+
return "boolean";
220+
case "string":
221+
return "string";
222+
case "bytes":
223+
return "Uint8Array";
224+
default:
225+
if (field.resolvedType instanceof Enum)
226+
return "number";
227+
if (field.resolvedType instanceof Type)
228+
return field.resolvedType.fullName.substring(1);
229+
return "*"; // should not happen
230+
}
231+
}
232+
202233
function buildType(ref, type) {
203234
var fullName = type.fullName.substring(1);
204235

@@ -233,44 +264,10 @@ function buildType(ref, type) {
233264
// default values
234265
type.fieldsArray.forEach(function(field) {
235266
field.resolve();
236-
var jsType;
237-
switch (field.type) {
238-
case "double":
239-
case "float":
240-
case "int32":
241-
case "uint32":
242-
case "sint32":
243-
case "fixed32":
244-
case "sfixed32":
245-
jsType = "number";
246-
break;
247-
case "int64":
248-
case "uint64":
249-
case "sint64":
250-
case "fixed64":
251-
case "sfixed64":
252-
jsType = "number|$protobuf.Long";
253-
break;
254-
case "bool":
255-
jsType = "boolean";
256-
break;
257-
case "string":
258-
jsType = "string";
259-
break;
260-
case "bytes":
261-
jsType = "Uint8Array";
262-
break;
263-
default:
264-
if (field.resolvedType instanceof Enum) {
265-
jsType = "number";
266-
} else if (field.resolvedType instanceof Type) {
267-
jsType = field.resolvedType.fullName.substring(1);
268-
} else {
269-
jsType = "*"; // should not happen
270-
}
271-
break;
272-
}
273-
if (field.repeated)
267+
var jsType = toJsType(field);
268+
if (field.map)
269+
jsType = "Object.<string," + jsType + ">"; // keys are always strings
270+
else if (field.repeated)
274271
jsType = "Array.<" + jsType + ">";
275272
var prop = util.safeProp(field.name);
276273
if (config.comments) {
@@ -281,12 +278,16 @@ function buildType(ref, type) {
281278
"@type {" + jsType + "}"
282279
]);
283280
}
284-
if (Array.isArray(field.defaultValue)) {
281+
if (field.repeated)
285282
push("$prototype" + prop + " = $protobuf.util.emptyArray;");
286-
} else if (util.isObject(field.defaultValue))
283+
else if (field.map)
287284
push("$prototype" + prop + " = $protobuf.util.emptyObject;");
285+
else if (field.long)
286+
push("$prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromValue(" + JSON.stringify(field.typeDefault) + ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";");
287+
else if (field.bytes)
288+
push("$prototype" + prop + " = " + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ";");
288289
else
289-
push("$prototype" + prop + " = " + JSON.stringify(field.defaultValue) + ";");
290+
push("$prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
290291
});
291292

292293
// virtual oneof fields

Diff for: tests/data/mapbox/vector_tile.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,19 @@ $root.vector_tile = (function() {
253253
* Value intValue.
254254
* @type {number|$protobuf.Long}
255255
*/
256-
$prototype.intValue = $protobuf.util.emptyObject;
256+
$prototype.intValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":false}) : 0;
257257

258258
/**
259259
* Value uintValue.
260260
* @type {number|$protobuf.Long}
261261
*/
262-
$prototype.uintValue = $protobuf.util.emptyObject;
262+
$prototype.uintValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":true}) : 0;
263263

264264
/**
265265
* Value sintValue.
266266
* @type {number|$protobuf.Long}
267267
*/
268-
$prototype.sintValue = $protobuf.util.emptyObject;
268+
$prototype.sintValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":false}) : 0;
269269

270270
/**
271271
* Value boolValue.
@@ -517,7 +517,7 @@ $root.vector_tile = (function() {
517517
* Feature id.
518518
* @type {number|$protobuf.Long}
519519
*/
520-
$prototype.id = $protobuf.util.emptyObject;
520+
$prototype.id = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":true}) : 0;
521521

522522
/**
523523
* Feature tags.

Diff for: tests/data/package.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,31 @@ $root.Package = (function() {
9090

9191
/**
9292
* Package bin.
93-
* @type {string}
93+
* @type {Object.<string,string>}
9494
*/
9595
$prototype.bin = $protobuf.util.emptyObject;
9696

9797
/**
9898
* Package scripts.
99-
* @type {string}
99+
* @type {Object.<string,string>}
100100
*/
101101
$prototype.scripts = $protobuf.util.emptyObject;
102102

103103
/**
104104
* Package dependencies.
105-
* @type {string}
105+
* @type {Object.<string,string>}
106106
*/
107107
$prototype.dependencies = $protobuf.util.emptyObject;
108108

109109
/**
110110
* Package optionalDependencies.
111-
* @type {string}
111+
* @type {Object.<string,string>}
112112
*/
113113
$prototype.optionalDependencies = $protobuf.util.emptyObject;
114114

115115
/**
116116
* Package devDependencies.
117-
* @type {string}
117+
* @type {Object.<string,string>}
118118
*/
119119
$prototype.devDependencies = $protobuf.util.emptyObject;
120120

Diff for: tests/data/test.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,18 @@ export namespace jspb {
394394

395395
class TestMapFieldsNoBinary {
396396
constructor(properties?: Object);
397-
mapStringString: string;
398-
mapStringInt32: number;
399-
mapStringInt64: (number|$protobuf.Long);
400-
mapStringBool: boolean;
401-
mapStringDouble: number;
402-
mapStringEnum: number;
403-
mapStringMsg: jspb.test.MapValueMessageNoBinary;
404-
mapInt32String: string;
405-
mapInt64String: string;
406-
mapBoolString: string;
397+
mapStringString: { [k: string]: string };
398+
mapStringInt32: { [k: string]: number };
399+
mapStringInt64: { [k: string]: (number|$protobuf.Long) };
400+
mapStringBool: { [k: string]: boolean };
401+
mapStringDouble: { [k: string]: number };
402+
mapStringEnum: { [k: string]: number };
403+
mapStringMsg: { [k: string]: jspb.test.MapValueMessageNoBinary };
404+
mapInt32String: { [k: string]: string };
405+
mapInt64String: { [k: string]: string };
406+
mapBoolString: { [k: string]: string };
407407
testMapFields: jspb.test.TestMapFieldsNoBinary;
408-
mapStringTestmapfields: jspb.test.TestMapFieldsNoBinary;
408+
mapStringTestmapfields: { [k: string]: jspb.test.TestMapFieldsNoBinary };
409409
static create(properties?: Object): jspb.test.TestMapFieldsNoBinary;
410410
static encode(message: (jspb.test.TestMapFieldsNoBinary|Object), writer?: $protobuf.Writer): $protobuf.Writer;
411411
static encodeDelimited(message: (jspb.test.TestMapFieldsNoBinary|Object), writer?: $protobuf.Writer): $protobuf.Writer;

Diff for: tests/data/test.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,7 @@ $root.jspb = (function() {
27852785
* DefaultValues intField.
27862786
* @type {number|$protobuf.Long}
27872787
*/
2788-
$prototype.intField = $protobuf.util.emptyObject;
2788+
$prototype.intField = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":11,"high":0,"unsigned":false}) : 11;
27892789

27902790
/**
27912791
* DefaultValues enumField.
@@ -2803,7 +2803,7 @@ $root.jspb = (function() {
28032803
* DefaultValues bytesField.
28042804
* @type {Uint8Array}
28052805
*/
2806-
$prototype.bytesField = $protobuf.util.emptyObject;
2806+
$prototype.bytesField = [109,111,111];
28072807

28082808
// Referenced types
28092809
var $types = [null, null, null, "jspb.test.DefaultValues.Enum", null, null]; $lazyTypes.push($types);
@@ -3422,7 +3422,7 @@ $root.jspb = (function() {
34223422
* TestClone bytesField.
34233423
* @type {Uint8Array}
34243424
*/
3425-
$prototype.bytesField = $protobuf.util.emptyArray;
3425+
$prototype.bytesField = [];
34263426

34273427
/**
34283428
* TestClone unused.
@@ -4823,7 +4823,7 @@ $root.jspb = (function() {
48234823
* TestEndsWithBytes data.
48244824
* @type {Uint8Array}
48254825
*/
4826-
$prototype.data = $protobuf.util.emptyArray;
4826+
$prototype.data = [];
48274827

48284828
/**
48294829
* Creates a new TestEndsWithBytes instance using the specified properties.
@@ -4992,61 +4992,61 @@ $root.jspb = (function() {
49924992

49934993
/**
49944994
* TestMapFieldsNoBinary mapStringString.
4995-
* @type {string}
4995+
* @type {Object.<string,string>}
49964996
*/
49974997
$prototype.mapStringString = $protobuf.util.emptyObject;
49984998

49994999
/**
50005000
* TestMapFieldsNoBinary mapStringInt32.
5001-
* @type {number}
5001+
* @type {Object.<string,number>}
50025002
*/
50035003
$prototype.mapStringInt32 = $protobuf.util.emptyObject;
50045004

50055005
/**
50065006
* TestMapFieldsNoBinary mapStringInt64.
5007-
* @type {number|$protobuf.Long}
5007+
* @type {Object.<string,number|$protobuf.Long>}
50085008
*/
50095009
$prototype.mapStringInt64 = $protobuf.util.emptyObject;
50105010

50115011
/**
50125012
* TestMapFieldsNoBinary mapStringBool.
5013-
* @type {boolean}
5013+
* @type {Object.<string,boolean>}
50145014
*/
50155015
$prototype.mapStringBool = $protobuf.util.emptyObject;
50165016

50175017
/**
50185018
* TestMapFieldsNoBinary mapStringDouble.
5019-
* @type {number}
5019+
* @type {Object.<string,number>}
50205020
*/
50215021
$prototype.mapStringDouble = $protobuf.util.emptyObject;
50225022

50235023
/**
50245024
* TestMapFieldsNoBinary mapStringEnum.
5025-
* @type {number}
5025+
* @type {Object.<string,number>}
50265026
*/
50275027
$prototype.mapStringEnum = $protobuf.util.emptyObject;
50285028

50295029
/**
50305030
* TestMapFieldsNoBinary mapStringMsg.
5031-
* @type {jspb.test.MapValueMessageNoBinary}
5031+
* @type {Object.<string,jspb.test.MapValueMessageNoBinary>}
50325032
*/
50335033
$prototype.mapStringMsg = $protobuf.util.emptyObject;
50345034

50355035
/**
50365036
* TestMapFieldsNoBinary mapInt32String.
5037-
* @type {string}
5037+
* @type {Object.<string,string>}
50385038
*/
50395039
$prototype.mapInt32String = $protobuf.util.emptyObject;
50405040

50415041
/**
50425042
* TestMapFieldsNoBinary mapInt64String.
5043-
* @type {string}
5043+
* @type {Object.<string,string>}
50445044
*/
50455045
$prototype.mapInt64String = $protobuf.util.emptyObject;
50465046

50475047
/**
50485048
* TestMapFieldsNoBinary mapBoolString.
5049-
* @type {string}
5049+
* @type {Object.<string,string>}
50505050
*/
50515051
$prototype.mapBoolString = $protobuf.util.emptyObject;
50525052

@@ -5058,7 +5058,7 @@ $root.jspb = (function() {
50585058

50595059
/**
50605060
* TestMapFieldsNoBinary mapStringTestmapfields.
5061-
* @type {jspb.test.TestMapFieldsNoBinary}
5061+
* @type {Object.<string,jspb.test.TestMapFieldsNoBinary>}
50625062
*/
50635063
$prototype.mapStringTestmapfields = $protobuf.util.emptyObject;
50645064

@@ -11470,13 +11470,13 @@ $root.google = (function() {
1147011470
* UninterpretedOption positiveIntValue.
1147111471
* @type {number|$protobuf.Long}
1147211472
*/
11473-
$prototype.positiveIntValue = $protobuf.util.emptyObject;
11473+
$prototype.positiveIntValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":true}) : 0;
1147411474

1147511475
/**
1147611476
* UninterpretedOption negativeIntValue.
1147711477
* @type {number|$protobuf.Long}
1147811478
*/
11479-
$prototype.negativeIntValue = $protobuf.util.emptyObject;
11479+
$prototype.negativeIntValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":false}) : 0;
1148011480

1148111481
/**
1148211482
* UninterpretedOption doubleValue.
@@ -11488,7 +11488,7 @@ $root.google = (function() {
1148811488
* UninterpretedOption stringValue.
1148911489
* @type {Uint8Array}
1149011490
*/
11491-
$prototype.stringValue = $protobuf.util.emptyArray;
11491+
$prototype.stringValue = [];
1149211492

1149311493
/**
1149411494
* UninterpretedOption aggregateValue.

0 commit comments

Comments
 (0)