Skip to content

Commit

Permalink
Properly annotate map fields in pbjs, fixes #624
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 7, 2017
1 parent 4b78628 commit f303049
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 79 deletions.
83 changes: 42 additions & 41 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.<string," + jsType + ">"; // keys are always strings
else if (field.repeated)
jsType = "Array.<" + jsType + ">";
var prop = util.safeProp(field.name);
if (config.comments) {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,31 @@ $root.Package = (function() {

/**
* Package bin.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.bin = $protobuf.util.emptyObject;

/**
* Package scripts.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.scripts = $protobuf.util.emptyObject;

/**
* Package dependencies.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.dependencies = $protobuf.util.emptyObject;

/**
* Package optionalDependencies.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.optionalDependencies = $protobuf.util.emptyObject;

/**
* Package devDependencies.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.devDependencies = $protobuf.util.emptyObject;

Expand Down
22 changes: 11 additions & 11 deletions tests/data/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 18 additions & 18 deletions tests/data/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -3422,7 +3422,7 @@ $root.jspb = (function() {
* TestClone bytesField.
* @type {Uint8Array}
*/
$prototype.bytesField = $protobuf.util.emptyArray;
$prototype.bytesField = [];

/**
* TestClone unused.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -4992,61 +4992,61 @@ $root.jspb = (function() {

/**
* TestMapFieldsNoBinary mapStringString.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.mapStringString = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapStringInt32.
* @type {number}
* @type {Object.<string,number>}
*/
$prototype.mapStringInt32 = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapStringInt64.
* @type {number|$protobuf.Long}
* @type {Object.<string,number|$protobuf.Long>}
*/
$prototype.mapStringInt64 = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapStringBool.
* @type {boolean}
* @type {Object.<string,boolean>}
*/
$prototype.mapStringBool = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapStringDouble.
* @type {number}
* @type {Object.<string,number>}
*/
$prototype.mapStringDouble = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapStringEnum.
* @type {number}
* @type {Object.<string,number>}
*/
$prototype.mapStringEnum = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapStringMsg.
* @type {jspb.test.MapValueMessageNoBinary}
* @type {Object.<string,jspb.test.MapValueMessageNoBinary>}
*/
$prototype.mapStringMsg = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapInt32String.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.mapInt32String = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapInt64String.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.mapInt64String = $protobuf.util.emptyObject;

/**
* TestMapFieldsNoBinary mapBoolString.
* @type {string}
* @type {Object.<string,string>}
*/
$prototype.mapBoolString = $protobuf.util.emptyObject;

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

/**
* TestMapFieldsNoBinary mapStringTestmapfields.
* @type {jspb.test.TestMapFieldsNoBinary}
* @type {Object.<string,jspb.test.TestMapFieldsNoBinary>}
*/
$prototype.mapStringTestmapfields = $protobuf.util.emptyObject;

Expand Down Expand Up @@ -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.
Expand All @@ -11488,7 +11488,7 @@ $root.google = (function() {
* UninterpretedOption stringValue.
* @type {Uint8Array}
*/
$prototype.stringValue = $protobuf.util.emptyArray;
$prototype.stringValue = [];

/**
* UninterpretedOption aggregateValue.
Expand Down

0 comments on commit f303049

Please sign in to comment.