Skip to content

Commit

Permalink
CLI: Accept null for optional fields in generated static code
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jul 11, 2017
1 parent c43243b commit ed7e2e7
Show file tree
Hide file tree
Showing 29 changed files with 790 additions and 777 deletions.
9 changes: 6 additions & 3 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ function buildType(ref, type) {
type.fieldsArray.forEach(function(field) {
var prop = util.safeProp(field.name);
prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
typeDef.push("@property {" + toJsType(field) + "} " + (field.optional ? "[" + prop + "]" : field.name) + " " + (field.comment || type.name + " " + field.name));
var jsType = toJsType(field);
if (field.optional)
jsType = jsType + "|null";
typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : prop) + " " + (field.comment || type.name + " " + field.name));
});
push("");
pushComment(typeDef);
Expand All @@ -391,10 +394,10 @@ function buildType(ref, type) {
push("");
var jsType = toJsType(field);
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type)
jsType = "(" + jsType + "|null|undefined)";
jsType = jsType + "|null|undefined";
pushComment([
field.comment || type.name + " " + field.name + ".",
"@member {" + jsType + "}" + escapeName(field.name),
"@member {" + jsType + "} " + escapeName(field.name),
"@memberof " + exportName(type),
"@instance"
]);
Expand Down
23 changes: 14 additions & 9 deletions dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/minimal/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/minimal/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/minimal/protobuf.min.js.gz
Binary file not shown.
23 changes: 14 additions & 9 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.8.0",
"version": "6.8.1",
"versionScheme": "~",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <[email protected]>",
Expand Down
6 changes: 3 additions & 3 deletions tests/data/comments.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as $protobuf from "../..";

export interface ITest1 {
field1?: string;
field2?: number;
field3?: boolean;
field1?: (string|null);
field2?: (number|null);
field3?: (boolean|null);
}

