From f303049f92c53970619375653be46fbb4e3b7d78 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sat, 7 Jan 2017 01:01:05 +0100 Subject: [PATCH] Properly annotate map fields in pbjs, fixes #624 --- cli/targets/static.js | 83 ++++++++++++++++---------------- tests/data/mapbox/vector_tile.js | 8 +-- tests/data/package.js | 10 ++-- tests/data/test.d.ts | 22 ++++----- tests/data/test.js | 36 +++++++------- 5 files changed, 80 insertions(+), 79 deletions(-) diff --git a/cli/targets/static.js b/cli/targets/static.js index 313e31270..81b2b8d3b 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -199,6 +199,37 @@ function buildFunction(type, functionName, gen, scope) { push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");"); } +function toJsType(field) { + switch (field.type) { + case "double": + case "float": + case "int32": + case "uint32": + case "sint32": + case "fixed32": + case "sfixed32": + return "number"; + case "int64": + case "uint64": + case "sint64": + case "fixed64": + case "sfixed64": + return "number|$protobuf.Long"; + case "bool": + return "boolean"; + case "string": + return "string"; + case "bytes": + return "Uint8Array"; + default: + if (field.resolvedType instanceof Enum) + return "number"; + if (field.resolvedType instanceof Type) + return field.resolvedType.fullName.substring(1); + return "*"; // should not happen + } +} + function buildType(ref, type) { var fullName = type.fullName.substring(1); @@ -233,44 +264,10 @@ function buildType(ref, type) { // default values type.fieldsArray.forEach(function(field) { field.resolve(); - var jsType; - switch (field.type) { - case "double": - case "float": - case "int32": - case "uint32": - case "sint32": - case "fixed32": - case "sfixed32": - jsType = "number"; - break; - case "int64": - case "uint64": - case "sint64": - case "fixed64": - case "sfixed64": - jsType = "number|$protobuf.Long"; - break; - case "bool": - jsType = "boolean"; - break; - case "string": - jsType = "string"; - break; - case "bytes": - jsType = "Uint8Array"; - break; - default: - if (field.resolvedType instanceof Enum) { - jsType = "number"; - } else if (field.resolvedType instanceof Type) { - jsType = field.resolvedType.fullName.substring(1); - } else { - jsType = "*"; // should not happen - } - break; - } - if (field.repeated) + var jsType = toJsType(field); + if (field.map) + jsType = "Object."; // keys are always strings + else if (field.repeated) jsType = "Array.<" + jsType + ">"; var prop = util.safeProp(field.name); if (config.comments) { @@ -281,12 +278,16 @@ function buildType(ref, type) { "@type {" + jsType + "}" ]); } - if (Array.isArray(field.defaultValue)) { + if (field.repeated) push("$prototype" + prop + " = $protobuf.util.emptyArray;"); - } else if (util.isObject(field.defaultValue)) + else if (field.map) push("$prototype" + prop + " = $protobuf.util.emptyObject;"); + else if (field.long) + push("$prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromValue(" + JSON.stringify(field.typeDefault) + ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";"); + else if (field.bytes) + push("$prototype" + prop + " = " + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ";"); else - push("$prototype" + prop + " = " + JSON.stringify(field.defaultValue) + ";"); + push("$prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";"); }); // virtual oneof fields diff --git a/tests/data/mapbox/vector_tile.js b/tests/data/mapbox/vector_tile.js index 084d45cd7..7a2d702e5 100644 --- a/tests/data/mapbox/vector_tile.js +++ b/tests/data/mapbox/vector_tile.js @@ -253,19 +253,19 @@ $root.vector_tile = (function() { * Value intValue. * @type {number|$protobuf.Long} */ - $prototype.intValue = $protobuf.util.emptyObject; + $prototype.intValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":false}) : 0; /** * Value uintValue. * @type {number|$protobuf.Long} */ - $prototype.uintValue = $protobuf.util.emptyObject; + $prototype.uintValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":true}) : 0; /** * Value sintValue. * @type {number|$protobuf.Long} */ - $prototype.sintValue = $protobuf.util.emptyObject; + $prototype.sintValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":false}) : 0; /** * Value boolValue. @@ -517,7 +517,7 @@ $root.vector_tile = (function() { * Feature id. * @type {number|$protobuf.Long} */ - $prototype.id = $protobuf.util.emptyObject; + $prototype.id = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":true}) : 0; /** * Feature tags. diff --git a/tests/data/package.js b/tests/data/package.js index 7b9aa3d97..2b2002d36 100644 --- a/tests/data/package.js +++ b/tests/data/package.js @@ -90,31 +90,31 @@ $root.Package = (function() { /** * Package bin. - * @type {string} + * @type {Object.} */ $prototype.bin = $protobuf.util.emptyObject; /** * Package scripts. - * @type {string} + * @type {Object.} */ $prototype.scripts = $protobuf.util.emptyObject; /** * Package dependencies. - * @type {string} + * @type {Object.} */ $prototype.dependencies = $protobuf.util.emptyObject; /** * Package optionalDependencies. - * @type {string} + * @type {Object.} */ $prototype.optionalDependencies = $protobuf.util.emptyObject; /** * Package devDependencies. - * @type {string} + * @type {Object.} */ $prototype.devDependencies = $protobuf.util.emptyObject; diff --git a/tests/data/test.d.ts b/tests/data/test.d.ts index 02db04d39..d3181355c 100644 --- a/tests/data/test.d.ts +++ b/tests/data/test.d.ts @@ -394,18 +394,18 @@ export namespace jspb { class TestMapFieldsNoBinary { constructor(properties?: Object); - mapStringString: string; - mapStringInt32: number; - mapStringInt64: (number|$protobuf.Long); - mapStringBool: boolean; - mapStringDouble: number; - mapStringEnum: number; - mapStringMsg: jspb.test.MapValueMessageNoBinary; - mapInt32String: string; - mapInt64String: string; - mapBoolString: string; + mapStringString: { [k: string]: string }; + mapStringInt32: { [k: string]: number }; + mapStringInt64: { [k: string]: (number|$protobuf.Long) }; + mapStringBool: { [k: string]: boolean }; + mapStringDouble: { [k: string]: number }; + mapStringEnum: { [k: string]: number }; + mapStringMsg: { [k: string]: jspb.test.MapValueMessageNoBinary }; + mapInt32String: { [k: string]: string }; + mapInt64String: { [k: string]: string }; + mapBoolString: { [k: string]: string }; testMapFields: jspb.test.TestMapFieldsNoBinary; - mapStringTestmapfields: jspb.test.TestMapFieldsNoBinary; + mapStringTestmapfields: { [k: string]: jspb.test.TestMapFieldsNoBinary }; static create(properties?: Object): jspb.test.TestMapFieldsNoBinary; static encode(message: (jspb.test.TestMapFieldsNoBinary|Object), writer?: $protobuf.Writer): $protobuf.Writer; static encodeDelimited(message: (jspb.test.TestMapFieldsNoBinary|Object), writer?: $protobuf.Writer): $protobuf.Writer; diff --git a/tests/data/test.js b/tests/data/test.js index f6d1237fb..0d6cb1e7d 100644 --- a/tests/data/test.js +++ b/tests/data/test.js @@ -2785,7 +2785,7 @@ $root.jspb = (function() { * DefaultValues intField. * @type {number|$protobuf.Long} */ - $prototype.intField = $protobuf.util.emptyObject; + $prototype.intField = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":11,"high":0,"unsigned":false}) : 11; /** * DefaultValues enumField. @@ -2803,7 +2803,7 @@ $root.jspb = (function() { * DefaultValues bytesField. * @type {Uint8Array} */ - $prototype.bytesField = $protobuf.util.emptyObject; + $prototype.bytesField = [109,111,111]; // Referenced types var $types = [null, null, null, "jspb.test.DefaultValues.Enum", null, null]; $lazyTypes.push($types); @@ -3422,7 +3422,7 @@ $root.jspb = (function() { * TestClone bytesField. * @type {Uint8Array} */ - $prototype.bytesField = $protobuf.util.emptyArray; + $prototype.bytesField = []; /** * TestClone unused. @@ -4823,7 +4823,7 @@ $root.jspb = (function() { * TestEndsWithBytes data. * @type {Uint8Array} */ - $prototype.data = $protobuf.util.emptyArray; + $prototype.data = []; /** * Creates a new TestEndsWithBytes instance using the specified properties. @@ -4992,61 +4992,61 @@ $root.jspb = (function() { /** * TestMapFieldsNoBinary mapStringString. - * @type {string} + * @type {Object.} */ $prototype.mapStringString = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapStringInt32. - * @type {number} + * @type {Object.} */ $prototype.mapStringInt32 = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapStringInt64. - * @type {number|$protobuf.Long} + * @type {Object.} */ $prototype.mapStringInt64 = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapStringBool. - * @type {boolean} + * @type {Object.} */ $prototype.mapStringBool = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapStringDouble. - * @type {number} + * @type {Object.} */ $prototype.mapStringDouble = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapStringEnum. - * @type {number} + * @type {Object.} */ $prototype.mapStringEnum = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapStringMsg. - * @type {jspb.test.MapValueMessageNoBinary} + * @type {Object.} */ $prototype.mapStringMsg = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapInt32String. - * @type {string} + * @type {Object.} */ $prototype.mapInt32String = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapInt64String. - * @type {string} + * @type {Object.} */ $prototype.mapInt64String = $protobuf.util.emptyObject; /** * TestMapFieldsNoBinary mapBoolString. - * @type {string} + * @type {Object.} */ $prototype.mapBoolString = $protobuf.util.emptyObject; @@ -5058,7 +5058,7 @@ $root.jspb = (function() { /** * TestMapFieldsNoBinary mapStringTestmapfields. - * @type {jspb.test.TestMapFieldsNoBinary} + * @type {Object.} */ $prototype.mapStringTestmapfields = $protobuf.util.emptyObject; @@ -11470,13 +11470,13 @@ $root.google = (function() { * UninterpretedOption positiveIntValue. * @type {number|$protobuf.Long} */ - $prototype.positiveIntValue = $protobuf.util.emptyObject; + $prototype.positiveIntValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":true}) : 0; /** * UninterpretedOption negativeIntValue. * @type {number|$protobuf.Long} */ - $prototype.negativeIntValue = $protobuf.util.emptyObject; + $prototype.negativeIntValue = $protobuf.util.Long ? $protobuf.util.Long.fromValue({"low":0,"high":0,"unsigned":false}) : 0; /** * UninterpretedOption doubleValue. @@ -11488,7 +11488,7 @@ $root.google = (function() { * UninterpretedOption stringValue. * @type {Uint8Array} */ - $prototype.stringValue = $protobuf.util.emptyArray; + $prototype.stringValue = []; /** * UninterpretedOption aggregateValue.