export class Test1 {
Expand Down
12 changes: 6 additions & 6 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ $root.Test1 = (function() {
* Properties of a Test1.
* @exports ITest1
* @interface ITest1
* @property {string} [field1] Field with a comment.
* @property {number} [field2] Test1 field2
* @property {boolean} [field3] Field with a comment and a <a href="http://example.com/foo/">link</a>
* @property {string|null} [field1] Field with a comment.
* @property {number|null} [field2] Test1 field2
* @property {boolean|null} [field3] Field with a comment and a <a href="http://example.com/foo/">link</a>
*/

/**
Expand All @@ -39,23 +39,23 @@ $root.Test1 = (function() {

/**
* Field with a comment.
* @member {string}field1
* @member {string} field1
* @memberof Test1
* @instance
*/
Test1.prototype.field1 = "";

/**
* Test1 field2.
* @member {number}field2
* @member {number} field2
* @memberof Test1
* @instance
*/
Test1.prototype.field2 = 0;

/**
* Field with a comment and a <a href="http://example.com/foo/">link</a>
* @member {boolean}field3
* @member {boolean} field3
* @memberof Test1
* @instance
*/
Expand Down
18 changes: 9 additions & 9 deletions tests/data/convert.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as $protobuf from "../..";

export interface IMessage {
stringVal?: string;
stringRepeated?: string[];
uint64Val?: (number|Long);
uint64Repeated?: (number|Long)[];
bytesVal?: Uint8Array;
bytesRepeated?: Uint8Array[];
enumVal?: Message.SomeEnum;
enumRepeated?: Message.SomeEnum[];
int64Map?: { [k: string]: (number|Long) };
stringVal?: (string|null);
stringRepeated?: (string[]|null);
uint64Val?: (number|Long|null);
uint64Repeated?: ((number|Long)[]|null);
bytesVal?: (Uint8Array|null);
bytesRepeated?: (Uint8Array[]|null);
enumVal?: (Message.SomeEnum|null);
enumRepeated?: (Message.SomeEnum[]|null);
int64Map?: ({ [k: string]: (number|Long) }|null);
}

export class Message {
Expand Down
36 changes: 18 additions & 18 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ $root.Message = (function() {
* Properties of a Message.
* @exports IMessage
* @interface IMessage
* @property {string} [stringVal] Message stringVal
* @property {Array.<string>} [stringRepeated] Message stringRepeated
* @property {number|Long} [uint64Val] Message uint64Val
* @property {Array.<number|Long>} [uint64Repeated] Message uint64Repeated
* @property {Uint8Array} [bytesVal] Message bytesVal
* @property {Array.<Uint8Array>} [bytesRepeated] Message bytesRepeated
* @property {Message.SomeEnum} [enumVal] Message enumVal
* @property {Array.<Message.SomeEnum>} [enumRepeated] Message enumRepeated
* @property {Object.<string,number|Long>} [int64Map] Message int64Map
* @property {string|null} [stringVal] Message stringVal
* @property {Array.<string>|null} [stringRepeated] Message stringRepeated
* @property {number|Long|null} [uint64Val] Message uint64Val
* @property {Array.<number|Long>|null} [uint64Repeated] Message uint64Repeated
* @property {Uint8Array|null} [bytesVal] Message bytesVal
* @property {Array.<Uint8Array>|null} [bytesRepeated] Message bytesRepeated
* @property {Message.SomeEnum|null} [enumVal] Message enumVal
* @property {Array.<Message.SomeEnum>|null} [enumRepeated] Message enumRepeated
* @property {Object.<string,number|Long>|null} [int64Map] Message int64Map
*/

/**
Expand All @@ -47,71 +47,71 @@ $root.Message = (function() {

/**
* Message stringVal.
* @member {string}stringVal
* @member {string} stringVal
* @memberof Message
* @instance
*/
Message.prototype.stringVal = "";

/**
* Message stringRepeated.
* @member {Array.<string>}stringRepeated
* @member {Array.<string>} stringRepeated
* @memberof Message
* @instance
*/
Message.prototype.stringRepeated = $util.emptyArray;

/**
* Message uint64Val.
* @member {number|Long}uint64Val
* @member {number|Long} uint64Val
* @memberof Message
* @instance
*/
Message.prototype.uint64Val = $util.Long ? $util.Long.fromBits(0,0,true) : 0;

/**
* Message uint64Repeated.
* @member {Array.<number|Long>}uint64Repeated
* @member {Array.<number|Long>} uint64Repeated
* @memberof Message
* @instance
*/
Message.prototype.uint64Repeated = $util.emptyArray;

/**
* Message bytesVal.
* @member {Uint8Array}bytesVal
* @member {Uint8Array} bytesVal
* @memberof Message
* @instance
*/
Message.prototype.bytesVal = $util.newBuffer([]);

/**
* Message bytesRepeated.
* @member {Array.<Uint8Array>}bytesRepeated
* @member {Array.<Uint8Array>} bytesRepeated
* @memberof Message
* @instance
*/
Message.prototype.bytesRepeated = $util.emptyArray;

/**
* Message enumVal.
* @member {Message.SomeEnum}enumVal
* @member {Message.SomeEnum} enumVal
* @memberof Message
* @instance
*/
Message.prototype.enumVal = 1;

/**
* Message enumRepeated.
* @member {Array.<Message.SomeEnum>}enumRepeated
* @member {Array.<Message.SomeEnum>} enumRepeated
* @memberof Message
* @instance
*/
Message.prototype.enumRepeated = $util.emptyArray;

/**
* Message int64Map.
* @member {Object.<string,number|Long>}int64Map
* @member {Object.<string,number|Long>} int64Map
* @memberof Message
* @instance
*/
Expand Down
32 changes: 16 additions & 16 deletions tests/data/mapbox/vector_tile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as $protobuf from "../../..";
export namespace vector_tile {

interface ITile {
layers?: vector_tile.Tile.ILayer[];
layers?: (vector_tile.Tile.ILayer[]|null);
}

class Tile {
Expand All @@ -30,13 +30,13 @@ export namespace vector_tile {
}

interface IValue {
stringValue?: string;
floatValue?: number;
doubleValue?: number;
intValue?: (number|Long);
uintValue?: (number|Long);
sintValue?: (number|Long);
boolValue?: boolean;
stringValue?: (string|null);
floatValue?: (number|null);
doubleValue?: (number|null);
intValue?: (number|Long|null);
uintValue?: (number|Long|null);
sintValue?: (number|Long|null);
boolValue?: (boolean|null);
}

class Value {
Expand All @@ -60,10 +60,10 @@ export namespace vector_tile {
}

interface IFeature {
id?: (number|Long);
tags?: number[];
type?: vector_tile.Tile.GeomType;
geometry?: number[];
id?: (number|Long|null);
tags?: (number[]|null);
type?: (vector_tile.Tile.GeomType|null);
geometry?: (number[]|null);
}

class Feature {
Expand All @@ -86,10 +86,10 @@ export namespace vector_tile {
interface ILayer {
version: number;
name: string;
features?: vector_tile.Tile.IFeature[];
keys?: string[];
values?: vector_tile.Tile.IValue[];
extent?: number;
features?: (vector_tile.Tile.IFeature[]|null);
keys?: (string[]|null);
values?: (vector_tile.Tile.IValue[]|null);
extent?: (number|null);
}

class Layer {
Expand Down
Loading

0 comments on commit ed7e2e7

Please sign in to comment.